Home > Software design >  How to make docker volume locations on the host relative to working directory?
How to make docker volume locations on the host relative to working directory?

Time:12-22

I have a working tree like this:

├── docker-compose.yml
├── folder1
│   ├── docker-compose.yml
│   └── volume1
│       └── source
└── volume
    └── source

root docker-compose.yml:

version: "3.4"

services:
    service:
        image: hello-world
        volumes:
            - ./volume/source:/volume/destination
        command: sleep 180

folder1/docker-compose.yml:

version: "3.4"

services:
    service1:
        image: hello-world
        volumes:
            - ${PWD}/volume1/source:/volume1/destination
        command:
            - sleep 180

When I go docker-compose -f docker-compose.yml -f folder1/docker-compose.yml config from the root directory, I get

services:
  service:
    command: sleep 180
    image: hello-world
    volumes:
    - /home/user/repos/learn/dockerl-compose/volume/source:/volume/destination:rw
  service1:
    command:
    - sleep 180
    image: hello-world
    volumes:
    - /home/user/repos/learn/dockerl-compose/volume1/source:/volume1/destination:rw
version: '3.4'

How do I make the locations of the bind mounts on the host machine relative to the location at which the command is executed?

CodePudding user response:

You should first write a script like this:

#!/bin/bash

SCRIPT_PATH=$(readlink -f $(dirname $0))
SCRIPT_PATH=${SCRIPT_PATH} docker-compose <YOUR_OWN_ARGUMENTS>

Then, inside your YAML, you should replace ${PWD} by ${SCRIPT_PATH}.

  • Related