3

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's actually what i want. Thanks :)
4

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.

2 Comments

Then how to connect container to container? I need to connect db container to tomcat container. I will use 'links' in docker-compose if it will not be deprecated.
I updated the answer to also answer the question from your comment. In this case, tomcat can simply use "db" (assuming the service is named db)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.