根据Docker 文档,链接被描述为“遗留”。特别是,有这个警告:
警告:--link 标志是 Docker 的弃用旧功能。它最终可能会被删除。除非您绝对需要继续使用它,否则我们建议您使用用户定义网络来促进两个容器之间的通信,而不是使用 --link。用户定义网络不支持的一项功能是您可以使用 --link 在容器之间共享环境变量。但是,您可以使用其他机制(例如卷)以更受控制的方式在容器之间共享环境变量。
相反,Docker Compose 也有一个“链接”功能,它不是根据已弃用Docker Compose 文档。
Docker 的“旧式容器链接”功能与 Docker Compose 的“链接”部分之间是否存在任何关系?如果存在,为什么它们在一个功能中被弃用,而在另一个功能中却没有?如果没有,为什么它们都被称为“链接”?
(我最初在 Docker 论坛上问了这个问题,但没有得到回复)。
答案1
我发现这个问题是因为今天它看起来几乎像“links:”自 Docker 25.x 以来停止支持容器名称映射
但原因是我仍在使用docker-compose
,根据此页面,它不受支持:https://docs.docker.com/compose/migrate/
Compose V1 的最终版本 1.29.2 于 2021 年 5 月 10 日发布。自此以后,这些软件包未收到任何安全更新。使用时请自担风险。
docker compose
当我按照页面建议进行切换时,“链接:”容器名称映射照常工作:
services:
container1:
image: pingidentity/pingtoolkit
links:
- container2:db
command: ping db
container2:
image: pingidentity/pingtoolkit
command: sleep infinity
证明:
$ docker compose up
[+] Running 2/0
✔ Container repro-container2-1 Created 0.0s
✔ Container repro-container1-1 Created 0.0s
Attaching to container1-1, container2-1
container2-1 | ----- Starting hook: ./bootstrap.sh
container2-1 | ### Bootstrap
container2-1 | ### Using the default container user and group
container2-1 | ### Container user and group
container2-1 | ### user : ping (id: 9031)
container2-1 | ### group: root (id: 0)
container2-1 | ----- Starting hook: /opt/entrypoint.sh
container2-1 | Running command: sleep infinity
container1-1 | ----- Starting hook: ./bootstrap.sh
container1-1 | ### Bootstrap
container1-1 | ### Using the default container user and group
container1-1 | ### Container user and group
container1-1 | ### user : ping (id: 9031)
container1-1 | ### group: root (id: 0)
container1-1 | ----- Starting hook: /opt/entrypoint.sh
container1-1 | Running command: ping db
container1-1 | PING db (10.50.3.2): 56 data bytes
container1-1 | 64 bytes from 10.50.3.2: seq=0 ttl=42 time=0.111 ms
container1-1 | 64 bytes from 10.50.3.2: seq=1 ttl=42 time=0.313 ms
^CGracefully stopping... (press Ctrl+C again to force)
[+] Stopping 2/2
✔ Container repro-container1-1 Stopped 0.2s
✔ Container repro-container2-1 Stopped 0.2s
canceled
$ docker-compose up
Recreating repro-container2-1 ... done
Recreating repro-container1-1 ... done
Attaching to repro_container2_1, repro_container1_1
container1_1 | ----- Starting hook: ./bootstrap.sh
container2_1 | ----- Starting hook: ./bootstrap.sh
container1_1 | ### Bootstrap
container2_1 | ### Bootstrap
container1_1 | ### Using the default container user and group
container2_1 | ### Using the default container user and group
container2_1 | ### Container user and group
container2_1 | ### user : ping (id: 9031)
container2_1 | ### group: root (id: 0)
container1_1 | ### Container user and group
container1_1 | ### user : ping (id: 9031)
container1_1 | ### group: root (id: 0)
container2_1 | ----- Starting hook: /opt/entrypoint.sh
container1_1 | ----- Starting hook: /opt/entrypoint.sh
container2_1 | Running command: sleep infinity
container1_1 | Running command: ping db
container1_1 | ping: bad address 'db'
$ docker info |grep -A 1 compose
compose: Docker Compose (Docker Inc.)
Version: v2.24.2
$ docker-compose --version
docker-compose version 1.29.2, build unknown