Docker/Ruby
Appearance
< Docker
Dockerfile
[edit | edit source]# Configures a Ruby web server and copies the current folder content
# to use as app root.
# Use the following commands to build and run the server.
# docker build -t ruby-server .
# docker run -d -p 8000:8000 --name=ruby-server ruby-server
# Then open a web browser and connect to http://localhost:8000 .
# References:
# https://hub.docker.com/_/ruby
FROM ruby:alpine
WORKDIR /usr/src/app
COPY . .
EXPOSE 8000
CMD ["ruby", "app.rb"]
app.rb
[edit | edit source]# Displays "Hello world!"
#
# References:
# https://en.wikipedia.org/wiki/WEBrick
require 'webrick'
server = WEBrick::HTTPServer.new(:Port => 8000)
server.mount_proc('/') do |request, response|
response.body = "Hello world!"
end
trap("INT") {server.shutdown}
server.start
Try It
[edit | edit source]Online Free
[edit | edit source]- Use Play with Docker. Create an account and/or log in.
- Start an interactive session and add a new instance.
- In the terminal window, enter the following commands:
touch Dockerfile
touch app.rb
- Use the
Editor
button to edit both files and save the contents above into each respective file. - Run the following commands:
docker build -t ruby-server .
docker run -d -p 8000:8000 --name=ruby-server ruby-server
- In the top window, select the
8000
button to connect to the running server.
On Your Own System
[edit | edit source]- Install Docker Desktop or the Docker Engine.
- Save the files above into a new
Docker Flask
folder:Dockerfile
app.rb
- At a command prompt, change to the
Docker Flask
folder and then run the following commands:docker build -t ruby-server .
docker run -d -p 8000:8000 --name=ruby-server ruby-server
- Open a web browser to connect to the running server: