59

I'm learning docker. I see those two terms that make me confused. For example here is a docker-compose that defined two services redis and web-app.

services:
  redis:
    container_name: redis
    image: redis:latest
    ports:
      - "6379:6379"
    networks:
      - lognet

  app:
    container_name: web-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - ".:/webapp"
    links:
      - redis
    networks:
      - lognet

networks:
  lognet:
    driver: bridge

This docker-compose file defines a bridge network named lognet and all services will connect to this network. As I understand, this action makes those services see others. So why app service still needs to link to redis service in the above case?

Thanks

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107

1 Answers1

90

Links have been replaced by networks. Docker describes them as a legacy feature that you should avoid using. You can safely remove the link and the two containers will be able to refer to each other by their service name (or container_name).

With compose, links do have a side effect of creating an implied dependency. You should replace this with a more explicit depends_on section so that the app doesn't attempt to run without or before redis starts.

As an aside, I'm not a fan of hard coding container_name unless you are certain that this is the only container that will exist with that name on the host and you need to refer to it from the docker cli by name. Without the container name, docker-compose will give it a less intuitive name, but it will also give it an alias of redis on the network, which is exactly what you need for container to container networking. So the end result with these suggestions is:

version: '2'
# do not forget the version line, this file syntax is invalid without it

services:
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    networks:
      - lognet

  app:
    container_name: web-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - ".:/webapp"
    depends_on:
      - redis
    networks:
      - lognet

networks:
  lognet:
    driver: bridge
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    I think @Trân was asking about `links` in docker-compose which aren't deprecated. Only in `docker run` commands. https://forums.docker.com/t/are-links-deprecated-in-docker-compose/27348/2 – Ethan Davis Oct 28 '17 at 08:00
  • 9
    They are deprecated, I've verified this directly from the docker compose developer previously. The documentation shows that they aren't supported at all with swarm mode, and to make them work with docker compose you need a common network which would provide DNS support anyway. https://docs.docker.com/compose/compose-file/#links – BMitch Oct 28 '17 at 10:12
  • So here, `depends_on` only refers to precedence? – chenghuayang Jul 24 '21 at 07:11
  • 1
    @chenghuayang yes, I've updated the doc link. – BMitch Jul 24 '21 at 11:58