0

Is it possible to "combine" naming volumes and defining the path between containers? (docker compose)

I got a docker compose file with multiple services which need to share volumes. At the same time I want to specify the path of these volumes for backup purposes and to control on which drive the data resides.

Currently I am using an .env file combined with the docker-compose file to solve this as follows.

File .env:

VOLUME_SHARED=/home/docker_user/data_service

File docker-compose.yml

version: '3.8'

services:
  service1:
    image: service1
    volumes:
      - ${VOLUME_SHARED}:/data

  service2:
    image: service2
    volumes:
      - ${VOLUME_SHARED}:/data

I know there are named volumes, but I couldnt find a way to define their path. Is there a way to locate all the information within the docker-compose file?

So to make it clear, is something like the following possible?

File docker-compose.yml

version: '3.8'

services:
  service1:
    image: service1
    volumes:
      - shared_data:/data

  service2:
    image: service2
    volumes:
      - shared_data:/data

volumes:
  shared_data=/home/docker_user/data_service
Burner
  • 71
  • 1
  • 12

1 Answers1

0

There is no such feature neither in compose file reference nor in docker volume create command manual. Thus you can't define storage location when you create a volume.

You can either change location for all docker files (see this post) or use bind mounts (as you do now).

There is also a hack which can do what you want but I don't recommend it. On Linux you can replace an existing volume with a soft link pointing somewhere else.

# move the volume to a new location
mv /var/lib/docker/volumes/postgres_volume/ /root
# replace it with a soft link
ln -sv /root/postgres_volume/ /var/lib/docker/volumes/postgres_volume
anemyte
  • 17,618
  • 1
  • 24
  • 45