loading...

Cache Rails gems using docker-compose

k_penguin_sato profile image K-Sato ・1 min read

Introduction

Let's suppose you have a docker-compose.yml like the one below.
You have to run docker-compose build every time you add/remove a gem to/from your Gemfile and it is very time-consuming.
It would be great if I could just run bundle install and get back to developing the application.
Well... I'll show you exactly that in this post.

version: "3"
services:
  app:
    build: .
    volumes:
      - .:/app
    depends_on:
      - postgres
  postgres:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

Set up your docker-compose.yml

Follow the steps below.

  • (1) Set up BUNDLE_PATH using environment.
  • (2) Set up the named volume bundle_path and add it to the top level volumes.

Here is the modified docker-compose.yml.

version: "3"
services:
  app:
    build: .
    volumes:
      - .:/app
      - bundle_path:/bundle # New
    environment:
      - BUNDLE_PATH=/bundle/vendor #New
    depends_on:
      - postgres
  postgres:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  bundle_path: # New
  postgres-data:

How to use it?

Create the cache of the gems

Run the command below to create the cache of the gems after you modified your docker-compose.yml.

$ docker-compose app bundle install

Add or Remove a gem

  • (1) Stop the running containers.
$ docker-compose down
  • (2) Add a new gem and run the command below.
$ docker-compose run app bundle install
  • (3) Get the containers running again.
$ docker-compose up

Posted on by:

k_penguin_sato profile

K-Sato

@k_penguin_sato

I am a software-engineer based somewhere on earth.

Discussion

markdown guide
 

There must be a typo on first command .

docker-compose app bundle install

Maybe you meant

docker-compose run app bundle install

Thanks for the good work.