Let’s say I have a docker-compose file with two containers:
version: "3"
services:
app:
image: someimage:fpm-alpine
volumes:
- myvolume:/var/www/html
web:
image: nginx:alpine
volumes:
- myvolume:/var/www/html
volumes:
myvolume:
The app container contains the application code in the /var/www/html
directory which gets updated with each version of the image, so I don’t want this directory to be persistent.
Yet I need to share the data with the nginx container. If I use a volume or a host bind the data is persistent and doesn’t get updated with a new version. Maybe there is a way to automatically delete a volume whenever I pull a new image? Or a way to share an anonymous volume?
3
Answers
You would have to be willing to drop back to docker-compose version 2 and use data containers with the
volumes_from
directive.Which is equivalent to
--volumes-from
on adocker run
command.This should work fine. The problem isn’t with docker. You can use volumes to communicate in this way. If you run
docker-compose up
in a directory with the following compose file:Then, in a 2nd terminal
docker exec -it so_one_1 bash
(you might have to do adocker ps
to find the exact name of the container, it can change). You’ll find yourself in a bash container. Change to the /vol directorycd /vol
and thenecho "wobble" > wibble.txt", then
exit` the shell (ctrl-d).In the same terminal you can then type
docker exec -it so_two_1 bash
(again, check the names). Just like last time you cancd /vol
and typels -gAlFh
you’ll see the wibble.txt file we created in the other container. You can evencat wibble.txt
to see the contents. It’ll be there.So if the problem isn’t docker, what can it be? I think the problem is that nginx isn’t seeing the changes on the filesystem. For that, I believe that setting
expires -1;
inside a location block in the config will actually disable caching completely and may solve the problem (dev only).i think its better for you to use anonymous volume