Docker link is actually labeled dec, but there are still some traditional applications using it
The most typical is linking web applications and databases
For example, you can start a WordPress
docker run --name wp_mysql -p 3306:3306 -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 -d mysql:5.7
docker run --name wp_web --link wp_mysql:mysql -v /data/wordpress:/var/www/html -p 80:80 -p 443:443 -d wordpress:5.6.0
WordPress docker deployment using – link can directly connect to the database without configuring MySQL database connection.
Here I want to discuss what link has done
First of all, we need to clarify two concepts: MySQL is the source container and the connected container, WordPress is the target container and the receiving container
For the next test, we start two busybox containers and connect them together
docker run -i -t --name source -p 3001:3001 -p 3002:3002 -e source_env_1=123 -e source_env_2=456 busybox sleep infinity
docker run -i -t --name target -p 3003:3003 -p 3004:3004 -e target_env_1=123 -e target_env_2=456 --link source:mysource busybox sleep infinity
- alias
–link wp_mysql:mysql
For WordPress containers, WP_ MySQL and MySQL are a container with the same containerid
–link source:mysource
For the target container, source and mysource are one container
2. DNS
3. environment variable
The reason why WordPress can directly connect to the database above is that it actually uses environment variables
The image of WordPress will read the database configuration from the alias MySQL by default, so if it is written as –link wp_mysql:test_mysql and WordPress can’t recognize the database, which can be understood as a convention
The target container gets the following environment variables
4. If the IP of the source container changes
After deleting the source container, I create an nginx container, which takes up all the resources of the original source container
172.17.0.2 IP you can see that the new IP is 172.17.0.4
At this point, no content is updated in the target container
Start a target2 container, and you can see that DNS and env are replaced with new ones