According to the Docker Compose's compose-file documentation:
depends_on
- Express dependency between services.links
- Link to containers in another service and also express dependency between services in the same way as depends_on.
I don't understand the purpose of linking to other containers so the difference between two options still seems quite difficult for me.
It would be much easier if there is an example, but I can't find any.
I noticed, when I link container B with container A then container B will be "pingable" inside container A's shell.
I ran ping B
inside container A's bash
and got result like this (just for reference, image from the Internet)
This answer is for docker-compose version 2 and it also works on version 3
You can still access the data when you use depends_on.
If you look at docker docs Docker Compose and Django, you still can access the database like this:
What is the difference between links and depends_on?
links:
When you create a container for a database, for example:
And you may find
This means you can connect the database from your localhost port 32777 (3306 in container) but this port will change every time you restart or remove the container. So you can use links to make sure you will always connect to the database and don't have to know which port it is.
depends_on:
I found a nice blog from Giorgio Ferraris Docker-compose.yml: from V1 to V2
And
Update
depends_on
Express dependency between services, which has two effects:
Simple example:
[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 andlinks
not only does these, but alsoFor example, assuming the following
docker-compose.yml
file:With
links
, code insideweb
will be able to access the database usingdb:5432
, assuming port 5432 is exposed in thedb
image. Ifdepends_on
were used, this wouldn't be possible, but the startup order of the containers would be correct.