Labs docker networking - why do i link mysql-db to itself?

the question is from labs ‘docker networking’, step 8/9:

Deploy a web application named webapp using the kodekloud/simple-webapp-mysql image. Expose the port to 38080 on the host.

The application makes use of two environment variable:
1: DB_Host with the value mysql-db.
2: DB_Password with the value db_pass123.
Make sure to attach it to the newly created network called wp-mysql-network.

Also make sure to link the MySQL and the webapp container.
from prev step 7/9 i have mysql-db container running.

the solution:

docker run --network=wp-mysql-network -e DB_Host=mysql-db -e DB_Password=db_pass123 -p 38080:8080 --name webapp --link mysql-db:mysql-db -d kodekloud/simple-webapp-mysql

why do i do “–link mysql-db:mysql-db” ? why link to itself? the question is asking to link mysql-db and current webapp, no?

You are not linking to itself, you are creating an entry in the hosts file. The --link mysql-db:mysql-db part is creating an alias for the mysql-db container within the webapp container’s /etc/hosts file. The format for the --link flag is --link name:alias , where name is the name of the container you want to link to (in this case, mysql-db ), and alias is the alias for that link inside the container being created.

Hi @Erjan-G,

You are right for asking this question as the instruction saying " Also make sure to link the MySQL and the webapp container." actually doesn’t need to be followed :

As the network “wp-mysql-network” is user-created (by you), it has an internal DNS Server that containers inside the network use to DNS resolve other containers’ hostname. Therefore, you don’t need to explicitely link containers between them with the --link flag.

Docker’s default bridge network doesn’t include an internal DNS Server, so containers inside it need to be linked in order to communicate with each other using their names.