0
0
Ruby on Railsframework~30 mins

Docker deployment in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker Deployment for a Rails Application
📖 Scenario: You have built a simple Rails web application and want to deploy it using Docker. Docker helps package your app and its environment so it runs the same everywhere.
🎯 Goal: Build a Docker setup for your Rails app by creating a Dockerfile, adding configuration for the database, and writing a Docker Compose file to run the app and database together.
📋 What You'll Learn
Create a Dockerfile with Ruby and Rails setup
Add a configuration variable for the Rails environment
Write a Docker Compose file to run the Rails app and PostgreSQL database
Ensure the app container depends on the database container
💡 Why This Matters
🌍 Real World
Docker is widely used to package and deploy Rails applications consistently across different machines and cloud platforms.
💼 Career
Knowing how to containerize Rails apps with Docker and Docker Compose is a valuable skill for backend developers and DevOps engineers working on modern web applications.
Progress0 / 4 steps
1
Create the Dockerfile for the Rails app
Create a file named Dockerfile with these exact lines to set up Ruby 3.2, install dependencies, copy the app code, and set the default command to start the Rails server.
Ruby on Rails
Need a hint?

Start from the official Ruby image, install Node.js and PostgreSQL client, set working directory, copy Gemfile files, run bundle install, then copy the rest of the app and set the command to start Rails server.

2
Add environment variable for Rails environment
In the Dockerfile, add a line to set the environment variable RAILS_ENV to development right after setting the working directory.
Ruby on Rails
Need a hint?

Use the ENV instruction in Dockerfile to set RAILS_ENV to development.

3
Create a Docker Compose file to run app and database
Create a file named docker-compose.yml with a version of "3.8". Define two services: db using the postgres:15 image with environment variables POSTGRES_USER: postgres and POSTGRES_PASSWORD: password, and web using the build context . and ports mapping 3000:3000. Make sure web depends on db.
Ruby on Rails
Need a hint?

Use version: "3.8" at the top. Define db service with postgres image and environment variables. Define web service with build context, ports, and depends_on db.

4
Add command to wait for database before starting Rails server
In the docker-compose.yml file, under the web service, add a command that uses sh -c to wait for the database on db:5432 using until and then starts the Rails server with rails server -b 0.0.0.0.
Ruby on Rails
Need a hint?

Use command: sh -c "until nc -z db 5432; do echo 'Waiting for db'; sleep 1; done; rails server -b 0.0.0.0" to wait for the database before starting the server.