Dockerize old project

2021-01-03 docker

The log for dockirize Rails 4 project.

Use docker-compose.yml like below. lm-ubuntu18 is a local image which contains some libraries related to rubygems based on official Ubuntu image.

version: '3.8'
services:
  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./tmp/mysql:/var/lib/mysql
  web:
    image: lm-ubuntu18:latest
    command: sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ./:/home
      - bundle:/var/lib/gems/2.5.0
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  bundle:

Install bundler which is used in the project. You can find it in Gemfile.lock. Then run bundle update.

docker-compose run web gem install bundler -v 1.17.3 -N
docker-compose run web bundle update

Modify config/database.yml for development database env. Especially, the host is needed to set to db (not localhost).

Then create new database and run the web server.

docker-compose run web bundle exec rake db:create db:migrate db:seed
docker-compose up