-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e476ac
commit 3883476
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Use the official Ruby image from Docker Hub | ||
FROM ruby:latest | ||
|
||
# Set the working directory inside the container | ||
WORKDIR /usr/src/app | ||
|
||
# Copy the Ruby script into the container | ||
COPY app.rb . | ||
|
||
# Install the webrick gem | ||
RUN gem install webrick | ||
RUN gem install mysql2 | ||
|
||
# Expose port 8080 to be accessible from outside the container | ||
EXPOSE 8080 | ||
|
||
# Define the command to run the script | ||
CMD ["ruby", "app.rb"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'webrick' | ||
require 'mysql2' | ||
|
||
# Configure the MySQL client | ||
client = Mysql2::Client.new( | ||
host: 'db', # Docker Compose service name | ||
username: 'root', | ||
password: 'password', | ||
database: 'mydatabase' | ||
) | ||
|
||
# Define the web server | ||
server = WEBrick::HTTPServer.new(Port: 8080) | ||
|
||
# Define a response for the root path "/" | ||
server.mount_proc '/' do |req, res| | ||
# Query the database | ||
results = client.query('SELECT * FROM mytable') | ||
|
||
# Build the HTML response | ||
html = '<html><body><h1>Data from MySQL</h1><ul>' | ||
results.each do |row| | ||
html += "<li>#{row['name']}</li>" | ||
end | ||
html += '</ul></body></html>' | ||
|
||
res.body = html | ||
end | ||
|
||
# Trap interrupts (Ctrl+C) to gracefully shut down the server | ||
trap 'INT' do | ||
server.shutdown | ||
end | ||
|
||
# Start the server | ||
server.start | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
services: | ||
db: # The name of your service | ||
image: mysql/mysql-server:latest # The image to use | ||
environment: # Environment variables | ||
- MYSQL_ROOT_HOST=% | ||
- MYSQL_ROOT_PASSWORD=password | ||
ports: | ||
- "3306:3306" # Port mapping | ||
container_name: db # Name of the container | ||
restart: unless-stopped # Restart policy | ||
volumes: | ||
- ./sql-data:/docker-entrypoint-initdb.d # Mount the initialization directory | ||
|
||
app: | ||
depends_on: | ||
- db | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
ports: | ||
- "8080:8080" # Map port 8080 of the container to port 8080 on the host | ||
container_name: ruby-hello-world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE DATABASE IF NOT EXISTS mydatabase; | ||
USE mydatabase; | ||
|
||
CREATE TABLE IF NOT EXISTS mytable ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); | ||
|
||
INSERT INTO mytable (name) VALUES ('Hello, World!'); | ||
|