Docker Compose - Share named volume between multiple containers
This solves it without using named volumes:
volumes:
- ./appdata:/appdata
So, it looks like:
services:
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes:
- ./appdata:/appdata
php:
build: ./php/
expose:
- 9000
volumes:
- ./appdata:/appdata
Previous answers helped me a lot (thanks!) but it took some more time to figure out how to set options to create a shared tmpfs
volume (memory file system). I'm sharing it in the hope to make life easier for developers in the same situation.
An example showing only the relevant parts of docker-compose.yml
:
version: '3'
volumes:
shared-tmpfs:
driver: local
driver_opts:
type: "tmpfs"
device: "tmpfs"
o: "size=256m,uid=1000"
services:
nginx:
volumes:
- shared-tmpfs:/tmp/mytmpfs
php-fpm:
volumes:
- shared-tmpfs:/tmp/mytmpfs
I use it to spare my SSD from heavy writes (building lot's of static html files) in development/watch mode.
You can read more about driver_opts
in the official Docker docs here.
The docker named volumes were removed starting from version docker-compose 3.
However, you can use extension-fields to avoid duplicating volumes source and prevent yourself from future typos:
version: '3.5'
x-services-volume:
&services-volume
type: bind
source: ./appdata
target: /appdata
services:
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes: *services-volume
php:
build: ./php/
expose:
- 9000
# Use same way as for nginx if target override not needed.
volumes:
- <<: *services-volume
target: /opt/target-override
NOTE: This feature is available starting from version 3.4 file format.
The named volumes can be shared across containers in the following way:
services:
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes:
- app-volume:location_in_the_container
php:
build: ./php/
expose:
- 9000
volumes:
- app-volume:location_in_the_container
volumes:
app-volume:
Here's an example config that I use for better understanding. I'm exposing the static files generated from my web
container to a named volume called static-content
which is then read and served by the nginx
container:
services:
nginx:
container_name: nginx
build: ./nginx/
volumes:
- static-content:/usr/src/app
web:
container_name: web
env_file: .env
volumes:
- static-content:/usr/src/app/public
environment:
- NODE_ENV=production
command: npm run package
volumes:
static-content: