'Sharing data between docker containers without making data persistent
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?
Solution 1:[1]
i think its better for you to use anonymous volume
volumes:
- ./:/var/www/html
Solution 2:[2]
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 a docker run
command.
Solution 3:[3]
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:
version: "3"
services:
one:
image: ubuntu
command: sleep 100000
volumes:
- vol:/vol
two:
image: ubuntu
command: sleep 100000
volumes:
- vol:/vol
volumes:
vol:
Then, in a 2nd terminal docker exec -it so_one_1 bash
(you might have to do a docker ps
to find the exact name of the container, it can change). You'll find yourself in a bash container. Change to the /vol directory cd /vol
and then echo "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 can cd /vol
and type ls -gAlFh
you'll see the wibble.txt file we created in the other container. You can even cat 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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Samuel Kristianto |
Solution 2 | Chris Becke |
Solution 3 | Software Engineer |