Difference between links and depends_on in docker_compose.yml

前端 未结 3 1095
-上瘾入骨i
-上瘾入骨i 2020-12-04 05:01

According to the Docker Compose\'s compose-file documentation:

  • depends_on - Express dependency between services.
  • links - Lin
3条回答
  •  囚心锁ツ
    2020-12-04 05:36

    [Update Sep 2016]: This answer was intended for docker compose file v1 (as shown by the sample compose file below). For v2, see the other answer by @Windsooon.

    [Original answer]:

    It is pretty clear in the documentation. depends_on decides the dependency and the order of container creation and links not only does these, but also

    Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

    For example, assuming the following docker-compose.yml file:

    web:
      image: example/my_web_app:latest
      links:
        - db
        - cache
    
    db:
      image: postgres:latest
    
    cache:
      image: redis:latest
    

    With links, code inside web will be able to access the database using db:5432, assuming port 5432 is exposed in the db image. If depends_on were used, this wouldn't be possible, but the startup order of the containers would be correct.

提交回复
热议问题