如何使用Dockerfile将主容器链接到db容器? [英] How to use Dockerfile to link main container to a db container?
问题描述
Docker提供了一种非常便捷的方法,可以使用以下命令将主容器链接到db容器:
Docker has a quite convenient way to link the main container to a db container with a command like:
docker run --link db:db user/main
这已经非常方便了.但是,我认为与以下命令相比,它仍然很笨拙:
This is very convenient already. However, I believe it's still clumsy compared to a command like:
docker run user/ultra
其中 ultra
是一个已经将主容器链接到db容器的容器.
where ultra
is a container that is already linking the main container to the db container.
有可能我可以通过编写一个好的 Dockerfile
来实现这一目标.
Is that possible that I can achieve this by writing a good Dockerfile
.
我想我可以用
FROM user/main
但是如何使第二个容器参与其中,然后将其与 Dockerfile
链接?
but how do I get the second container involved and then link them with Dockerfile
?
谢谢.
推荐答案
FROM user/main
这将基于 user/main
创建一个映像( build time
),这与在运行时将两个容器链接在一起完全不同.
That would create an image (build time
) based on user/main
, which is not at all the same as linking at runtime two containers together.
此外,-link
现在已过时:请参见"旧版容器链接"
Plus, --link
is now obsolete: see "Legacy container links"
警告:
-link
标志是Docker不推荐使用的旧功能.它最终可能会被删除.
除非您绝对需要继续使用它,否则建议您使用用户定义的网络来促进两个容器之间的通信,而不要使用-link
Warning: The
--link
flag is a deprecated legacy feature of Docker. It may eventually be removed.
Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using--link
使用 文件中的链接指令 .
Use instead the links
directive in a docker-compose.yml
file.
可以使用与别名相同的主机名访问链接服务的容器,如果未指定别名,则可以访问服务名称
Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified
您可以使用 depends_on
指令.
You can make sure the containers are launched in the proper order with the depends_on
directive.
然后,一个简单的 docker-compose up
将启动容器并将它们链接到同一网络上.
Then a simple docker-compose up
will launch the containers and link them on the same network.
这篇关于如何使用Dockerfile将主容器链接到db容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!