[英]How can I make sure to always connect to the same docker container?
I have a little problem with really understanding this. 我在真正理解这一点上有一点问题。 When I start 3 containers in Docker they get 3 different IP-Addresses, that are assigned sequentially, I can address them from other containers and everything works fine.
当我在Docker中启动3个容器时,它们会获得3个不同的IP地址,这些IP地址是按顺序分配的,我可以从其他容器中寻址它们,并且一切正常。
But when I start a different container before those 3, the 3 get different IP-Addresses and the entire setup goes to hell. 但是,当我在这3个容器之前启动另一个容器时,这3个容器获得了不同的IP地址,整个设置陷入了困境。
I have been pestering Google for quite a while now, but I don't seem to find a proper (and not too complicated) solution to have one steady way to connect to a certain container. 我已经困扰Google了很长时间了,但是我似乎没有找到合适的(并且不太复杂)解决方案来拥有一种稳定的方法来连接到某个容器。 It's not that I want to assign the IP myself, just to tell the docker host something like "every time container X starts, assign the IP 172.17.0.X to it"
我并不是要自己分配IP,只是要告诉Docker主机类似“每次启动容器X时,为其分配IP 172.17.0.X”的信息。
There must be some way in which the order in which I start up the containers doesnt matter, it can't be that I have to change my settings every other time I restart containers. 必须以某种方式启动容器的顺序无关紧要,这不一定是每隔一次重新启动容器就必须更改设置的情况。
So some help here would be nice? 所以这里的一些帮助会很好吗?
Some way in which I can assign IPs to a container or give it some sort of alias that I can address from another container. 我可以通过某种方式将IP分配给一个容器,或者给它提供某种别名,我可以从另一个容器中解决该别名。
I think that you should use hostname
instead of IP Address in your Docker containers. 我认为您应该在Docker容器中使用
hostname
而不是IP地址。 And when you run Docker containers, you are able to use --link
between the container. 而且,当您运行Docker容器时,您可以在容器之间使用
--link
。 See https://docs.docker.com/v1.8/userguide/dockerlinks/#communication-across-links 请参阅https://docs.docker.com/v1.8/userguide/dockerlinks/#communication-across-links
In the new version, It is easy to connect the containers together in same network. 在新版本中,将容器在同一网络中连接在一起很容易。 See https://docs.docker.com/engine/userguide/networking/dockernetworks/
请参阅https://docs.docker.com/engine/userguide/networking/dockernetworks/
Connect from container to another container through ip it is not the "docker-way", so the really proper way is using of --link
as posted Nguyen Sy Thanh Son . 通过ip从容器连接到另一个容器不是“ docker-way”,因此真正正确的方法是使用
--link
如Nguyen Sy Thanh Son所示 。
As example: 例如:
docker run --name=db image1 docker run --name=app --link outernamedb:internalname image2
and then inside of app
container use internalname:3306
docker run --name=db image1 docker run --name=app --link outernamedb:internalname image2
然后在app
容器internalname:3306
使用internalname:3306
But it will not work if you try to start app
before
db
, for that case there is kind a legal hack for docker higher than 1.11. 但是,如果您尝试在
db
之前启动
app
,则将无法正常工作,在这种情况下,对于docker高于1.11的Docker会有某种合法的破解方式。
From docker official docks you can use --ip
flag to set exact ip for container docker run --name=db --ip 172.17.0.90 image1 docker run --name=app --ip 172.17.0.91 image2
and then inside of app
container use 172.17.0.90:3306
在Docker官方码头上,您可以使用
--ip
标志为容器docker run --name=db --ip 172.17.0.90 image1 docker run --name=app --ip 172.17.0.91 image2
然后在app
内部设置确切的ip容器使用172.17.0.90:3306
Use Docker Networks . 使用Docker网络 。 Links are being deprecated and the IP address stuff is a craziness :)
链接已被弃用,而IP地址的东西是疯狂的:)
$ docker network create my-net
$ docker run -d --net my-net --name container1 image1
$ docker run -d --net my-net --name container2 image2
$ docker exec -it container1 sh
# ping container2
PING container2 (172.21.0.3): 56 data bytes
64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.073 ms
...
you can find some information on this post 你可以在这篇文章中找到一些信息
How do I set up a DNS container inside docker-compose? 如何在docker-compose中设置DNS容器?
i hope this helped ;) 我希望这有所帮助;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.