【Docker實戰之入門】Dockerfile詳細分析:構建docker鏡像(4)構建動態網站WordPress

代碼文件php

[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放在該目錄下。html

查看dockerfilemysql

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目錄下。nginx

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

文件目錄以下web

[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其餘的代碼文件便可。sql

查看當前目錄下全部文件docker

[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文件內容shell

[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腳本。segmentfault

在執行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 /]#

訪問失敗
clipboard.png

查看出錯日誌

[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;
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

網卡信息

[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

clipboard.png

成功。

配置WordPress時,若是沒有配置數據庫以及數據庫用戶和密碼時,在第一頁須要填寫鏈接哪一個數據庫、數據庫服務器是哪臺IP地址或者域名。這裏已經設置好了,因此不須要再填寫。

選擇簡體中文,點擊繼續.
用戶名:admin
密碼:admin

clipboard.png


安裝完成

clipboard.png


登陸

clipboard.png


登陸成功

clipboard.png


訪問WordPress主頁

clipboard.png

基礎環境鏡像+中間件鏡像+WordPress的應用鏡像其中WordPress能夠是本身的應用程序。

相關文章
相關標籤/搜索