Docker-composenetworking链接

Docker的“链接”function将被弃用,因为新function“networking”已经发布( 链接 )。 我正在用一些容器做docker-compose,用'link'连接彼此(没有任何其他的命令)就没问题了。

由于我需要将链路configuration改为networking,所以我必须在“docker-up up”之前build立dockernetworking。 有没有任何dockernetworking自动构build的function? 或者任何其他方式来连接每个容器与一些configuration?

默认情况下,docker – 与V2的YML将组成一个networking为您的项目。 你所定义的任何networking也将被创build,除非你明确地告诉它否则。 以下是一个示例docker-compose.yml:

version: '2' networks: dbnet: appnet: services: db: image: busybox command: tail -f /dev/null networks: - dbnet app: image: busybox command: tail -f /dev/null networks: - dbnet - appnet proxy: image: busybox command: tail -f /dev/null ports: - 80 networks: - appnet 

然后,当你旋转起来,你会看到它创build的networking定义:

 $ docker-compose up -d Creating network "test_dbnet" with the default driver Creating network "test_appnet" with the default driver Creating test_app_1 Creating test_db_1 Creating test_proxy_1 

请注意,链接容器也创build了一个隐式的依赖关系,所以你可能想要在你的yml中使用depends_on在删除链接之后在任何依赖关系中显式化。

docker-compose为自己的撰写项目创build一个默认networking。 您只需将撰写项目迁移到构成yaml格式的version: '2'version: '3' 。 请阅读如何升级以获取更多信息。

对于版本2和版本3,您不必再指定链接,因为如果不明确指定其他networking,所有服务都将位于默认networking中。

更新:要使2个容器相互通信,可以简单地使用将parsing为容器IP的服务名称。 现在链接现在只需要,如果由于某种原因容器需要一个特定的名字,例如,因为它是硬编码。