Nais_One
Nais_One

Reputation: 566

Docker: sync files created by dockerfile with host through volume

When i use docker-compose with volumes to sync my files from host to container i cant see any new files created in the dockerfile

my docker-compose.yml:

version: "3"

services:
  web:
    image: nginx:latest
    volumes:
        - ./:/code
    links:
        - php

  php:
    build: .
    volumes:
      - ./:/code

My dockerfile looks like this:

FROM php:7-fpm

WORKDIR /code

RUN touch testfile

Of course thats a simplified example, but why do I not see the "testfile" on my host System? if I use docker-compose exec php touch testfile everything works as expected, i see the testfile on my host. From my understanding I do need to see it outside of my container for it to be shared with the other containers with the same volume (in this example nginx)

Upvotes: 1

Views: 1991

Answers (1)

leeman24
leeman24

Reputation: 2899

When you run RUN touch testfile within your Dockerfile it creates the testfile within the image itself.

Now when you start your container and volume mount your ./ directory to /code, it will mount over your existing /code folder in the image which is why you see it empty. If you didn't add the volume mount in your compose file, it would have the testfile in there.

Note: I don't fully understand your use case but if you wanted your image to create that file within the volume mount, you would need to add it to your entrypoint.

Upvotes: 1

Related Questions