问题描述
这是我对docker撰写的理解:
Here's my understanding of docker compose:
- 您可以缩放服务,一次将其运行在多个容器中。 li>
- 如果将服务A链接到服务B,服务A可以访问运行服务B的容器。
我的理解是否正确,如果是,如果有多个容器运行服务,链接在哪里连接?
Is my understanding correct, and if so, where does a link connect if there are multiple containers running the service?
推荐答案
首先,我将澄清,默认情况下,使用或不将容器与其他链接链接,所有容器都可以访问在同一主机中运行的其他容器(使用容器IP)。您可以使用docker守护程序中的 icc = true
标志来更改此行为。
First of all I would clarify that, by default, with or without linking a container with other, all container has visibility to other containers running in the same host (using the container IP). You can change this behavior using the icc=true
flag in docker daemon.
关于与docker的链接-compose,这些都是在创建具有链接的容器时生成的。我们来看一个例子。使用此docker-compose.yml
In respect of the links with docker-compose, these are generated when the container with the links are created. Let's see it with an example. Using this docker-compose.yml
web:
build: .
command: python app.py
ports:
- "5000:5000"
volumes:
- .:/code
links:
- redis
redis:
image: redis
运行 docker-compos up -d
web_1
容器与容器链接, redis_1 :
After running docker-compose up -d
the web_1
container is linked with the container with redis_1
:
(...)
"Links": [
"/compose_redis_1:/compose_web_1/compose_redis_1",
"/compose_redis_1:/compose_web_1/redis",
"/compose_redis_1:/compose_web_1/redis_1"
], (...)
现在我们要使用来缩放
。运行它(并创建一个新的容器 redis
docker-compose scale redis = 2 redis_2
)后, web_1
中的链接保持不变。
Now we want to scale the redis
service using docker-compose scale redis=2
. After running it (and create a new container redis_2
), the links in web_1
keeps unchanged.
(...)
"Links": [
"/compose_redis_1:/compose_web_1/compose_redis_1",
"/compose_redis_1:/compose_web_1/redis",
"/compose_redis_1:/compose_web_1/redis_1"
], (...)
需要停止,删除并运行 web_1
才能看到创建的链接: p>
It is needed to stop, remove and run web_1
to see these links created:
docker-compose stop web
docker-compose rm web
docker-compose run -d web
docker inspect compose_web_run_2
(...) "Links": [
"/compose_redis_1:/compose_web_run_2/compose_redis_1",
"/compose_redis_2:/compose_web_run_2/compose_redis_2",
"/compose_redis_1:/compose_web_run_2/redis",
"/compose_redis_1:/compose_web_run_2/redis_1",
"/compose_redis_2:/compose_web_run_2/redis_2"
],(...)
而 / etc / hosts
web_1
container:
And the /etc/hosts
of web_1
container:
172.17.0.24 7be2dabea910
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.21 compose_redis_1 8a1297a5b3e4
172.17.0.23 compose_redis_2 069dd46836aa
172.17.0.21 redis 8a1297a5b3e4 compose_redis_1
172.17.0.21 redis_1 8a1297a5b3e4 compose_redis_1
172.17.0.23 redis_2 069dd46836aa compose_redis_2
所以要生成新的链接,您需要停止,删除和再次运行容器。
这篇关于在码头组合中,链接和缩放方式如何协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!