简体   繁体   English

如何使主机上的 docker 卷位置相对于工作目录?

[英]How to make docker volume locations on the host relative to working directory?

I have a working tree like this:我有一个这样的工作树:

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

root docker-compose.yml:根 docker-compose.yml:

version: "3.4"

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

folder1/docker-compose.yml: 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当我从根目录docker-compose -f docker-compose.yml -f folder1/docker-compose.yml config时,我得到

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?如何使主机上绑定安装的位置相对于执行命令的位置?

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} .然后,在您的YAML中,您应该将${PWD}替换为${SCRIPT_PATH}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM