Dockerization of Rails 6 app
Play this article
Things you need to Learn
Please visit my blog post to learn about Docker and its installation from Containarization and Docker.
Using docker-compose to dockerize the app
Steps:-
Chdir
to the project directoryCreate an
entry point
file calleddocker-entrypoint.sh
#!/bin/bash set -e bundle check || bundle install --binstubs="$BUNDLE_BIN" echo "command executing $@" exec "$@"
Create a
Dockerfile
with the content like below:FROM ruby:2.6.3 # Needed to compile nodejs RUN apt-get update -qq && apt-get install -y build-essential RUN apt-get install -y libpq-dev RUN apt-get install -y libxml2-dev libxslt1-dev RUN apt-get install -y nodejs RUN mkdir /app WORKDIR /app ADD . /app EXPOSE 3000 COPY ./docker-entrypoint.sh / RUN chmod +x /docker-entrypoint.sh ENTRYPOINT ["/docker-entrypoint.sh"] ENV BUNDLE_PATH=/bundle \ BUNDLE_BIN=/bundle/bin \ GEM_HOME=/bundle ENV PATH="${BUNDLE_BIN}:${PATH}" RUN gem install bundler
Create
docker-compose-yml
fileversion: '3' services: postgres: image: postgres:9.6 ports: - '5432:5432' volumes: - postgres:/var/lib/postgresql/data web: build: . command: puma # we are using puma volumes: - bundle:/bundle - .:/app ports: - '3000:3000' links: - postgres volumes: bundle: postgres:
database.yml
file need to changedevelopment: <<: *default host: postgres # service name in docker-compose.yml username: postgres # has to be this way password: database: my_app_development
postgres
is the name of the service which will be your host.
Everytime you change
Dockerfile
, you need to rebuild the image usingdocker-compose build
You need to create the database the first time
docker-compose run web rails db:create && rails db:migrate && rails db:seed
or you can run
docker-compose run web rails db:create
docker-compose run web rails db:migrate
docker-compose run web rails db:seed
Then, your application will be available via localhost:3000
in browsers.
Summary
In this way you can move your existing Rails 5/6 projects to Docker
using Docker Compose
.