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.
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.
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.