1. 程式人生 > >Docker學習總結(3)——Docker實戰之入門以及Dockerfile(三)

Docker學習總結(3)——Docker實戰之入門以及Dockerfile(三)

應用映象

csphere/wordpress:4.2

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

/docker-training/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"]

使用docker後,在專案程式碼目錄下,寫Dockerfile檔案,非常方便把專案程式碼直接打包到docker映象中,如有哪些檔案不想被打包進去,可以在.dockerignore檔案中定義

Dockerfile解析:

  • wordpress映象是基於csphere/php-fpm:5.4來進行構建
  • ONBUILD指令生效,把程式碼檔案拷貝到網站根目錄下
  • init.sh指令碼對WordPress連線mysql資料庫進行配置,固執行wordpress映象後,只需要進行配置WordPress即可,資料庫已準備就緒!

生成WordPress映象

docker build -t csphere/wordpress:4.2 .

檢視當前主機本地都有哪些docker映象

docker images

建立WordPress準備

檢視主機ip地址

ifconfig eth0 192.168.1.20

建立WordPress容器:

docker run -d -p 80:80 --name wordpress -e WORDPRESS_DB_HOST=192.168.1.20 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.249d0cddb4e6998a43285fe09165030ba80485065867b9cb8fae9fbdb97cd077f

引數解析:

  • -d 後臺執行
  • -p 80:80 將宿主機的80埠對映到容器的80埠
  • --name wordpress 給容器命名為wordpress
  • -e WORDPRESS_DB_HOST=192.168.1.20 資料庫主機的ip地址(或者域名)
  • -e WORDPRESS_DB_USER=admin 資料庫的使用者,預設是admin
  • -e WORDPRESS_DB_PASSWORD=csphere2015 登陸資料的密碼,預設是csphere2015
  • csphere/wordpress:4.2使用此映象建立WordPress容器

訪問http://your_ip,選擇語言,並進行設定wordpress

ENTRYPOINT和CMD的區別

ENTRYPOINT解析

定義:

An ENTRYPOINT allows you to configure a container that will run as an executable

執行一個Docker容器像執行一個程式一樣

ENTRYPOINT的使用方法:

1.ENTRYPOINT ["executable", "param1", "param2"] (the preferred exec form)

推薦使用1方法,啟動起來後,pid為1

2.ENTRYPOINT command param1 param2 (shell form) 

啟動起來後,pid號為shell命令執行完的pid號

CMD解析

CMD的使用方法:

1.CMD ["executable","param1","param2"] (exec form, this is the preferred form)

執行一個可執行的檔案並提供引數

2.CMD ["param1","param2"] (as default parameters to ENTRYPOINT) 

為ENTRYPOINT指定引數

3.CMD command param1 param2 (shell form) 

是以”/bin/sh -c”的方法執行的命令

實戰測試CMD

vim Dockerfile
FROM centos:centos7.1.1503

CMD ["/bin/echo", "This is test cmd"]

生成cmd映象 docker build -t csphere/cmd:0.1 . 生成cmd容器,進行測試 docker run -it --rm csphere/cmd:0.1 This is test cmd 測試是否可以替換cmd的命令 docker run -it csphere/cmd:0.1 /bin/bash [[email protected] /]#

測試結果,在Dockerfile中定義的CMD命令,在執行docker run的時候,CMD命令可以被替換。

實戰測試ENTRYPOINT

FROM centos:centos7.1.1503

ENTRYPOINT ["/bin/echo", "This is test entrypoint"]

生成ent(entrypoint)映象 docker build -t csphere/ent:0.1 .

生成ent容器,進行測試 docker run -it csphere/ent:0.1 This is test entrypoint 測試是否可以替換entrypoint的命令 docker run -it csphere/ent:0.1 /bin/bash This is test entrypoint /bin/bash

測試結果,在Dockerfile定義的ENTRYPOINT命令,通過以上方式不能被替換

實戰再次測試ENTRYPOINT

docker run -it --entrypoint=/bin/bash csphere/ent:0.1 測試結果,ENTRYPOINT命令也可以被替換,需要在執行docker run時新增--entrypoint=引數,此方法多用來進行除錯