docker-compose 用目录挂载单个文件

时间:2021-02-22 22:42:18

标签: docker docker-compose

如果我将文件放在相对于 docker-compose up 命令的子目录 (myconfig) 中,我将无法挂载文件。 $PWD 是 /home/username/temp/ 并且从那里运行 docker-compose up。

这不起作用:

version: '3'
services: 
  proxy:
    container_name: proxy
    image: nginx
    ports:
      - "80:80"
    volumes:
      - $PWD/myconfig/nginx.conf:/etc/nginx/conf.d/default.conf

但是如果我将 nginx.conf 复制到 $PWD 目录,这会起作用:

version: '3'
services: 
  proxy:
    container_name: proxy
    image: nginx
    ports:
      - "80:80"
    volumes:
      - $PWD/nginx.conf:/etc/nginx/conf.d/default.conf

所以这些都不起作用:

$PWD/myconfig/nginx.conf
./myconfig/nginx.conf
/home/username/temp/myconfig/nginx.conf

但是这些工作,如果我将 $PWD/myconfig/nginx.conf 复制到 $PWD/config.conf:

./nginx.conf 
$PWD/nginx.conf

日志:

Creating network "temp_default" with the default driver
Creating proxy ... error

ERROR: for proxy  Cannot create container for service proxy: not a directory

ERROR: for proxy  Cannot create container for service proxy: not a directory
ERROR: Encountered errors while bringing up the project.

但是,当我使用长语法时,它正在使用子目录:

version: '3'
services: 
  proxy:
    container_name: proxy
    image: nginx
    ports:
      - "80:80"
    volumes:
    - type: bind
      source: $PWD/myconfig/nginx.conf
      target: /etc/nginx/conf.d/default.conf

这里有什么我想念的吗,或者这是一个错误或缺乏文档?

Docker 和 docker-compose 版本:

➜  ~ docker version
Client: Docker Engine - Community
 Cloud integration: 1.0.7
 Version:           20.10.2
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        2291f61
 Built:             Mon Dec 28 16:17:34 2020
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8891c58
  Built:            Mon Dec 28 16:15:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
➜  ~ docker-compose version
docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

1 个答案:

答案 0 :(得分:1)

你的例子对我来说很好。

# docker-compose --version
docker-compose version 1.25.0, build unknown

既然您提到即使使用显式路径它也不起作用,但它在您的主文件夹中工作时,我建议检查 myconfig 文件夹的权限。确保所有用户都被允许访问和阅读('x' 和 'r')

# ls -ltr
total 8
drwxr-xr-x 2 root root 4096 Feb 23 06:03 myconfig
-rw-r--r-- 1 root root  182 Feb 23 06:05 docker-compose.yml

如果权限不正确,用chmod a+xr myconfig/chmod a+r myconfig/nginx.conf设置