1

我们想做的事:

我们想使用docker-compose通过容器名称将一个已经运行的容器(A)链接到另一个容器(B)。我们使用“ external-link ”,因为两个容器都是从不同的 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 service docker restart”(有效),因为这是一个生产环境
  • 我们不想每次都手动解决这个问题,但要找到原因,以便我们可以
    • 了解我们做错了什么
    • 了解如何避免这种情况

假设:

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

评论

  • 不要docker-compose down按照评论中的建议使用,这会删除体积!
4

1 回答 1

1

Docker 链接已被弃用,因此除非您需要它们提供的某些功能或在非常旧的 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”,而不是“container_a_1”。

顺便说一句,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
于 2017-10-20T12:47:01.573 回答