天天看點

docker_容器間的連結 link

使用link就是類似使用了容器的ip位址,無需知道其真實ip,使用link後的别名就可以知道

使用link連接配接容器

以 mysql 為 栗子
  1. 啟動一個 mysql-server,詳細參數可以參照說明文檔:https://hub.docker.com/r/mysql/mysql-server/,設定密碼,挂載資料庫 等等
    wilker@ubuntu:~$ docker pull mysql/mysql-server # 拉一個官方的鏡像下來
    wilker@ubuntu:~$ docker run --name=mysql_server11 -e MYSQL_ROOT_PASSWORD= -d mysql/mysql-server # 啟動容器,并設定 root 使用者密碼
               
  2. 進入 mysql_server11 容器中修改一下 root 使用者可以通路的ip位址,預設是 localhost,别的ip是連不進來的,這裡改成 任意 ip 都可以連進來(生産環境不能這樣幹)
    [email protected]:~$ 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)
    
    mysql> show databases; # 看下預設的資料庫
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    
    mysql> use mysql; # 使用 mysql 資料庫
    Database changed
    mysql> select host,user from user where user='root';
    +-----------+------+
    | host      | user |
    +-----------+------+
    | localhost | root | # 預設隻有 目前容器能用 root 使用者連進來,要修改一下
    +-----------+------+
    1 row in set (0.00 sec)
    
    mysql> update user set host = '%'  where user ='root'; # 修改成 % 表示任意ip
    Query OK, 1 row affected (0.03 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select host,user from user where user='root'; # 檢視一下修改成功沒
    +------+------+
    | host | user |
    +------+------+
    | %    | root |
    +------+------+
    1 row in set (0.00 sec)
    
    mysql> flush privileges; # 重新整理權限使之生效
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> exit; # 退出 mysql,然後自動退出容器
    Bye
    [email protected]:~$
               
  3. 去 docker hub 拉一個用戶端下來,使用 link 連接配接 mysql_server11 容器,并使用别名 為 db
    [email protected]:~$ docker pull imega/mysql-client # 拉 mysql 用戶端鏡像
    [email protected]:~$ 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 
    Server version:  MySQL Community Server (GPL)
    
    Copyright (c) , , 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                |
    +--------------------+
     rows in set ( sec)
    
    MySQL [(none)]> exit;
    Bye
               
    • 退出用戶端容器是容器也就變為 Exit 狀态了,再次進入可以用 start 這個容器
      [email protected]:~$ docker start mysql_client11 -a 
      Welcome to the MariaDB monitor.  Commands end with ; or \g.
      Your MySQL connection id is 
      Server version:  MySQL Community Server (GPL)
      
      Copyright (c) , , 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 的環境變量
    wilker@ubuntu:~$ docker exec mysql_server11 env
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    HOSTNAME=e3edaf102b4a
    MYSQL_ROOT_PASSWORD=
    PACKAGE_URL=https://repo.mysql.com/yum/mysql-5.7-community/docker/x86_64/mysql-community-server-minimal-.-.el7.x86_64.rpm
    HOME=/root
               
  • mysql_client11 的環境變量,這裡可以看到 mysql_server11 容器的環境變量
    [email protected]:~$ 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,
    [email protected]:~$ 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
               

參考資料

  • http://blog.tingyun.com/web/article/detail/892
  • http://www.jianshu.com/p/13752117ff97
  • https://www.oschina.net/translate/dockerlinks
  • mysql 的相關操作:http://www.cnblogs.com/flying607/p/4766041.html

繼續閱讀