gpt4 book ai didi

docker - 当外部链接的容器实际上正在使用docker-compose运行时,如何避免 “Docker cannot link to a non running container”错误

转载 作者:行者123 更新时间:2023-12-02 19:34:53 25 4
gpt4 key购买 nike

我们想要做什么:

我们想使用docker-compose通过容器名称将一个已经在运行的容器(A)链接到另一个容器(B)。我们使用“外部链接”,因为两个容器都是从不同的docker-compose.yml文件启动的。

问题:

尽管正在运行具有该名称的容器,但容器B无法从错误开始。

ERROR: for container_b  Cannot start service container_b: Cannot link to a non running container: /PREVIOUSLY_LINKED_ID_container_a_1 AS /container_b_1/container_a_1

“docker ps”的输出:
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                         NAMES
RUNNING_ID container_a "/docker-entrypoint.s" 15 minutes ago Up 15 minutes 5432/tcp container_a_1

示例代码:

容器B的docker-compose.yml:
container_b:
external_links:
- container_a_1

此问题与其他“如何解决”问题有什么不同:
  • 我们不能使用“sudo服务docker restart”(有效),因为这是生产环境
  • 我们不想每次都手动修复此问题,但是找到原因以便我们可以
  • 了解我们在做什么错
  • 了解如何避免这种

  • 假设:
  • 似乎存在container_a的两个实例(RUNNING_ID和PREVIOUSLY_LINKED_ID)
  • 这可能是因为我们
  • 通过docker-compose build和
  • 重建了容器
  • 更改了容器的转发外部端口(8080 1 :8080)

  • 评论
  • 不要按照注释中的建议使用docker-compose down,这样可以消除体积!
  • 最佳答案

    Docker links are deprecated,因此除非您需要它们提供的某些功能或在极老的Docker版本上使用,否则建议您切换到docker网络。

    由于您要连接的容器似乎是在单独的撰写文件中启动的,因此您可以在外部创建该网络:

    docker network create app_net

    然后在docker-compose.yml文件中,将容器连接到该网络:
    version: '3'

    networks:
    app_net:
    external:
    name: app_net

    services:
    container_a:
    # ...
    networks:
    - app_net

    然后,在您的container_b中,您将以“container_a”而不是“container_a_1”连接到container_a。

    顺便说一句,除非您传递 docker-compose down标志,否则不记录 -v来删除卷。也许您正在使用匿名卷,在这种情况下,我不确定 docker-compose up是否会知道在哪里可以找到您的数据。首选命名卷。您的数据很可能没有存储在一个卷中,这很危险,并且使您无法更新容器:
    $ docker-compose down --help

    By default, the only things removed are:

    - Containers for services defined in the Compose file
    - Networks defined in the `networks` section of the Compose file
    - The default network, if one is used

    Networks and volumes defined as `external` are never removed.

    Usage: down [options]

    Options:
    --rmi type Remove images. Type must be one of:
    'all': Remove all images used by any service.
    'local': Remove only images that don't have a custom tag
    set by the `image` field.
    -v, --volumes Remove named volumes declared in the `volumes` section
    of the Compose file and anonymous volumes
    attached to containers.
    --remove-orphans Remove containers for services not defined in the
    Compose file

    关于docker - 当外部链接的容器实际上正在使用docker-compose运行时,如何避免 “Docker cannot link to a non running container”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669052/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com