【Docker】为什么docker容器启动成功几分钟后又自己关闭了?

总结

问题描述

通过docker run启动一个 WordPress 应用的 docker 容器时,显示启动成功为UP状态,但无法访问
WordPress 应用,并且几分钟后 container 的状态变为excited

启动命令

docker run -d -p 9090:80 --name wordpress -e WORDPRESS_DB_HOST=10.10.4.182 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2

网卡信息

    [[email protected] /]# ifconfig

docker0 Link encap:Ethernet HWaddr 02:C4:0F:42:9D:A4

inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0

inet6 addr: fe80::ac31:7eff:fe21:61bb/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:99200 errors:0 dropped:0 overruns:0 frame:0

TX packets:149257 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:5452754 (5.2 MiB) TX bytes:222793983 (212.4 MiB)

eth0 Link encap:Ethernet HWaddr 00:1E:67:4E:3D:7B

UP BROADCAST MULTICAST MTU:1500 Metric:1

RX packets:0 errors:0 dropped:0 overruns:0 frame:0

TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

Memory:d0960000-d097ffff

eth1 Link encap:Ethernet HWaddr 00:1E:67:4E:3D:7C

inet addr:10.10.4.182 Bcast:10.10.4.255 Mask:255.255.255.0

inet6 addr: fe80::21e:67ff:fe4e:3d7c/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:225323670 errors:0 dropped:0 overruns:0 frame:0

TX packets:78415571 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:42781224797 (39.8 GiB) TX bytes:107201976978 (99.8 GiB)

Memory:d0940000-d095ffff

背景补充

构建DB容器时,使用的端口映射为3307:3306.

解决方法

改为使用docker0网卡地址172.17.42.1,指定3307端口号。网页访问地址为eth1:9090,也就是10.10.4.182:9090.访问成功!

[[email protected] /]# docker run -d -p 9090:80 --name wordpress -e WORDPRESS_DB_HOST=172.17.42.1:3307 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2

6472364a37f5db3cee78608a649b0a9772952343914212fa8125faa0bcdc4152

【Docker】为什么docker容器启动成功几分钟后又自己关闭了?

感谢@CrazyCodes和@Feng_Yu两位的帮助!

原问题

补充信息

