docker_容器间的链接 link
使用link就是类似使用了容器的ip地址,无需知道其真实ip,使用link后的别名就可以知道
使用link连接容器
以 mysql 为 栗子
- 启动一个 mysql-server,详细参数可以参照说明文档:https://hub.docker.com/r/mysql/mysql-server/,设置密码,挂载数据库 等等
1 | wilker@ubuntu:~$ docker pull mysql/mysql-server # 拉一个官方的镜像下来 |
进入 mysql_server11 容器中修改一下 root 用户可以访问的ip地址,默认是 localhost,别的ip是连不进来的,这里改成 任意 ip 都可以连进来(生产环境不能这样干)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44wilker@ubuntu:~$ docker exec -it mysql_server11 mysql -uroot -p123456 # 进入容器的mysql服务
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 MySQL Community Server (GPL)
show databases; # 看下默认的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
use mysql; # 使用 mysql 数据库
Database changed
select host,user from user where user='root';
+-----------+------+
| host | user |
+-----------+------+
| localhost | root | # 默认只有 当前容器能用 root 用户连进来,要修改一下
+-----------+------+
1 row in set (0.00 sec)
update user set host = '%' where user ='root'; # 修改成 % 表示任意ip
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
select host,user from user where user='root'; # 查看一下修改成功没
+------+------+
| host | user |
+------+------+
| % | root |
+------+------+
1 row in set (0.00 sec)
flush privileges; # 刷新权限使之生效
Query OK, 0 rows affected (0.01 sec)
exit; # 退出 mysql,然后自动退出容器
Bye
wilker@ubuntu:~$去 docker hub 拉一个客户端下来,使用 link 连接 mysql_server11 容器,并使用别名 为 db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23wilker@ubuntu:~$ docker pull imega/mysql-client # 拉 mysql 客户端镜像
wilker@ubuntu:~$ docker run --name=mysql_client11 --link=mysql_server11:db -i -t imega/mysql-client mysql -h db -uroot -p123456 # -h 的值为 db,就是 mysql_server11 的ip别名
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases; # 成功进入 mysql_server11 容器中的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
MySQL [(none)]> exit;
Bye退出客户端容器是容器也就变为 Exit 状态了,再次进入可以用 start 这个容器
1
2
3
4
5
6
7
8
9
10wilker@ubuntu:~$ docker start mysql_client11 -a
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
查看容器连接后的环境变量
还是用上面运行的 mysql 栗子
mysql_server11 的环境变量
1
2
3
4
5
6wilker@ubuntu:~$ docker exec mysql_server11 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=e3edaf102b4a
MYSQL_ROOT_PASSWORD=123456
PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.18-1.el7.x86_64.rpm
HOME=/rootmysql_client11 的环境变量,这里可以看到 mysql_server11 容器的环境变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16wilker@ubuntu:~$ docker exec mysql_client11 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=db696da76b9c
DB_PORT=tcp://172.17.0.2:3306
DB_PORT_3306_TCP=tcp://172.17.0.2:3306
DB_PORT_3306_TCP_ADDR=172.17.0.2
DB_PORT_3306_TCP_PORT=3306
DB_PORT_3306_TCP_PROTO=tcp
DB_PORT_33060_TCP=tcp://172.17.0.2:33060
DB_PORT_33060_TCP_ADDR=172.17.0.2
DB_PORT_33060_TCP_PORT=33060
DB_PORT_33060_TCP_PROTO=tcp
DB_NAME=/mysql_client11/db
DB_ENV_MYSQL_ROOT_PASSWORD=123456
DB_ENV_PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-5.7.18-1.el7.x86_64.rpm
HOME=/root再看一下 hosts 文件, db 可以解析为 172.17.0.2,
1
2
3
4
5
6
7
8
9wilker@ubuntu:~$ docker exec mysql_client11 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 db e3edaf102b4a mysql_server11
172.17.0.3 db696da76b9c