• 209

为什么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 

网卡信息

    [root@Optimus /]# 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.访问成功!

[root@Optimus /]# 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

clipboard.png

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

原问题

补充信息

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

[root@Optimus 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)
[root@Optimus wordpress]# 

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

[root@Optimus ~]# 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)
[root@Optimus ~]# 

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

查看数据库用户信息如下

查看数据库用户

MariaDB [(none)]> select * from mysql.user;
Host User Password Select_priv Insert_priv Update_priv Delete_priv Create_priv Drop_priv Reload_priv Shutdown_priv Process_priv File_priv Grant_priv References_priv Index_priv Alter_priv Show_db_priv Super_priv Create_tmp_table_priv Lock_tables_priv Execute_priv Repl_slave_priv Repl_client_priv Create_view_priv Show_view_priv Create_routine_priv Alter_routine_priv Create_user_priv Event_priv Trigger_priv Create_tablespace_priv ssl_type ssl_cipher x509_issuer x509_subject max_questions max_updates max_connections max_user_connections plugin authentication_string
localhost root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
9ad32fb968fe root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
127.0.0.1 root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
::1 root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
localhost N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
9ad32fb968fe N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
% admin *318BF8C339F1F04B052E3D1ED22C2B5873740C5A Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
localhost admin *318BF8C339F1F04B052E3D1ED22C2B5873740C5A N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
127.0.0.1 admin *318BF8C339F1F04B052E3D1ED22C2B5873740C5A N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0

构建镜像过程

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

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

[root@Optimus ~]# 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 
[root@Optimus ~]# 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容器

[root@Optimus /]# 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状态,启动成功

[root@Optimus /]# 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端口本地已经被占用了。

访问应用

访问页面的时候,出错。

clipboard.png

查看容器状态

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

[root@Optimus /]# 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镜像的详细过程
阅读 5.6k
评论
    2 个回答
    docker logs 容器id

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

      相似问题
      推荐文章