Leon
Leon

Reputation: 6069

Dockerfile and docker-compose not updating with new instructions

When I try to build a container using docker-compose like so

nginx:
  build: ./nginx
  ports:
    - "5000:80"

the COPY instructions isnt working when my Dockerfile simply looks like this

FROM nginx

#Expose port 80
EXPOSE 80

COPY html /usr/share/nginx/test

#Start nginx server
RUN service nginx restart

What could be the problem?

Upvotes: 50

Views: 47750

Answers (3)

Davion
Davion

Reputation: 51

If you need to make docker-compose to copy files every time on up command I suggest declaring a volumes option to your service in the compose.yml file. It will persist your data and also will copy files from that folder into the container.

More info here volume-configuration-reference

server:
    image: server
    container_name: server
    build:
      context: .
      dockerfile: server.Dockerfile
    env_file:
      - .envs/.server
    working_dir: /app
    volumes:
      - ./server_data:/app  # <= here it is
    ports:
      - "9999:9999"
    command: ["command", "to", "run", "the", "server", "--some-options"]

Optionally, you can add the following section to the end of the compose.yml file. It will keep that folder persisted then. The data in that folder will not be removed after the docker-compose stop command or the docker-compose down command. To remove the folder you will need to run the down command with an additional flag -v:

docker-compose down -v

For example, including volumes:

services:
  server:
    image: server
    container_name: server
    build:
      context: .
      dockerfile: server.Dockerfile
    env_file:
      - .envs/.server
    working_dir: /app
    volumes:
      - ./server_data:/app  # <= here it is
    ports:
      - "9999:9999"
    command: ["command", "to", "run", "the", "server", "--some-options"]

volumes:  # at the root level, the same as services
  server_data:

Upvotes: 1

Philippe Boulanger
Philippe Boulanger

Reputation: 371

I had the same issue and a one liner that does it for me is :

docker-compose up --build --remove-orphans --force-recreate

  • --build does the biggest part of the job and triggers the build.
  • --remove-orphans is useful if you have changed the name of one of your services. Otherwise, you might have a warning leftover telling you about the old, now wrongly named service dangling around.
  • --force-recreate is a little drastic but will force the recreation of the containers.

Reference: https://docs.docker.com/compose/reference/up/

Warning I could do this on my project because I was toying around with really small container images. Recreating everything, everytime, could take significant time depending on your situation.

Upvotes: 26

Leon
Leon

Reputation: 6069

It seems that when using the docker-compose command it saves an intermediate container that it doesnt show you and constantly reruns that never updating it correctly. Sadly the documentation regarding something like this is poor. The way to fix this is to build it first with no cache and then up it like so

docker-compose build --no-cache
docker-compose up -d

Upvotes: 87

Related Questions