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

Time:02-05

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 ??

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.

CodePudding user response:

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.

  •  Tags:  
  • Related