docker-compose.yml中如果不对网络进行任何设置,默认会:
- 以docker-compose.yml文件所在的目录名为前缀创建名为"目录名_default"的网络,比如目录名为test,则网络名为test_default,通过"docker network ls"命令查看所有已创建网络;
- 将docker-compose.yml中的容器添加至该网络中,通过"docker network inspect 网络名"命令查看该网络设置;
最终效果就是在任意容器中均能通过服务名或容器名或ip地址访问另外容器
下图中定义了ubuntu和mongodb服务,并没有对网络进行任何设置,up后,进入ubuntu容器中,分别ping服务名、容器名、ip地址,均正常
根据docker compose文档:
Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified.
Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.
在同一个docker compose中的不同服务均默认可以通过服务名称进行访问
且官方建议不要继续使用links参数,未来将被废弃。
参考:https://docs.docker.com/compose/compose-file/