[Ruby] [Rails] How to speed up docker-compose

less than 1 minute read

Development environment

・Docker: 19.03.8 ・Docker-Compose: 1.25.5 ・Ruby: 2.5.7 Rails: 5.2.4 ・Vagrant: 2.2.7 ・VirtualBox: 6.1 ・OS: macOS Catalina

Premise

Implemented below.

How to build environment with Docker

Cause

When running docker-compose, extra files such as temp, log, vendor, and .git are mounted.

Solution

Overwrite mounts of directories that do not need to be mounted with another volume.

edit docker-compose.yml

docker-compose.yml


# Change before
version: '3'
services:
  db:
    image: postgres
    volumes:
      -./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build:.
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      -.:/myapp
    ports:
      -"3000:3000"
    depends_on:
      -db

# After change
version: '3'
services:
  db:
    image: postgres
    volumes:
      -./tmp/db:/var/lib/postgresql/data
    environment:
      -POSTGRES_HOST_AUTH_METHOD=trust
  web:
    build:.
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      -.:/app:cached
      -/app/vendor
      -/app/tmp
      -/app/log
      -/app/.git
    ports:
      -'3000:3000'
    depends_on:
      -db