#Docker實戰之入門以及Dockerfile(三)
[Docker實戰之入門以及Dockerfile(一)](http://git.oschina.net/dockerf/docker-practice/blob/master/Docker%E5%AE%9E%E6%88%98%E4%B9%8B%E5%85%A5%E9%97%A8%E4%BB%A5%E5%8F%8ADockerfile%28%E4%B8%80%29.md?dir=0&filepath=Docker%E5%AE%9E%E6%88%98%E4%B9%8B%E5%85%A5%E9%97%A8%E4%BB%A5%E5%8F%8ADockerfile%28%E4%B8%80%29.md&oid=fd533b0b7665712fdafd3142a61e326ef416dbe1&sha=f11d77675b6ad637a21f26ece6bb11b9e4a66386)
[Docker實戰之入門以及Dockerfile(二)](http://git.oschina.net/dockerf/docker-practice/blob/master/Docker%E5%AE%9E%E6%88%98%E4%B9%8B%E5%85%A5%E9%97%A8%E4%BB%A5%E5%8F%8ADockerfile%28%E4%BA%8C%29.md?dir=0&filepath=Docker%E5%AE%9E%E6%88%98%E4%B9%8B%E5%85%A5%E9%97%A8%E4%BB%A5%E5%8F%8ADockerfile%28%E4%BA%8C%29.md&oid=76ce88d4a3af4e797873fac07f8f84c4ac72602a&sha=f11d77675b6ad637a21f26ece6bb11b9e4a66386)
文章內容,由【[Docker實訓課程](https://csphere.cn/training)】
[第一講視頻](http://pan.baidu.com/s/1hq2COGc)翻譯整理而成
[培訓代碼](https://github.com/nicescale/docker-training) https://github.com/nicescale/docker-training
[虛擬機鏡像](http://market.aliyun.com/products/56014007/jxsc000181.html) http://market.aliyun.com/products/56014007/
##應用鏡像
##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.2
49d0cddb4e6998a43285fe09165030ba80485065867b9cb8fae9fbdb97cd077f
```
參數解析:
- -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
[root@c1963a366319 /]#
```
測試結果,在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=`參數,此方法多用來進行調試
##更多精彩內容,訪問:[cSphere-希云社區](https://discuss.csphere.cn)
說明,文章由[cSphere-希云](https://csphere.cn)所有,轉載請整體轉載,并保留原文鏈接,且不得修改原文!
轉載,請聯系"cSphere"微信公眾號
