Published | 09/06/2018 |
---|---|
Last Updated | 20/11/2024 |
Docker is a great tool to synchronize dev envinronment on different machine. This resolves the excuse "not work on my machine". This also helps new devs to quickly set up the project without scratching their head with all the missing libs, mismatch versions, etc.
This article will describe how to setup Docker and Docker Compose for a Rails app that uses Postgresql/MySQL as database, Sidekiq and Redis.
Software versions use in this article
Docker version 18.03.1-ce, build 9ee9f40
docker-compose version 1.21.2, build a133471
Ruby 2.6.1
Rails 5
Postgres 10.6
MySQL 5.7.22
Redis 4.0.9
Installation guides for Docker and Docker Compose are well written in Docker docs.
FROM ruby:2.6.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /demoapp
WORKDIR /demoapp
COPY Gemfile /demoapp/Gemfile
COPY Gemfile.lock /demoapp/Gemfile.lock
RUN bundle install
COPY . /demoapp
version: '3'
services:
db:
image: mysql:5.7.22
volumes:
- ./tmp/db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
redis:
image: redis:4.0.9
volumes:
- ./tmp/redis:/data
web:
depends_on:
- db
- redis
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/demoapp
ports:
- 3000:3000
environment:
- REDIS_URL=redis://redis:6379/
sidekiq:
depends_on:
- db
- redis
build: .
command: sidekiq -C config/sidekiq.yml
volumes:
- .:/demoapp
environment:
- REDIS_URL=redis://redis:6379/
If the app uses PostgreSQL instead of MySQL, the DB image will be like follow:
db:
image: postgres:10.6
volumes:
- ./tmp/db:/var/lib/postgresql/data
If the app uses MongoDB:
version: '3'
services:
mongodb:
image: mongo
ports:
- "27017:27017"
web:
...
depends_on:
- mongodb
Then we'll need to update config/database.yml
to point to the DB image:
For MySQL
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: 123456
host: db
development:
<<: *default
database: demoapp_development
test:
<<: *default
database: demoapp_test