Docker for Rails: From Development to Deployment (Complete Guide)
Docker simplifies building, shipping, and running applications in isolated containers. This guide covers:
✅ Running a Rails app with Docker
✅ Deploying to Docker Hub
✅ Running the app on another machine with zero setup
1. Setting Up a Rails App with Docker
Prerequisites
Step 1: Create a Rails App
1
2
| rails new docker_rails_demo --database=postgresql
cd docker_rails_demo
|
Add a Welcome Page
1
| rails generate controller Welcome index
|
Update config/routes.rb
:
1
2
3
| Rails.application.routes.draw do
root 'welcome#index'
end
|
Step 2: Docker Configuration
ignore while pushing the code
.dockerignore
list env vars
.env
1. Write script for installation. Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| # Use official Ruby image (updated to a valid version)
FROM ruby:3.2.6-slim
# Install dependencies
RUN apt-get update -qq && apt-get install -y \
build-essential \
libpq-dev \
nodejs \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /siv_rails_app_dockerdemo
# Install gems
COPY Gemfile Gemfile.lock ./
RUN bundle install
# Copy application code
COPY . .
# Add a script to be executed every time the container starts
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# Expose port
EXPOSE 3001
# Start the main process
CMD ["rails", "server", "-b", "0.0.0.0"]
|
2. entrypoint.sh
1
2
3
4
5
6
7
8
| #!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails
rm -f /siv_rails_app_dockerdemo/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile)
exec "$@"
|
3. docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| version: "3.8"
services:
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && rails server -b 0.0.0.0"
volumes:
- .:/siv_rails_app_dockerdemo
ports:
- "3001:3000"
depends_on:
- db
environment:
DATABASE_URL: postgres://postgres:password@db:5432/dockerrails_development
volumes:
postgres_data:
|
Step 3: Run the App
1
2
3
4
5
| # Build containers
docker-compose build
# Start services
docker-compose up
|
Access: http://localhost:3001
Initialize the Database
1
2
| docker-compose exec web rails db:create
docker-compose exec web rails db:migrate
|
2. Deploying to Docker Hub
1. Build & Tag the Image
1
| docker build -t shivrajbadu/docker_rails_demo:1.0 .
|
2. Push to Docker Hub
1
2
| docker login
docker push shivrajbadu/docker_rails_demo:1.0
|
3. Running the App on Another Machine
1. Create a New Directory
1
| mkdir ~/docker_rails_deploy && cd ~/docker_rails_deploy
|
2. Add docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| version: "3.8"
services:
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
web:
image: shivrajbadu/docker_rails_demo:1.0
command: bash -c "rm -f tmp/pids/server.pid && rails server -b 0.0.0.0"
depends_on:
- db
environment:
DATABASE_URL: postgres://postgres:password@db:5432/docker_rails_demo_production
RAILS_ENV: production
ports:
- "3002:3000"
volumes:
postgres_data:
|
3. Start the App
Access: http://localhost:3002
Initialize DB (If Needed)
1
| docker-compose exec web rails db:create db:migrate
|
Key Docker Commands Cheat Sheet
Command |
Description |
docker-compose --build |
Build dependencies |
docker-compose up |
Start containers. Run the services and processes. |
docker-compose down |
Stop containers |
docker-compose logs web |
View Rails logs |
docker-compose exec web bash |
Enter container shell |
docker-compose exec web rails c |
Open Rails console |
docker ps |
List running containers, processes |
docker push <image> |
Push to Docker Hub |
docker container ls |
to see container list |
docker-compose stop |
to stop all the processes for later restart |
docker-compose run website rake db:migrate db:seed RAILS_ENV=production |
website means name of service to run rails app |
docker-compose run --rm website rake db:create db:migrate |
website is name of service |
sudo docker run -i -t <image/id> /bin/bash |
enter into bash shell |
docker rmi 24a77bfbb9ee -f |
forcefully remove image |
docker rm $(docker ps -a -q) |
remove all the containers |
Note: If you don’t have Docker running on your local machine, you need to replace localhost in the above URL with the IP address of the machine Docker is running on. If you’re using Docker Machine, you can run below cmd to find out the IP.
docker-machine ip “${DOCKER_MACHINE_NAME}”