Skip to content

Instantly share code, notes, and snippets.

@letsjump
Last active November 14, 2022 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letsjump/f289596cda28a4323e88660bdc20baf9 to your computer and use it in GitHub Desktop.
Save letsjump/f289596cda28a4323e88660bdc20baf9 to your computer and use it in GitHub Desktop.
A simple Rails application in one Docker container

A simple Rails application in one Docker container

This Gist helps to build and start a simple Rails application in a single Docker container (no docker-compose needed).

  1. Go to your projects folder
cd /home/user/my_projects
  1. Run a temporary ruby:2.x container that will install Rails
docker run --rm -v "${PWD}":/application -w /application  ruby:2.7 /bin/bash -c "gem install rails -v '6.1' && rails new simple_rails_app_container --minimal ."
  1. Change the newly created folder's permissions to match the current user
sudo chown -R ${USER:=$(/usr/bin/id -run)}:$USER simple_rails_app_container
  1. Enter the newly created folder
cd /home/user/my_projects/simple_rails_app_container
  1. Create a Dockerfile with this content
FROM ruby:2.7

RUN apt-get update -qq && \
    apt-get install -y nodejs && \
    apt-get clean && \
    bundle config --global frozen 1

WORKDIR /application

COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY . .

EXPOSE 3000

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
  1. Build the custom image
docker build -t simple_rails_app_container:1.0 . 
  1. Run it
docker run --name simple_rails_app_container-1 -v "${PWD}":/application -p3000:3000 simple_rails_app_container:1.0
  1. Try it in your browser: http://0.0.0.0:3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment