【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress

代码文件

[root@Optimus /]# cd docker-training/
[root@Optimus docker-training]# ls
centos7  mysql  php-fpm  README.md  wordpress
[root@Optimus docker-training]# cd wordpress/
[root@Optimus wordpress]# ls
Dockerfile  init.sh      readme.html      wp-admin            wp-comments-post.php  wp-content   wp-includes        wp-load.php   wp-mail.php      wp-signup.php     xmlrpc.php
index.php   license.txt  wp-activate.php  wp-blog-header.php  wp-config-sample.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
[root@Optimus wordpress]#

其中,除了dockerfile和init.sh,都是项目代码文件。推荐把dockerfile放在该目录下。

查看dockerfile

from csphere/php-fpm:5.4

add init.sh /init.sh

entrypoint ["/init.sh", "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]

代码文件已经了,只需要能解析运行PHP的环境和webserver的镜像。
在/php-fpm/Dockerfile文件中,最后两行代码是onbuild,会在构建WordPress的时候生效。添加所有文件到/app目录下。

ONBUILD ADD . /app
ONBUILD RUN chown -R nginx:nginx /app

文件目录如下

[root@Optimus wordpress]#  ls
Dockerfile  init.sh      readme.html      wp-admin            wp-comments-post.php  wp-content   wp-includes        wp-load.php   wp-mail.php      wp-signup.php     xmlrpc.php
index.php   license.txt  wp-activate.php  wp-blog-header.php  wp-config-sample.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
[root@Optimus wordpress]#

其中Dockerfile不必要添加到/app目录下,只要copy其他的代码文件即可。

查看当前目录下所有文件

[root@Optimus wordpress]# ls -a
.   Dockerfile     index.php  license.txt  wp-activate.php  wp-blog-header.php    wp-config-sample.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
..  .dockerignore  init.sh    readme.html  wp-admin         wp-comments-post.php  wp-content            wp-includes  wp-load.php        wp-mail.php   wp-signup.php    xmlrpc.php
[root@Optimus wordpress]#

.dockerignore文件内容

[root@Optimus wordpress]# cat .dockerignore 
Dockerfile

只有一句dockerfile,代表着除了dockerfile文件,其他文件都会copy到/app目录下。

WordPress的dockerfile

[root@Optimus wordpress]# cat Dockerfile 
from csphere/php-fpm:5.4

add init.sh /init.sh

entrypoint ["/init.sh", "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
[root@Optimus wordpress]#

from执行完毕后,就会执行onbuild去添加代码文件到/app,然后才会执行add命令。先添加文件,后执行init.sh脚本。

在执行WordPress entrypoint命令时,先执行init.sh脚本,再启动supervisor。这里启动supervisor,相当于启动了Nginx和php-fpm,因为在php-fpm这个镜像下,supervisor.conf.d目录下只有Nginx和php-fpm的启动文件。

entrypoint ["/init.sh", "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]

init.sh脚本

构建WordPress镜像
dockerfile不在当前目录下,构建镜像

[root@Optimus /]# docker build -t csphere/wordpress:4.2 ./docker-training/wordpress/
Sending build context to Docker daemon  19.8 MB
Sending build context to Docker daemon 
Step 0 : FROM csphere/php-fpm:5.4
# Executing 2 build triggers
Trigger 0, ADD . /app
Step 0 : ADD . /app
Trigger 1, RUN chown -R nginx:nginx /app
Step 0 : RUN chown -R nginx:nginx /app
          ---> Running in 5884fc6d3da5
          ---> cac915ee5a8f
Removing intermediate container dc55f73b6381
Removing intermediate container 5884fc6d3da5
Step 1 : ADD init.sh /init.sh
          ---> da14e3eab628
Removing intermediate container e03e077ee0ad
Step 2 : ENTRYPOINT /init.sh /usr/bin/supervisord -n -c /etc/supervisord.conf
          ---> Running in 87d28377c3ea
          ---> 6d90342cc99d
Removing intermediate container 87d28377c3ea
Successfully built 6d90342cc99d
[root@Optimus /]#

查看镜像

[root@Optimus /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
csphere/wordpress   4.2                 6d90342cc99d        About a minute ago   747.3 MB
csphere/mysql       5.5                 e147ac2a588e        3 days ago           752.7 MB
csphere/php-fpm     5.4                 1b08c94ce801        5 days ago           709.7 MB
csphere/centos      7.1                 fd1f7619e63e        6 days ago           613 MB
centos              centos7.1.1503      879c6d07c60e        6 weeks ago          212.1 MB
[root@Optimus /]#

已经有了DB、php-fpm,现在构建一个应用镜像,并连接DB container。

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
8e266cd27a63        csphere/mysql:5.5     "/scripts/start"       16 hours ago        Up 16 hours               22/tcp, 0.0.0.0:3307->3306/tcp          newdb               
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   5 days ago          Up 5 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   6 days ago          Up 6 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            7 days ago          Exited (127) 7 days ago                                           naughty_engelbart   
[root@Optimus /]#

查看当前主机的内网网卡的主机地址(服务器的内部IP地址)

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

(我的主机在eth0下没有IP地址,查看其它的网卡eth1)

[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:224369799 errors:0 dropped:0 overruns:0 frame:0
      TX packets:78309983 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:42464926703 (39.5 GiB)  TX bytes:107190159141 (99.8 GiB)
      Memory:d0940000-d095ffff

运行docker WordPress

docker run -d -p 80: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
-e 可以传入环境变量给container。在inut,sh文件中有三个参数需要传给container。
WORDPRESS_DB_HOST=连哪台数据库。
端口映射为3306端口不需要改。
-e可以使用多次。-p可以使用多次。-d不可以使用多次。
WORDPRESS_DB_USER=admin
WOREDPRESS_DB_PASSWORD=csphere2015
指定镜像 csphere/wordpress:4.2

绑定80端口出错。想重新端口映射,名字冲突。删除container,重新端口映射,8080和8000均不成功。最后用9090做端口映射。

[root@Optimus /]# docker run -d -p 80: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 
f0bf3b9ecdf026b06bd79cdcf364354eab8760a7d2ee5fdd5fb715f997a7a649
Error response from daemon: Cannot start container f0bf3b9ecdf026b06bd79cdcf364354eab8760a7d2ee5fdd5fb715f997a7a649: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use
[root@Optimus /]# docker run -d -p 8080: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 
Error response from daemon: Conflict. The name "wordpress" is already in use by container f0bf3b9ecdf0. You have to delete (or rename) that container to be able to reuse that name.
[root@Optimus /]# docker rm wordpress
wordpress
[root@Optimus /]# docker run -d -p 8080: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 
2a9942d2ce32aa673d14563378d18b53970ede65b6599cb9880fe6d86010cc77
Error response from daemon: Cannot start container 2a9942d2ce32aa673d14563378d18b53970ede65b6599cb9880fe6d86010cc77: Bind for 0.0.0.0:8080 failed: port is already allocated
[root@Optimus /]# docker rm wordpress
wordpress
[root@Optimus /]# docker run -d -p 8000: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 
85c51e130edc625c9c77040a8ba0c1f4dfcdbf419daec74f97cb3d9a385202f7
Error response from daemon: Cannot start container 85c51e130edc625c9c77040a8ba0c1f4dfcdbf419daec74f97cb3d9a385202f7: Error starting userland proxy: listen tcp 0.0.0.0:8000: bind: address already in use
[root@Optimus /]# docker rm wordpress
wordpress
[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 
18e21a1d127f1f99f12d67a67ee8a26391f992d7e93fbd2c78d45235c475e74a
[root@Optimus /]#

查看容器

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                CREATED             STATUS                     PORTS                                   NAMES
18e21a1d127f        csphere/wordpress:4.2   "/init.sh /usr/bin/s   5 minutes ago       Exited (1) 4 minutes 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

显示exited,没有启动成功。删除WordPress,重启,查看容器,启动成功。

[root@Optimus /]# docker rm wordpress
wordpress
[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
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   
[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 
fcf40517154261e3b7768272d1c856df63f5703455ccd229d22ba57ab9656163
[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                CREATED             STATUS                    PORTS                                   NAMES
fcf405171542        csphere/wordpress:4.2   "/init.sh /usr/bin/s   3 seconds ago       Up 2 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   
[root@Optimus /]#

使用80端口做映射,访问的时候可以不用加端口号访问。否则要加上端口号。
出错:成功启动过了几分钟,发现,又excited了。。

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                CREATED             STATUS                     PORTS                                   NAMES
fcf405171542        csphere/wordpress:4.2   "/init.sh /usr/bin/s   2 minutes ago       Exited (1) 2 minutes 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   
[root@Optimus /]#

访问失败
【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress

查看出错日志

[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]#

有人建议我,临时进入docker连接数据库,运行如下:

root@Optimus ~]# docker run -it wordpress bash
Unable to find image 'wordpress:latest' locally
latest: Pulling from wordpress

632d62e9ff45: Pulling fs layer 
2309d29c605a: Pull complete 
b5c24cf2be08: Pull complete 
4b0489c8c5ad: Pull complete 
0e18f8fbae80: Pull complete 
ae8ee28d33fd: Pull complete 
4e6bd1f11ffa: Pull complete 
b150aee2f63a: Pull complete 
bda04964adbb: Pull complete 
765636a32409: Pull complete 
9f39310817a4: Pull complete 
d5965880f205: Pull complete 
bc291240dd82: Pull complete 
ed3c4a2166ea: Pull complete 
7f9f8040b904: Pull complete 
30d2ca86be32: Pull complete 
9796545de9ab: Pull complete 
96f8917e11d8: Pull complete 
b3aebdc4c86d: Pull complete 
dfc5c74517f4: Pull complete 
b74ab8ff8171: Pull complete 
f8f5b923aa4a: Pull complete 
d336bb27db75: Pull complete 
76d91343f4f9: Pull complete 
640a07199820: Pull complete 
5fb5a73259de: Pull complete 
1ff113204b89: Pull complete 
f0d30083f45e: Pull complete 
95cf8f007a47: Pull complete 
e54ef1de6066: Pull complete 
4c985e73a00d: Pull complete 
2b1f410ae0f1: Pull complete 
f6e007144096: Pull complete 
86b573b7bc9b: Pull complete 
a1c0c746f816: Pull complete 
df87483bb3b4: Pull complete 
906569adf667: Pull complete 
5bdd010bcd76: Pull complete 
c9986e430552: Pull complete 
b027afd30886: Pull complete 
Digest: sha256:b277723a281b94d5d28058b346361704f84caa681ff45ccf89f68bf3ba96e788
Status: Downloaded newer image for wordpress:latest

root@de644e92ee52:/var/www/html#

shell命令没有指定镜像,系统找不到最新版本的WordPress镜像,pull了一个新的WordPress镜像,构建了容器,然后进入了这个容器。好像不对。
删除这个容器:

[root@Optimus ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                      PORTS                                   NAMES
de644e92ee52        wordpress             "docker-entrypoint.s   8 minutes ago       Exited (0) 37 seconds ago                                           mad_hodgkin         
8e266cd27a63        csphere/mysql:5.5     "/scripts/start"       45 hours ago        Up 45 hours                 22/tcp, 0.0.0.0:3307->3306/tcp          newdb               
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   7 days ago          Up 7 days                   22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   8 days ago          Up 8 days                   0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            9 days ago          Exited (127) 9 days ago                                             naughty_engelbart   
[root@Optimus ~]# docker rm mad_hodgkin
mad_hodgkin
[root@Optimus ~]#

查看数据库用户

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
localhostroot YYYYYYYYYYYYYYYYYYYYYYYYYYYYY 0000
9ad32fb968feroot YYYYYYYYYYYYYYYYYYYYYYYYYYYYY 0000
127.0.0.1root YYYYYYYYYYYYYYYYYYYYYYYYYYYYY 0000
::1root YYYYYYYYYYYYYYYYYYYYYYYYYYYYY 0000
localhost NNNNNNNNNNNNNNNNNNNNNNNNNNNNN 0000
9ad32fb968fe NNNNNNNNNNNNNNNNNNNNNNNNNNNNN 0000
%admin*318BF8C339F1F04B052E3D1ED22C2B5873740C5AYYYYYYYYYYYYYYYYYYYYYYYYYYYYY 0000
localhostadmin*318BF8C339F1F04B052E3D1ED22C2B5873740C5ANNNNNNNNNNNNNNNNNNNNNNNNNNNNN 0000
127.0.0.1admin*318BF8C339F1F04B052E3D1ED22C2B5873740C5ANNNNNNNNNNNNNNNNNNNNNNNNNNNNN 0000

网卡信息

[root@Optimus centos7]# 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:98676 errors:0 dropped:0 overruns:0 frame:0
      TX packets:148581 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:4576610 (4.3 MiB)  TX bytes:222704957 (212.3 MiB)

使用IP地址172.17.42.1构建容器

[root@Optimus /]# docker run -d -p 9090:80 --name wordpress -e WORDPRESS_DB_HOST=172.17.42.1 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2
6a1942015dd23bbf6252f1a4836ada09ffb40a6e8f815480b22e31ab1f2847ed
[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                CREATED             STATUS                    PORTS                                   NAMES
6a1942015dd2        csphere/wordpress:4.2   "/init.sh /usr/bin/s   6 seconds ago       Up 3 seconds              22/tcp, 443/tcp, 0.0.0.0:9090->80/tcp   wordpress           
8e266cd27a63        csphere/mysql:5.5       "/scripts/start"       47 hours ago        Up 47 hours               22/tcp, 0.0.0.0:3307->3306/tcp          newdb               
e274049db704        csphere/php-fpm:5.4     "/usr/bin/supervisor   7 days ago          Up 7 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1      "/usr/bin/supervisor   8 days ago          Up 8 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                  "/bin/bash"            9 days ago          Exited (127) 9 days ago                                           naughty_engelbart   
[root@Optimus /]#

访问172.17.42.1:9090失败。

再次查看容器

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                CREATED             STATUS                          PORTS                                   NAMES
6a1942015dd2        csphere/wordpress:4.2   "/init.sh /usr/bin/s   2 minutes ago       Exited (1) About a minute ago                                           wordpress           
8e266cd27a63        csphere/mysql:5.5       "/scripts/start"       47 hours ago        Up 47 hours                     22/tcp, 0.0.0.0:3307->3306/tcp          newdb               
e274049db704        csphere/php-fpm:5.4     "/usr/bin/supervisor   7 days ago          Up 7 days                       22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1      "/usr/bin/supervisor   8 days ago          Up 8 days                       0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                  "/bin/bash"            9 days ago          Exited (127) 9 days ago                                                 naughty_engelbart   
[root@Optimus /]#

查看日志

[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.28' (using password: YES) in - on line 7

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

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

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

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

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

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

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

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

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

MySQL Connection Error: (1045) Access denied for user 'admin'@'172.17.0.28' (using password: YES)
[root@Optimus /]#

问题描述

通过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

【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress

成功。

配置WordPress时,如果没有配置数据库以及数据库用户和密码时,在第一页需要填写连接哪个数据库、数据库服务器是哪台IP地址或者域名。这里已经设置好了,所以不需要再填写。

选择简体中文,点击继续.
用户名:admin
密码:admin

【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress


安装完成

【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress


登录

【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress


登录成功

【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress


访问WordPress主页

【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress

基础环境镜像+中间件镜像+WordPress的应用镜像
其中WordPress可以是自己的应用程序。

相关推荐