Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose: difference between networks and links

I'm learning docker. I see those two terms make me confuse. 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 can see others. So why app service still need to link to redis service in above case.

Thanks

like image 412
Trần Kim Dự Avatar asked Dec 23 '16 02:12

Trần Kim Dự


People also ask

What does links mean in Docker compose?

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.

What is the use of network in Docker compose?

Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.

Is Docker compose links deprecated?

Warning: The --link flag is a deprecated legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.


1 Answers

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 
like image 142
BMitch Avatar answered Oct 11 '22 09:10

BMitch