在同一个宿主机、同一网段下,容器间ping IP可通。

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS
c7ffadc83291        centos_with_httpd:genesis   "bash"              4 hours ago         Up 4 hours
7169e8be6d3e        centos                      "/bin/bash"         6 hours ago         Up 6 hours
4cd696928bbe        centos                      "bash"              6 hours ago         Up 6 hours
4f5bf6f33f2c        centos                      "bash"              6 hours ago         Up 6 hours
0a80861145c9        centos                      "bash"              6 hours ago         Up 6 hours
fb45150dbc21        centos                      "bash"              6 hours ago         Up 6 hours
3222c7c5c456        centos                      "bash"              7 hours ago         Up 7 hours
e136b27a8e17        centos                      "bash"              7 hours ago         Up 7 hours
[root@localhost ~]# docker exec -it c7ffa bash
[root@c7ffadc83291 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.9  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:9  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:09  txqueuelen 0  (Ethernet)
        RX packets 26  bytes 2103 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1255 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 20  bytes 7036 (6.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20  bytes 7036 (6.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@c7ffadc83291 /]# exit
exit
[root@localhost ~]# docker exec -it 7169 bash
[root@7169e8be6d3e /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.8  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:8  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:08  txqueuelen 0  (Ethernet)
        RX packets 10103  bytes 41156618 (39.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8762  bytes 562975 (549.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@7169e8be6d3e /]# ping 172.17.0.9
PING 172.17.0.9 (172.17.0.9) 56(84) bytes of data.
64 bytes from 172.17.0.9: icmp_seq=1 ttl=64 time=1.58 ms
64 bytes from 172.17.0.9: icmp_seq=2 ttl=64 time=0.058 ms
^C
--- 172.17.0.9 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.058/0.820/1.583/0.763 ms
[root@7169e8be6d3e /]# curl 172.17.0.9/1.html
ginesis2011.github.io

使用mysql镜像

下载一个mysql镜像

[root@localhost ~]# docker pull mysql

新建一个容器

[root@localhost ~]# docker run -itd -p 13306:3306 mysql bash
4611d806d8d01cb5a8c2976d8bfbf511a9cc23a7307c723ecfbdd2ebcb25b8ef
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                     NAMES
4611d806d8d0        mysql               "docker-entrypoint.s   54 seconds ago      Up 52 seconds       0.0.0.0:13306->3306/tcp   condescending_nobel

再新建一个web容器并和前一个容器互联

[root@localhost ~]# docker run -itd -p 12308:80 --name web --link condescending_nobel:db centos_with_httpd:genesis bash
3d3248766159a31546093d42912a748fc616808910223a36a20c55b3a15ea1cc
  • 使用--name标记可以为容器自定义命名
  • 在执行docker run时,如果添加--rm标记,则容器在终止后会立刻删除。注意,--rm-d参数不能同时使用。
  • 使用--link参数可以让容器之间安全的进行交互
  • --link参数的格式为--link name:alias,其中name是要连接容器的名称,alias是这个连接eider别名。
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                     NAMES
3d3248766159        centos_with_httpd:genesis   "bash"                 9 minutes ago       Up 9 minutes        0.0.0.0:12308->80/tcp     web
4611d806d8d0        mysql                       "docker-entrypoint.s   12 minutes ago      Up 12 minutes       0.0.0.0:13306->3306/tcp   condescending_nobel

Docker在两个互联的容器之间创建了一个安全隧道,而且不用映射它们的端口到宿主主机上,避免暴露数据库端口到外部网络上。

在web容器中用ping命令测试跟db容器的连通

[root@localhost ~]# docker exec -it web bash
[root@3d3248766159 /]# ping db
PING db (172.17.0.2) 56(84) bytes of data.
64 bytes from db (172.17.0.2): icmp_seq=1 ttl=64 time=1.11 ms
64 bytes from db (172.17.0.2): icmp_seq=2 ttl=64 time=0.090 ms
64 bytes from db (172.17.0.2): icmp_seq=3 ttl=64 time=0.048 ms
^C
--- db ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.048/0.416/1.110/0.491 ms

Docker通过两种方式为容器公开连接信息:

  1. 环境变量
  2. 更新/etc/hosts文件
[root@3d3248766159 /]# cat /etc/hosts
172.17.0.3      3d3248766159
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 4611d806d8d0 condescending_nobel

这里有两个hosts信息,第一个是web容器,web容器用自己的id作为默认主机名;第二个是db容器的IP和主机名。

[root@3d3248766159 /]# telnet db 3306
bash: telnet: command not found
[root@3d3248766159 /]# yum install -y telnet
[root@3d3248766159 /]# telnet db 3306
Trying 172.17.0.2...
telnet: connect to address 172.17.0.2: Connection refused
[root@3d3248766159 /]# exit
exit
[root@localhost ~]# docker exec -it condescending_nobel bash
root@4611d806d8d0:/# /etc/init.d/mysql start
root@4611d806d8d0:/# exit
exit
[root@localhost ~]# docker exec -it web bash
[root@3d3248766159 /]# telnet db 3306
Trying 172.17.0.2...
Connected to db.
Escape character is '^]'.
CHost '172.17.0.3' is not allowed to connect to this MySQL serverConnection closed by foreign host.

使用env命令查看web容器的环境变量

[root@3d3248766159 /]# env
HOSTNAME=3d3248766159
DB_NAME=/web/db
TERM=xterm
DB_PORT=tcp://172.17.0.2:3306
DB_PORT_3306_TCP_PORT=3306
DB_ENV_GOSU_VERSION=1.7
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
DB_PORT_3306_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
DB_PORT_3306_TCP_ADDR=172.17.0.2
SHLVL=1
HOME=/root
DB_PORT_3306_TCP=tcp://172.17.0.2:3306
DB_ENV_MYSQL_VERSION=5.7.16-1debian8
DB_ENV_MYSQL_MAJOR=5.7
_=/usr/bin/env

使用centos 6镜像

使用OpenVZ提供的模板(centos-6-x86-minimal.tar.gz)创建的镜像,报错仍未解决

[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos_with_httpd            genesis             50611dba3a22        About an hour ago   335.3 MB
registry                     latest              5c929a8b587a        34 hours ago        33.27 MB
genesis_centos               latest              85bc3a58f134        5 days ago          277.6 MB
192.168.1.179:5000/busybox   latest              9967c5ad88de        12 days ago         1.093 MB
busybox                      latest              9967c5ad88de        12 days ago         1.093 MB
centos-6-x86                 latest              8fca9486a39b        2 weeks ago         341.3 MB
centos_with_net              latest              3e8ea8607f08        4 weeks ago         294.9 MB
centos                       latest              9baab0af79c4        6 weeks ago         196.7 MB
[root@localhost ~]# docker run -itd centos-6-x86 bash
06e48529b37a743ee6a03102b37f37f5724749324115bd034085d3a70f9f7132
[root@localhost ~]# docker exec -it 06e4 bash
[root@06e48529b37a /]# yum install -y mysql-server

操作到这里出错,centos-6-x86是使用OpenVZ提供的模板(centos-6-x86-minimal.tar.gz)创建的镜像。
报错

使用OpenVZ提供的模板(centos-6-x86_64.tar.gz)创建的镜像

重新在openvz官网下载另一个centos6模板(centos-6-x86_64.tar.gz)做成镜像

[root@localhost ~]# wget http://download.openvz.org/template/precreated/centos-6-x86_64.tar.gz
2016-10-21 20:56:01 (18.9 KB/s) - 已保存 “centos-6-x86_64.tar.gz” [230555815/230555815])
[root@localhost ~]# ls
anaconda-ks.cfg         centos-6-x86-minimal.tar.gz  genesis_centos.tar  install.log.syslog  模板  图片  下载  桌面
centos-6-x86_64.tar.gz  centos_with_net.tar          install.log         公共的              视频  文档  音乐
[root@localhost ~]# du -sh centos-6-x86_64.tar.gz
220M    centos-6-x86_64.tar.gz
[root@localhost ~]# cat centos-6-x86_64.tar.gz |docker import - centos-6-x86_64
78555e13daef8eb0d8f34cb39979ced687cd80ccb6491c5262795231f9410d3c
[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos-6-x86_64              latest              78555e13daef        2 minutes ago       615.8 MB
centos_with_httpd            genesis             50611dba3a22        28 hours ago        335.3 MB
registry                     latest              5c929a8b587a        2 days ago          33.27 MB
mysql                        latest              5536fdb1bd72        6 days ago          383.4 MB
genesis_centos               latest              85bc3a58f134        7 days ago          277.6 MB
192.168.1.179:5000/busybox   latest              9967c5ad88de        13 days ago         1.093 MB
busybox                      latest              9967c5ad88de        13 days ago         1.093 MB
centos-6-x86                 latest              8fca9486a39b        2 weeks ago         341.3 MB
centos_with_net              latest              3e8ea8607f08        5 weeks ago         294.9 MB
centos                       latest              9baab0af79c4        6 weeks ago         196.7 MB
[root@localhost ~]# docker run -itd centos-6-x86_64 bash
4dfbe855582616a2dbfb51275be7ab66dcd6afb04774cf503c2783952a7176ed 
[root@localhost ~]# docker exec -it 4df bash
[root@4dfbe8555826 /]# yum list |grep mysql
apr-util-mysql.x86_64                      1.3.9-3.el6_0.1             base
bacula-director-mysql.x86_64               5.0.0-13.el6                base
bacula-storage-mysql.x86_64                5.0.0-13.el6                base
dovecot-mysql.x86_64                       1:2.0.9-22.el6              base
freeradius-mysql.x86_64                    2.2.6-6.el6_7               base
libdbi-dbd-mysql.x86_64                    0.8.3-5.1.el6               base
mod_auth_mysql.x86_64                      1:3.0.0-11.el6_0.1          base
mysql.x86_64                               5.1.73-7.el6                base
mysql-bench.x86_64                         5.1.73-7.el6                base
mysql-connector-java.noarch                1:5.1.17-6.el6              base
mysql-connector-odbc.x86_64                5.1.5r1144-7.el6            base
mysql-devel.i686                           5.1.73-7.el6                base
mysql-devel.x86_64                         5.1.73-7.el6                base
mysql-embedded.i686                        5.1.73-7.el6                base
mysql-embedded.x86_64                      5.1.73-7.el6                base
mysql-embedded-devel.i686                  5.1.73-7.el6                base
mysql-embedded-devel.x86_64                5.1.73-7.el6                base
mysql-libs.i686                            5.1.73-7.el6                base
mysql-libs.x86_64                          5.1.73-7.el6                base
mysql-server.x86_64                        5.1.73-7.el6                base
mysql-test.x86_64                          5.1.73-7.el6                base
pcp-pmda-mysql.x86_64                      3.10.9-6.el6                base
php-mysql.x86_64                           5.3.3-48.el6_8              updates
qt-mysql.i686                              1:4.6.2-28.el6_5            base
qt-mysql.x86_64                            1:4.6.2-28.el6_5            base
rsyslog-mysql.x86_64                       5.8.10-10.el6_6             base
rsyslog7-mysql.x86_64                      7.4.10-5.el6                base
[root@4dfbe8555826 /]# yum install -y mysql-server
[root@4dfbe8555826 /]# /etc/init.d/mysqld start
[root@4dfbe8555826 /]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      -
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     59005  -                   /var/lib/mysql/mysql.sock
[root@4dfbe8555826 /]# exit
exit 
[root@localhost ~]# docker commit -m "centos_6_with_mysql" 4df centos6_with_mysql
821a69bf88525484ad718468adc65b84719d9b8d952c4da7c41d913a767056e1
[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos6_with_mysql           latest              821a69bf8852        26 seconds ago      724.8 MB
centos-6-x86_64              latest              78555e13daef        57 minutes ago      615.8 MB
centos_with_httpd            genesis             50611dba3a22        29 hours ago        335.3 MB
registry                     latest              5c929a8b587a        2 days ago          33.27 MB
mysql                        latest              5536fdb1bd72        6 days ago          383.4 MB
genesis_centos               latest              85bc3a58f134        7 days ago          277.6 MB
192.168.1.179:5000/busybox   latest              9967c5ad88de        13 days ago         1.093 MB
busybox                      latest              9967c5ad88de        13 days ago         1.093 MB
centos-6-x86                 latest              8fca9486a39b        2 weeks ago         341.3 MB
centos_with_net              latest              3e8ea8607f08        5 weeks ago         294.9 MB
centos                       latest              9baab0af79c4        6 weeks ago         196.7 MB

为了接着用13306、12308端口,先停掉之前用于容器互联的两个容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                     NAMES
4dfbe8555826        centos-6-x86_64             "bash"                 16 minutes ago      Up 16 minutes                                 sleepy_pare
3d3248766159        centos_with_httpd:genesis   "bash"                 4 hours ago         Up 4 hours          0.0.0.0:12308->80/tcp     web
4611d806d8d0        mysql                       "docker-entrypoint.s   4 hours ago         Up 4 hours          0.0.0.0:13306->3306/tcp   condescending_nobel
[root@localhost ~]# docker stop 3d3
3d3
[root@localhost ~]# docker stop 461
461
[root@localhost ~]# docker run -itd -p 13306:3306 --name db centos6_with_mysql bash
18010636c53584fd1f300a459c1bd03bbb2cbc4111acd72a48c8b65655d334c9
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                     NAMES
18010636c535        centos6_with_mysql   "bash"              2 seconds ago       Up 2 seconds        0.0.0.0:13306->3306/tcp   db             
4dfbe8555826        centos-6-x86_64      "bash"              21 minutes ago      Up 21 minutes                                 sleepy_        pare
[root@localhost ~]# docker run -itd -p 18080:80 --name web --link db:db centos_with_httpd:genesis bash
1dcac96e9649c0e96797a01d17cce7cd7952987570bd3a822812ee1c3f8e78e5
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS                     NAMES
1dcac96e9649        centos_with_httpd:genesis   "bash"              5 seconds ago       Up 4 seconds        0.0.0.0:18080->80/tcp     web     
18010636c535        centos6_with_mysql          "bash"              3 minutes ago       Up 3 minutes        0.0.0.0:13306->3306/tcp   db      
4dfbe8555826        centos-6-x86_64             "bash"              24 minutes ago      Up 24 minutes                                 sleepy_pare
[root@localhost ~]# docker exec -it web bash
[root@1dcac96e9649 /]# ping db
PING db (172.17.0.7) 56(84) bytes of data.
64 bytes from db (172.17.0.7): icmp_seq=1 ttl=64 time=0.628 ms
64 bytes from db (172.17.0.7): icmp_seq=2 ttl=64 time=0.112 ms
^C
--- db ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.112/0.370/0.628/0.258 ms
[root@1dcac96e9649 /]# cat /etc/hosts
172.17.0.8      1dcac96e9649
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.7      db 18010636c535
[root@1dcac96e9649 /]# telnet db 3306
bash: telnet: command not found
[root@1dcac96e9649 /]# yum install -y telnet
[root@1dcac96e9649 /]# telnet db 3306
Trying 172.17.0.7...
telnet: connect to address 172.17.0.7: Connection refused
[root@1dcac96e9649 /]# exit
exit
[root@localhost ~]# docker exec -it db bash
[root@18010636c535 /]# /etc/init.d/mysqld start
Starting mysqld:                                           [  OK  ]
[root@18010636c535 /]# exit
[root@localhost ~]# docker exec -it web bash
[root@1dcac96e9649 /]# telnet db 3306
Trying 172.17.0.7...
Connected to db.
Escape character is '^]'.
CHost '172.17.0.8' is not allowed to connect to this MySQL serverConnection closed by foreign host.
[root@1dcac96e9649 /]# env
HOSTNAME=1dcac96e9649
DB_NAME=/web/db
TERM=xterm
DB_PORT=tcp://172.17.0.7:3306
DB_PORT_3306_TCP_PORT=3306
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
DB_PORT_3306_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
DB_PORT_3306_TCP_ADDR=172.17.0.7
SHLVL=1
HOME=/root
DB_PORT_3306_TCP=tcp://172.17.0.7:3306
_=/usr/bin/env

使用centos 7镜像,报错D-Bus,仍未解决

改用docker pull centos从官方下载的centos镜像(centos 7.2)

[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos_with_httpd            genesis             50611dba3a22        21 hours ago        335.3 MB
registry                     latest              5c929a8b587a        2 days ago          33.27 MB
mysql                        latest              5536fdb1bd72        6 days ago          383.4 MB
genesis_centos               latest              85bc3a58f134        6 days ago          277.6 MB
192.168.1.179:5000/busybox   latest              9967c5ad88de        13 days ago         1.093 MB
busybox                      latest              9967c5ad88de        13 days ago         1.093 MB
centos-6-x86                 latest              8fca9486a39b        2 weeks ago         341.3 MB
centos_with_net              latest              3e8ea8607f08        5 weeks ago         294.9 MB
centos                       latest              9baab0af79c4        6 weeks ago         196.7 MB
[root@localhost ~]# docker run -itd centos bash
092e8e97db0e7f5f49a52f5a7a3c9340706cfe0f3a6e92a98e2f2b64425871b0
[root@localhost ~]# docker exec -it 092e bash
[root@092e8e97db0e /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

安装MySQL

[root@092e8e97db0e /]# yum list |grep mysql
akonadi-mysql.x86_64                     1.9.2-4.el7                    base
apr-util-mysql.x86_64                    1.5.2-6.el7                    base
dovecot-mysql.x86_64                     1:2.2.10-5.el7                 base
freeradius-mysql.x86_64                  3.0.4-6.el7                    base
libdbi-dbd-mysql.x86_64                  0.8.3-16.el7                   base
mysql-connector-java.noarch              1:5.1.25-3.el7                 base
mysql-connector-odbc.x86_64              5.2.5-6.el7                    base
pcp-pmda-mysql.x86_64                    3.10.6-2.el7                   base
php-mysql.x86_64                         5.4.16-36.3.el7_2              updates
php-mysqlnd.x86_64                       5.4.16-36.3.el7_2              updates
qt-mysql.i686                            1:4.8.5-12.el7_2               updates
qt-mysql.x86_64                          1:4.8.5-12.el7_2               updates
redland-mysql.x86_64                     1.0.16-6.el7                   base
rsyslog-mysql.x86_64                     7.4.7-12.el7                   base

可以看到,并没有mysql-server。CentOS 7版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。

[root@092e8e97db0e /]# yum list |grep mariadb
mariadb.x86_64                           1:5.5.50-1.el7_2               updates
mariadb-bench.x86_64                     1:5.5.50-1.el7_2               updates
mariadb-devel.i686                       1:5.5.50-1.el7_2               updates
mariadb-devel.x86_64                     1:5.5.50-1.el7_2               updates
mariadb-embedded.i686                    1:5.5.50-1.el7_2               updates
mariadb-embedded.x86_64                  1:5.5.50-1.el7_2               updates
mariadb-embedded-devel.i686              1:5.5.50-1.el7_2               updates
mariadb-embedded-devel.x86_64            1:5.5.50-1.el7_2               updates
mariadb-libs.i686                        1:5.5.50-1.el7_2               updates
mariadb-libs.x86_64                      1:5.5.50-1.el7_2               updates
mariadb-server.x86_64                    1:5.5.50-1.el7_2               updates
mariadb-test.x86_64                      1:5.5.50-1.el7_2               updates

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

[root@092e8e97db0e /]# yum install -y mariadb-server

启动数据库(失败

[root@092e8e97db0e /]# systemctl start mariadb
Failed to get D-Bus connection: Operation not permitted

这个的原因是因为dbus-daemon没能启动。在网上用搜到的各种方式尝试,均无效……

  • docker run --privileged -ti -e "container=docker" -v /sys/fs/cgroup:/sys/fs/cgroup centos /usr/sbin/init
  • docker run --privileged -itd centos /usr/sbin/init
  • systemctl restart mariadb.service
  • 关闭容器内的firewalld,systemctl disable firwalld
  • 关闭宿主机selinux

    cat /etc/selinux/config
    SELINUX=disabled
    

下面为其中一种的具体情况,其他的最后结果均类似

[root@localhost ~]# docker run --privileged -itd centos /usr/sbin/init
d044d42b88c6e68bad032908bff522d0fd35d1a0603924e331dc2a60d9c62558
[root@localhost ~]# docker exec -it d044 bash
[root@d044d42b88c6 /]# yum install -y mariadb-server
Failed to get D-Bus connection: No such file or directory