Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why need COPY Gemfile in Dockerfile to docker while set volumes in docker-compose.yml?

I followed https://docs.docker.com/samples/rails/ to setup docker environment.

I found it set WORKDIR /myapp in Dockerfile and set volumes mount in docker-compose.yml:

services:
  web:
    build: .
    command: bundle exec rails server -b 0.0.0.0
    volumes:
      - .:/myapp

Why still need config these two COPY commands to invoke RUN bundle install ??

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

If I remove it from Dockerfile, when I run docker compose build, got following error:

 => ERROR [5/5] RUN bundle install                                                                                                                                                                     0.7s
------
 > [5/5] RUN bundle install:
#9 0.629 Could not locate Gemfile

Is there no Gemfile file in docker /myapp directory? Volumes mount not worked? I thought after I set volumes mount in docker-compose.yml, the Gemfile and other source code files should exist in /myapp directory.

>Solution :

Volumes are only mounted at run-time. The RUN bundle install command is run at build-time where the mounted volume isn’t available.

I often recommend keeping building and running the image separate, since it can be confusing what is used when.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading