1. 程式人生 > >docker築基篇-04-使用Dockerfile構建自己的映象

docker築基篇-04-使用Dockerfile構建自己的映象

上一篇文章介紹了使用docker commit命令來構建自己的映象。
本篇文章將使用Dockerfile實現上篇文章中的需求。

1 構建自己的映象

此處我們打算,給一個centos:6.8容器安裝nginx伺服器。
並將其狀態保留,以便不用每次啟動新容器都要再次安裝nginx。

1.1 構建Dockerfile上下文

先來隨便找個位置建個目錄當做Dockerfile的上下文。
此處本人在/root/workspace/docker/建立目錄my-first-Dockerfile,如下所示:

[[email protected] my-first-Dockerfile]# pwd
/root/workspace/docker/my-first-Dockerfile [[email protected] my-first-Dockerfile]# tree . ├── Dockerfile #Dockerfile檔案 └── nginx.repo #安裝nginx用的yum源 0 directories, 2 files [[email protected] my-first-Dockerfile]#

檔案內容如下:

# nginx.repo檔案內容
[root@h1 my-first-Dockerfile]# cat nginx.repo 
[nginx]
name=nginx repo
baseurl=http:
//nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1

1.2 Dockerfile檔案內容

# Dockerfile內容
[[email protected] my-first-Dockerfile]# cat Dockerfile 
# version 0.0.1-snapshot
# 從一個基礎映象centos:6.8開始構建
FROM centos:6.8
# 維護者資訊
MAINTAINER hylexus "[email protected]"
# 將Dockerfile上下文中的nginx.repo複製到容器中的yum源位置
COPY ./nginx.repo /etc/yum.repos.d/nginx.repo RUN yum makecache # 安裝nginx RUN yum install -y nginx # 修改nginx首頁資訊 RUN echo "home page of container niginx server" > /usr/share/nginx/html/index.html # 暴露80埠 EXPOSE 80 [[email protected] my-first-Dockerfile]#

1.3 構建映象

執行命令開始構建:

docker build -t="hylexus/nginx-1" .

過程如下:

# 命令最後的點表示Dockerfile所在目錄
[[email protected] my-first-Dockerfile]# docker build -t="hylexus/nginx-1" .
Sending build context to Docker daemon 4.096 kB
Sending build context to Docker daemon 
Step 0 : FROM centos:6.8
 ---> 80e46367f846
Step 1 : MAINTAINER hylexus "[email protected]"
 ---> Using cache
 ---> c518397fc23e
Step 2 : COPY ./nginx.repo /etc/yum.repos.d/nginx.repo
 ---> 5c6e01981678
Removing intermediate container c6e430804af3
Step 3 : RUN yum makecache
 ---> Running in d17bef44ff52
Loaded plugins: fastestmirror, ovl
Metadata Cache Created
 ---> 25e01b8498fc
Removing intermediate container d17bef44ff52
Step 4 : RUN yum install -y nginx
 ---> Running in 3054e6a7d381
Loaded plugins: fastestmirror, ovl
Setting up Install Process
Determining fastest mirrors
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.10.1-1.el6.ngx will be installed
# 此處省略N行.....
--> Running transaction check
---> Package dbus-glib.x86_64 0:0.86-6.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch       Version                       Repository   Size
================================================================================
Installing:
 nginx               x86_64     1.10.1-1.el6.ngx              nginx       821 k
Installing for dependencies:
# 此處省略N行...
 util-linux-ng       x86_64     2.17.2-12.24.el6              base        1.6 M

Transaction Summary
================================================================================
Install      15 Package(s)

Total download size: 21 M
Installed size: 41 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           356 kB/s |  21 MB     01:01     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <[email protected]>
 Package: centos-release-6-8.el6.centos.12.3.x86_64 (@CentOS/6.8)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : hwdata-0.233-16.1.el6.noarch                                1/15 
# 此處省略N行……
  Installing : nginx-1.10.1-1.el6.ngx.x86_64                              15/15 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : iptables-1.4.7-16.el6.x86_64                                1/15 
# 此處省略N行
  Verifying  : hwdata-0.233-16.1.el6.noarch                               15/15 

Installed:
  nginx.x86_64 0:1.10.1-1.el6.ngx                                               

Dependency Installed:
#此處省略N行……                                           
  util-linux-ng.x86_64 0:2.17.2-12.24.el6                                       

Complete!
 ---> 8741ff7c796b
Removing intermediate container 3054e6a7d381
Step 5 : RUN echo "home page of container niginx server" > /usr/share/nginx/html/index.html
 ---> Running in 81627cb51b65
 ---> 0d610e7a0000
Removing intermediate container 81627cb51b65
Step 6 : EXPOSE 80
 ---> Running in 778468b1dcea
 ---> 9f18a04cafcb
Removing intermediate container 778468b1dcea
Successfully built 9f18a04cafcb
[[email protected] my-first-Dockerfile]#

1.4 啟動容器

# 檢視剛才構建的映象
[root@h1 my-first-Dockerfile]# docker images hylexus/nginx-1
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hylexus/nginx-1     latest              9f18a04cafcb        38 minutes ago      564.4 MB

# 啟動容器
[root@h1 my-first-Dockerfile]# docker run -d -p 80 --name my-nginx-server hylexus/nginx-1 nginx -g "daemon off;"
8d6d810448fb7c80825f62058494f77eb991ee0e43ef82fa9367b96dd343f617

# 檢視隨機生成的埠對映
[root@h1 my-first-Dockerfile]# docker port 8d6d810448fb7c80825f62058494f77eb991ee0e43ef82fa9367b96dd343f617
80/tcp -> 0.0.0.0:32768
[root@h1 my-first-Dockerfile]#

#####################################################
#####################################################

# 當然也可以在啟動的時候指定埠對映
[root@h1 my-first-Dockerfile]# docker run -dit -p 8080:80 --name my-nginx-server hylexus/nginx-1 nginx -g "daemon off;"
726ef73f47d1bd419f9aebbc2aaecae494170a3733a59678a0fc6e5a52b502a6
# 檢視埠對映
[root@h1 my-first-Dockerfile]# docker port 726ef73f47d1bd419f9aebbc2aaecae494170a3733a59678a0fc6e5a52b502a6
80/tcp -> 0.0.0.0:8080

之後就可以使用瀏覽器訪問測試了

1.5 將映象推送到DockerHub

就像github一樣的版本控制一樣。自己的docker映象也可以提交到DockerHub。

# 要先登入DockerHub
# 此處的hylexus/nginx-1即是映象名稱:<user-name>/<image-name>
docker push hylexus/nginx-1

2 構建過程中的幾個問題

2.1 Dockerfile大致流程

從構建時的輸出,可以看出每條RUN指令都會生成一個新的映象層。
並且預設情況下RUN指令都會以/bin/sh -c來包裝執行。

大致流程如下:

  • Dockerfile的第一條指令一般都是FROM,表示從一個基礎映象開始構建
  • 執行一條命令對映象做出修改
  • 提交更新
  • 基於本次更新,執行新的容器
  • 繼續執行下一條命令
  • 如此反覆執行……

2.2 快取

在構建過程中每次生成一層新的映象的時候這個映象就會被快取。即使是後面的某個步驟導致構建失敗,再次構建的時候就會從失敗的那層映象的前一條指令繼續往下執行。
如果不想使用這種快取功能,可以在構建的時候加上--no-cache選項:

docker build --no-cache -t="hylexus/nginx-1" .

下一篇將介紹Dockerfile常用指令。