Reputation: 736
I am trying to create a file while building a docker container, when I build it I don't get any error and it seems like the file was written correctly, however when I ssh into the container the file is not there.
I write the file by doing RUN echo "Some line to add to a file" >> /var/www/public/text.txt
on Dockerfile
My docker-compose.yml
looks like:
version: '2'
services:
web:
build: web/
ports:
- "8080:80"
volumes:
- ./web/:/var/www/
env_file:
- web/.env
I execute docker-compose up --build
docker exec -it <container id> bash
cd
to the folder /var/www/public
and the file text.txt
is not thereAny ideas?
Upvotes: 4
Views: 3686
Reputation: 107
You should run the command in docker-compose.yml file - version: '2'
services:
web:
build: web/
ports:
- "8080:80"
volumes:
- ./web/:/var/www/
env_file:
- web/.env
command:"echo "Some line to add to a file" >> /var/www/public/text.txt "
Since you are mounting the volumes after building the image.
Upvotes: 0
Reputation: 263726
Volume mounts apply at run time, not during a build. The output of a build is a portable image, not an image and some persistent data you would need to ship separately. That file should exist inside your image, depending on the rest of your Dockerfile.
When you mount a host volume at run time, anything inside the built image is hidden by the host mount. You only see files (along with permissions, uid, and gid) as they exist in the host.
However, if you switch to named volumes, docker will initialize that volume when it's new/empty with the contents of the image at that location.
Upvotes: 3