How To Dockerize an Existing Ruby on Rails Application | by Mahbub Zaman | Towards Data Science

Ruby on Rails is an open-source web application development framework based on Ruby. Many big companies use Rails to build their products, such as GitHub, Shopify, Airbnb, and Twitch.
From this post, you’ll learn how to dockerize an existing Ruby on Rails application to make development faster and easier.
First, we need need to install Docker and download a git repository from GitHub. For this setup, I’m using macOS.
Now, we will create a Docker image that contains Ruby on Rails. Let’s break down the individual ingredients of the Dockerfile file.
FROM ruby:3.0.0RUN apt-get update -qq \&& apt-get install -y nodejs postgresql-clientADD . /Rails-DockerWORKDIR /Rails-DockerRUN bundle installEXPOSE 3000CMD ["bash"]
Here we are using the From instruction to define the parent image. We are using the pre-built official image of Ruby from Docker Hub. After that, we are installing all the dependencies using the RUN instruction. Now we need to add everything from the current folder into a directory in the image called Rails-Docker by using the ADD instruction. The WORKDIR instruction will set the working directory to Rails-Docker. Again we are using the RUN instruction to install our ruby gems. After that, we are exposing port 3000 for our container using the EXPOSE instruction. By default, Rails run on port 3000. Finally, the CMD instruction is used to run a command when a container starts. In this case, it’s used to open the bash shell.
Now, we need to run the db and web containers. For this, we will create a Docker compose file. Let’s break down the individual ingredients of the docker-compose.yml file.
version: '3.8'services:  db:    image: postgres    environment:      POSTGRES_USER: postgres      POSTGRES_PASSWORD: password    volumes:      - postgres:/var/lib/postgresql/data  web:    build: .    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"    volumes:      - .:/Rails-Docker    ports:      - "3000:3000"    depends_on:      - dbvolumes:  postgres:
The version tag is used to define the Compose file format. The services tag is used to define the services we want to use for our application. Here, we have two services called db and web.
Under the db service, we are using the pre-built postgres image to build the image. Then we have enviriomnt varaibles. Finally, we have a named volume called postgres to persist data.
Under the web service, we are using the Dockerfile we created earlier to build the image. The command tag is used to run our application. We are using ‘0.0.0.0’ so that the server will listen to all available IP addresses. The ports tag is used to define both host and container ports. It maps port 3000 on the host to port 3000 on the container. Finally, the volumes tag is used to mount a folder from the host machine to the container.
Now run the following commands from the same directory where the docker-compose.yml file is located. This command will start and run the entire app.
docker compose up
If you encounter the an error “ActiveRecord::NoDatabaseError” then run the following command to create the database.
docker-compose run web rake db:create
Congratulations! You can now access the Rails application via your favorite web browser by vising the URL http://localhost:3000/.
I hope this will help you to get started with Rails and Docker. Now create your next big thing using Rails. Happy coding!