新容器访问现有容器上的卷

时间:2020-01-15 12:41:59

标签: docker docker-compose traefik

我有一个“主”容器,启动其他所有容器时应该已经在运行。

其中有一个conf/目录,该服务正在监视和应用相关更改。

如何让每个新容器在此目录中放置一个文件?


真实情况:

鉴于我在下面的docker-compose.yml,我希望每个服务(portainerwhoamiapache)将.yml文件拖放到{{1} } "./traefik/conf/:/etc/traefik/conf/"服务的路径映射。

docker-compose.yml

traefik

portainer.traefik.yml

version: "3.5"

services:

    traefik:
        image: traefik
        env_file: ./traefik/env
        restart: unless-stopped
        ports:
            - "80:80"
            - "443:443"
            - "8080:8080"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - ./traefik/conf/:/etc/traefik/conf/
            - ./traefik/traefik.yml:/etc/traefik/traefik.yml

    portainer:
        image: portainer/portainer
        depends_on: [traefik]
        command: --no-auth -H unix:///var/run/docker.sock
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock

    whoami:
        image: containous/whoami
        depends_on: [traefik]

whoami.traefik.yml

http:
    routers:
        portainer:
            entryPoints: [http]
            middlewares: [redirect-to-http]
            service: portainer-preauth@docker
            rule: Host(`portainer.docker.mydomain`)

文件portainer.traefik.yml和whoami.traefik.yml在哪里 位于?如果它们在主机上,则可以直接将它们复制到 ./traefik/conf/。 – Shashank V

问题是我无法在http: routers: whoami: entryPoints: [http] middlewares: [redirect-to-http] service: whoami-preauth@docker rule: Host(`whoami.docker.mydomain`) 中拥有所有文件。
每次创建新图像时,都需要手动将文件拖放到那里。
我认为每个服务都应对自己的文件负责。
同样,当traefik/conf启动并查找那些尚未启动的其他服务的文件时,它会记录很多错误。
为了避免这种行为,我只想在启动容器时才将文件放在那里。

下面是项目文件结构。

project file structure

1 个答案:

答案 0 :(得分:1)

您可以在所有服务中使用卷。只需在docker-compose.yml中定义它,并将其分配给每个服务即可:

version: "3.5"

services:

    traefik:
        image: traefik
        env_file: ./traefik/env
        restart: unless-stopped
        ports:
            - "80:80"
            - "443:443"
            - "8080:8080"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - ./traefik/conf/:/etc/traefik/conf/
            - ./traefik/traefik.yml:/etc/traefik/traefik.yml
            - foo:/path/to/share/

    portainer:
        image: portainer/portainer
        depends_on: [traefik]
        command: --no-auth -H unix:///var/run/docker.sock
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - foo:/another/path/to/share/

    whoami:
        image: containous/whoami
        depends_on: [traefik]
        volumes:
          - foo:/and/another/path/

    volumes:
      foo:
        driver: local

这等效于“普通” Docker的--volumes-from功能。或者至少是最接近它的东西。

您的主容器将不得不使用相同的体积。如果此容器不在同一Docker Compose上下文中运行,则必须在外部定义此卷。