Augusto Queirantes
Posted on September 17, 2022
Hey guys! How are you!
That's the second article of a series that teaches how to create a production ready Rails application. In this article we'll configure docker and docker compose.
Configuring docker
First thing we need to do is create a file called Dockerfile
in the root path of the application.
The following snippet contains the file content:
FROM ruby:3.1.2-alpine3.16
RUN apk --update add postgresql-dev postgresql-client build-base tzdata libxslt-dev libxml2-dev
RUN mkdir /app
WORKDIR /app
This file creates a image that contains everything the application needs to run. To create this image docker follow this steps:
FROM ruby:3.1.2-alpine3.16
This line specifies that docker should download the following image from dockerhub and use it as base to run the following commands
RUN apk --update add postgresql-dev postgresql-client build-base tzdata libxslt-dev libxml2-dev
This instruction tells to docker install the following dependencies:
- build-base: To ensure certain gems can be compiled
- tzdata: Handle timezones
- libxslt-dev libxml2-dev: Nokogiri native dependencies
- postgresql-dev postgresql-client: Postgres related
RUN mkdir /app
Creates a folder called "/app"
WORKDIR /app
Tells docker that the folder "/path" is the main folder of the application.
Configuring docker compose
The first thing to do to configure docker-compose is create a file called docker-compose.yml
in the root path of the application. This file should have the following content:
version: '3.9'
services:
postgres:
image: postgres:14.5-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: 123456
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
rails_guide:
build: .
volumes:
- .:/app
image: rails_guide
container_name: rails_guide
tty: true
ports:
- "3000:3000"
environment:
- DATABASE_NAME=app_development
- DATABASE_USERNAME=user
- DATABASE_PASSWORD=123456
- DATABASE_HOST=postgres
depends_on:
- postgres
networks:
- postgres
networks:
postgres:
driver: bridge
volumes:
postgres:
Executing docker
Now that everything is configured run the following command to create the containers:
docker-compose up -d
This command will create both containers and get it running
You can verify if everything is ok by running the following commando
docker ps
The output should looks like this:
Now run this command to go inside the container:
docker exec -it rails_guide sh
Setting up the database
You'll need to update the database configuration to mathces the docker environment, you'll need to change config/database.yml
with the following content:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch("DATABASE_HOST", "localhost") %>
user: <%= ENV.fetch("DATABASE_USERNAME", "user") %>
password: <%= ENV.fetch("DATABASE_PASSWORD", "123456") %>
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: <%= ENV.fetch("DATABASE_NAME", "app_production") %>
To setup the database you'll need to run the following commands:
rails db:drop
rails db:create
rails db:migrate
Running the server
You can verify if everything is ok by starting the server by running the following command:
rails s -b 0.0.0.0
If everything is ok you'll see something like this:
You can see all code changes here
Posted on September 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.