无法 link 到由 docker-compose 启动的 运行 容器
Cannot link to a running container started by docker-compose
我正在使用 docker 容器设置我的本地开发环境。 docker-compose.yml 如下
version: '2'
services:
db:
image: mongo:3
mq:
image: rabbitmq:3
api:
build: .
image: my_app/api
ports:
- "3000:3000"
links:
- db
- mq
environment:
- NODE_ENV=development
启动无误。
docker 列出了 3 运行 个容器
docker-compose up -d
docker ps
e90e5a8b5d33 my_app/api "/usr/local/bin/node " 0.0.0.0:3000->3000/tcp my_app_api_1
42bfcd971b16 mongo:3 "/entrypoint.sh mongo" 27017/tcp my_app_db_1
a0685a816c47 rabbitmq:3 "/docker-entrypoint.s" 4369/tcp, 5671-5672/tcp, 25672/tcp my_app_mq_1
然而,当我尝试从另一个容器link到那些运行容器时
docker run --link my_app_mq_1:mq --link my_app_db_1:db -it worker
我收到错误
docker: Error response from daemon: Cannot link to /my_app_mq_1, as it does not belong to the default network.
我也试过了
docker run --link my_app_mq_1:mq --link my_app_db_1:db -it --net default worker
同样的错误。
那么我如何 link 到由 docker-compose 启动的 运行 容器?
好的,找到答案了。如果其他人遇到同样的问题,请执行
docker network ls
此命令列出所有 docker 网络。 docker-compose 将在您 运行 docker-compose 时创建一个新网络。在我的例子中,网络被命名为 myapp_default.
Note: Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable. Networking in Compose
所以link到这些容器的正确方法是
docker run --link my_app_mq_1:mq --link my_app_db_1:db -it --net myapp_default worker
当您使用服务定义版本 2 或更高版本时,docker-compose
会创建用户定义的网络。用户定义网络中的名称解析通过 Docker 嵌入式 DNS 服务器工作。这是来自 documentation 的相关引述:
The Docker embedded DNS server enables name resolution for containers connected to a given [user-defined] network. This means that any connected container can ping another container on the same network by its container name.
容器也可以通过 docker-compose
创建的网络别名使用。可以通过如下命令验证:
docker inspect \
-f '{{json .NetworkSettings.Networks.myapp_default.Aliases}}' my_app_db_1
它打印 ["db","$CONTAINER_ID"]
.
提供链接 --link
对现有用户定义的网络没有任何影响。您可以确定并查看/etc/hosts
,其中不会有相应的行。
因此下面的命令就足够了:
docker run -it --net myapp_default worker
我对 运行 链接的命令
docker run --network=YOUR_NETWORK --link SERVICE -t -i app command
我正在使用 docker 容器设置我的本地开发环境。 docker-compose.yml 如下
version: '2'
services:
db:
image: mongo:3
mq:
image: rabbitmq:3
api:
build: .
image: my_app/api
ports:
- "3000:3000"
links:
- db
- mq
environment:
- NODE_ENV=development
启动无误。 docker 列出了 3 运行 个容器
docker-compose up -d
docker ps
e90e5a8b5d33 my_app/api "/usr/local/bin/node " 0.0.0.0:3000->3000/tcp my_app_api_1
42bfcd971b16 mongo:3 "/entrypoint.sh mongo" 27017/tcp my_app_db_1
a0685a816c47 rabbitmq:3 "/docker-entrypoint.s" 4369/tcp, 5671-5672/tcp, 25672/tcp my_app_mq_1
然而,当我尝试从另一个容器link到那些运行容器时
docker run --link my_app_mq_1:mq --link my_app_db_1:db -it worker
我收到错误
docker: Error response from daemon: Cannot link to /my_app_mq_1, as it does not belong to the default network.
我也试过了
docker run --link my_app_mq_1:mq --link my_app_db_1:db -it --net default worker
同样的错误。
那么我如何 link 到由 docker-compose 启动的 运行 容器?
好的,找到答案了。如果其他人遇到同样的问题,请执行
docker network ls
此命令列出所有 docker 网络。 docker-compose 将在您 运行 docker-compose 时创建一个新网络。在我的例子中,网络被命名为 myapp_default.
Note: Your app’s network is given a name based on the “project name”, which is based on the name of the directory it lives in. You can override the project name with either the --project-name flag or the COMPOSE_PROJECT_NAME environment variable. Networking in Compose
所以link到这些容器的正确方法是
docker run --link my_app_mq_1:mq --link my_app_db_1:db -it --net myapp_default worker
当您使用服务定义版本 2 或更高版本时,docker-compose
会创建用户定义的网络。用户定义网络中的名称解析通过 Docker 嵌入式 DNS 服务器工作。这是来自 documentation 的相关引述:
The Docker embedded DNS server enables name resolution for containers connected to a given [user-defined] network. This means that any connected container can ping another container on the same network by its container name.
容器也可以通过 docker-compose
创建的网络别名使用。可以通过如下命令验证:
docker inspect \
-f '{{json .NetworkSettings.Networks.myapp_default.Aliases}}' my_app_db_1
它打印 ["db","$CONTAINER_ID"]
.
提供链接 --link
对现有用户定义的网络没有任何影响。您可以确定并查看/etc/hosts
,其中不会有相应的行。
因此下面的命令就足够了:
docker run -it --net myapp_default worker
我对 运行 链接的命令
docker run --network=YOUR_NETWORK --link SERVICE -t -i app command