Docker-compose network link

2019-07-24 08:43发布

Docker 'link' feature will be deprecated as new feature 'networking' has been released (link). I'm making docker-compose with some containers, and it was fine with 'link' to connect each others(without any other commands).

Since I need to change link configuration to network, I have to make docker network before 'docker-compose up'. Is there any docker-compose feature that making docker network automatically? Or any other way to connecting each containers with some configuration?

标签: docker
2条回答
对你真心纯属浪费
2楼-- · 2019-07-24 09:09

By default, docker-compose with a v2 yml will spin up a network for your project. Any networks you define will also be created unless you explicitly tell it otherwise. Here's an example 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

And then when you spin it up, you'll see that it creates the networks defined:

$ 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

Note that linking containers also created an implicit dependency, so you may want to use depends_on in your yml to be explicit in any dependencies after removing your link.

查看更多
姐就是有狂的资本
3楼-- · 2019-07-24 09:24

docker-compose creates a default network for your compose project on itself. You only have to migrate your compose projects to version: '2' or version: '3' of the compose yaml format. Please read how to upgrade for more information.

With version 2 and 3, you don't have to specify links anymore, as all services will be in the default network if you don't explicitly specify other networks.

UPDATE: To make 2 containers talk to each other, you can simply use the service names which will resolve to container IPs. Links are now only required if for some reason a container expects a specific name, e.g. because it is hardcoded.

查看更多
登录 后发表回答