link连接容器
当我们把每个单独的容器串起来就是我们的整个环境了
连接前两篇文章生成的mysql和redis容器
➜ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db6d62ec29e0 redis:3.2.8 "docker-entrypoint..." 5 hours ago Up 5 hours 0.0.0.0:7001->6379/tcp redis3.2.8
e41193fe394a mysql:5.7 "docker-entrypoint..." 7 hours ago Up 7 hours 0.0.0.0:3307->3306/tcp mysql5.7
1.在被连接的redis容器中添加key作为后续验证依据
➜ docker exec -ti db6d62ec29e0 /bin/bash
root@db6d62ec29e0:/data# redis-cli
127.0.0.1:6379> set a1 aaa
OK
127.0.0.1:6379> get a1
"aaa"
2.在test_link容器中连接mysql和redis容器,–link=[容器names]:[新容器中的别名],此处测试例子为–link=redis3.2.8:redis
➜ docker run --name=test_link --link=mysql5.7:db --link=redis3.2.8:redis -d -t -i redis:3.2.8 42f2bf262df322430f3f2a18e6284268cd5d3fcffcefe6557fbd4ccbf70ee46e
3.进入test_link容器,并作验证。(同样方法测试mysql)
➜ docker exec -ti 42f2bf262df3 /bin/bash
root@42f2bf262df3:/data# redis-cli -h redis
redis:6379> get a1
"aaa"