出错信息1(第一次docker run的日志显示的是admin'@'172.17.0.22

[[email protected] wordpress]# docker logs -f  wordpress

PHP Notice: Undefined offset: 1 in - on line 4

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.22' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.22' (using password: YES)

[[email protected] wordpress]#

错误信息2(删除容器,再重新docker run,显示的错误信息变成了admin'@'172.17.0.25)

[[email protected] ~]# docker logs -f  wordpress

PHP Notice: Undefined offset: 1 in - on line 4

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Notice: Undefined offset: 1 in - on line 4

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'admin'@'172.17.0.25' (using password: YES) in - on line 7

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.25' (using password: YES)

[[email protected] ~]#

改用docker0网卡的IP地址172.17.42.1仍然不行。

查看数据库用户信息如下

查看数据库用户

MariaDB [(none)]> select * from mysql.user;

HostUserPasswordSelect_privInsert_privUpdate_privDelete_privCreate_privDrop_privReload_privShutdown_privProcess_privFile_privGrant_privReferences_privIndex_privAlter_privShow_db_privSuper_privCreate_tmp_table_privLock_tables_privExecute_privRepl_slave_privRepl_client_privCreate_view_privShow_view_privCreate_routine_privAlter_routine_privCreate_user_privEvent_privTrigger_privCreate_tablespace_privssl_typessl_cipherx509_issuerx509_subjectmax_questionsmax_updatesmax_connectionsmax_user_connectionspluginauthentication_string
localhostrootYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0000
9ad32fb968ferootYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0000
127.0.0.1rootYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0000
::1rootYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0000
localhostNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0000
9ad32fb968feNNNNNNNNNNNNNNNNNNNNNNNNNNNNN0000
%admin*318BF8C339F1F04B052E3D1ED22C2B5873740C5AYYYYYYYYYYYYYYYYYYYYYYYYYYYYY0000
localhostadmin*318BF8C339F1F04B052E3D1ED22C2B5873740C5ANNNNNNNNNNNNNNNNNNNNNNNNNNNNN0000
127.0.0.1admin*318BF8C339F1F04B052E3D1ED22C2B5873740C5ANNNNNNNNNNNNNNNNNNNNNNNNNNNNN0000


构建镜像过程

构建过程中的几个小问题:

  1. 使用的WORDPRESS_DB_HOST地址是eth1网卡的地址。原本应该用eth0网卡的地址,但是ifconfig eth0显示没有IP地址。我就拿eth1网卡代替了。

[[email protected] ~]# ifconfig eth0

eth0 Link encap:Ethernet HWaddr 00:1E:67:4E:3D:7B

UP BROADCAST MULTICAST MTU:1500 Metric:1

RX packets:0 errors:0 dropped:0 overruns:0 frame:0

TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

Memory:d0960000-d097ffff

[[email protected] ~]# ifconfig eth1

eth1 Link encap:Ethernet HWaddr 00:1E:67:4E:3D:7C

inet addr:10.10.4.182 Bcast:10.10.4.255 Mask:255.255.255.0

inet6 addr: fe80::21e:67ff:fe4e:3d7c/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:225279050 errors:0 dropped:0 overruns:0 frame:0

TX packets:78413400 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:42774258179 (39.8 GiB) TX bytes:107200782231 (99.8 GiB)

Memory:d0940000-d095ffff

2. 构建DB容器 docker run的时候,使用的是3307:3306做端口映射。本应该用3306:3306作为端口映射,但是由于3306端口被占用,所以我自己用了3307做了替换。


启动docker容器

[[email protected] /]# docker run -d -p 9090:80 --name wordpress -e WORDPRESS_DB_HOST=10.10.4.182 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2

a6aae74c483a9d662594695dc420d183bdb0ad52fdc844783456d40a22e5114a

这里启动一个WordPress的docker容器,端口映射为9090,因为80端口已经占用了。

容器名为WordPress,宿主机地址是eth1的网卡地址10.10.4.182。(这里原本要用内网网卡eth0的IP地址,但是ifconfig eth0 显示没有IP地址,我换成了eth1的IP地址)

三个环境变量WORDPRESS_DB_HOSTWORDPRESS_DB_USERWORDPRESS_DB_PASSWORD在脚本init.sh中定义。

使用的是csphere/wordpress:4.2 这个镜像。

查看docker容器

查看docker容器状态,是UP状态,启动成功

[[email protected] /]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

a6aae74c483a csphere/wordpress:4.2 "/init.sh /usr/bin/s 5 seconds ago Up 4 seconds 22/tcp, 443/tcp, 0.0.0.0:9090->80/tcp wordpress

8e266cd27a63 csphere/mysql:5.5 "/scripts/start" 23 hours ago Up 23 hours 22/tcp, 0.0.0.0:3307->3306/tcp newdb

e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 6 days ago Up 6 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website

2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 7 days ago Up 7 days 0.0.0.0:2222->22/tcp base

0d1f6225836b centos "/bin/bash" 8 days ago Exited (127) 8 days ago naughty_engelbart

说明:这里使用的DB容器端口映射使用的3307端口,因为3306端口本地已经被占用了。

访问应用

访问页面的时候,出错。

【Docker】为什么docker容器启动成功几分钟后又自己关闭了?

查看容器状态

此时,再次查看容器,已经关闭了,状态为excited

[[email protected] /]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

a6aae74c483a csphere/wordpress:4.2 "/init.sh /usr/bin/s About a minute ago Exited (1) About a minute ago wordpress

8e266cd27a63 csphere/mysql:5.5 "/scripts/start" 23 hours ago Up 23 hours 22/tcp, 0.0.0.0:3307->3306/tcp newdb

e274049db704 csphere/php-fpm:5.4 "/usr/bin/supervisor 6 days ago Up 6 days 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website

2d36dbfc72a0 csphere/centos:7.1 "/usr/bin/supervisor 7 days ago Up 7 days 0.0.0.0:2222->22/tcp base

0d1f6225836b centos "/bin/bash" 8 days ago Exited (127) 8 days ago naughty_engelbart

删除WordPress容器,用同样的方法再重新启动,还是同样的问题:显示启动成功UP状态后,再次查看又变成excited状态,无法访问网页应用。

下面是一些操作过程中的步骤:

  1. 构建WordPress镜像的详细过程
  2. 构建DB镜像的详细过程
  3. 构建php-fpm镜像的详细过程

回答

docker logs 容器id

自己动手、丰衣足食,logs下把错误信息贴出来

看看log,肯定出错挂掉了了

以上是 【Docker】为什么docker容器启动成功几分钟后又自己关闭了? 的全部内容, 来源链接: utcz.com/a/74245.html

回到顶部