简体   繁体   English

当外部链接的容器实际上正在使用docker-compose运行时,如何避免“ Docker无法链接到未运行的容器”错误

[英]How to avoid the “Docker cannot link to a non running container” error when the external-linked container is actually running using docker-compose

What we want to do: 我们要做什么:

We want to use docker-compose to link one already running container (A) to another container (B) by container name . 我们想使用docker-compose 通过容器名称将一个已经在运行的容器(A)链接到另一个容器(B)。 We use " external-link " as both containers are started from different docker-compose.yml files. 我们使用“ external-link ”,因为两个容器都是从不同的docker-compose.yml文件启动的。

Problem: 问题:

Container B fails to start with the error although a container with that name is running. 尽管正在运行具有该名称的容器,但是容器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

output of "docker ps" : “ 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

Sample code: 样例代码:

docker-compose.yml of Container B : 容器B的docker-compose.yml

container_b:
  external_links:
  - container_a_1

What differs this question from the other "how to fix"-questions: 此问题与其他“如何解决”问题有什么不同:

  • we can't use "sudo service docker restart" (which works) as this is a production environment 我们不能使用“ sudo服务docker restart”(有效),因为这是生产环境
  • We don't want to fix this every time manually but find the reason so that we can 我们不想每次都手动解决此问题但是找到原因以便我们可以
    • understand what we are doing wrong 了解我们在做什么错
    • understand how to avoid this 了解如何避免这种情况

Assumptions: 假设:

  • It seems like two instances of the container_a exist (RUNNING_ID and PREVIOUSLY_LINKED_ID) 似乎存在container_a的两个实例(RUNNING_ID和PREVIOUSLY_LINKED_ID)
  • This might happen because we 这可能是因为我们
    • rebuilt the container via docker-compose build and 通过docker-compose build重建容器,并
    • changed the forwarded external port of the container (808 0 1 :8080) 更改了容器的转发外部端口(808 0 1 :8080)

Comment 评论

  • Do not use docker-compose down as suggested in the comments, this removes volumnes! 不要按照注释中的建议使用docker-compose down ,这样可以消除体积!

Docker links are deprecated so unless you need some functionality they provide or are on an extremely old version of docker, I'd recommend switching to docker networks. 不建议使用Docker链接,因此,除非您需要它们提供的某些功能,或者在极老版本的Docker上使用 ,否则建议您切换到Docker网络。

Since the containers you want to connect appear to be started in separate compose files, you would create that network externally: 由于您要连接的容器似乎是在单独的撰写文件中启动的,因此您可以在外部创建该网络:

docker network create app_net

Then in your docker-compose.yml files, you connect your containers to that network: 然后在docker-compose.yml文件中,将容器连接到该网络:

version: '3'

networks:
  app_net:
    external:
      name: app_net

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

Then in your container_b, you would connect to container_a as "container_a", not "container_a_1". 然后,在container_b中,您将以“ container_a”而不是“ container_a_1”连接到container_a。

As an aside, docker-compose down is not documented to remove volumes unless you pass the -v flag. docker-compose down除非您传递-v标志,否则不会记录docker-compose down来删除卷。 Perhaps you are using anonymous volumes, in which case I'm not sure that docker-compose up would know where to find your data. 也许您正在使用匿名卷,在这种情况下,我不确定docker-compose up是否知道在哪里可以找到您的数据。 A named volume is preferred. 首选命名卷。 More than likely, your data was not being stored in a volume, which is dangerous and removes your ability to update your containers: 您的数据很可能没有存储在一个卷中,这很危险,并且使您无法更新容器:

$ 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

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

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