- 建構自己的鏡像
- 1 建構Dockerfile上下文
- 2 Dockerfile檔案内容
- 3 建構鏡像
- 4 啟動容器
- 5 将鏡像推送到DockerHub
- 建構過程中的幾個問題
- 1 Dockerfile大緻流程
- 2 緩存
上一篇文章介紹了使用
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源
directories, 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=
enabled=
1.2 Dockerfile檔案内容
# Dockerfile内容
[[email protected] my-first-Dockerfile]# cat Dockerfile
# version 0.0.1-snapshot
# 從一個基礎鏡像centos:6.8開始建構
FROM centos:
# 維護者資訊
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
[[email protected] my-first-Dockerfile]#
1.3 建構鏡像
執行指令開始建構:
過程如下:
# 指令最後的點表示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
---> e46367f846
Step 1 : MAINTAINER hylexus "[email protected]"
---> Using cache
---> c518397fc23e
Step 2 : COPY ./nginx.repo /etc/yum.repos.d/nginx.repo
---> c6e01981678
Removing intermediate container c6e430804af3
Step 3 : RUN yum makecache
---> Running in d17bef44ff52
Loaded plugins: fastestmirror, ovl
Metadata Cache Created
---> e01b8498fc
Removing intermediate container d17bef44ff52
Step 4 : RUN yum install -y nginx
---> Running in e6a7d381
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 :.-.el6.ngx will be installed
# 此處省略N行.....
--> Running transaction check
---> Package dbus-glib.x86_64 :.-.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!
---> ff7c796b
Removing intermediate container 3054e6a7d381
Step 5 : RUN echo "home page of container niginx server" > /usr/share/nginx/html/index.html
---> Running in cb51b65
---> 0d610e7a000
Removing intermediate container 81627cb51b65
Step 6 : EXPOSE 80
---> Running in b1dcea
---> f18a04cafcb
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- latest f18a04cafcb minutes ago MB
# 啟動容器
[root@h1 my-first-Dockerfile]# docker run -d -p 80 --name my-nginx-server hylexus/nginx-1 nginx -g "daemon off;"
d6d810448fb7c80825f62058494f77eb991ee0e43ef82fa9367b96dd343f617
# 檢視随機生成的端口映射
[root@h1 my-first-Dockerfile]# docker port 8d6d810448fb7c80825f62058494f77eb991ee0e43ef82fa9367b96dd343f617
/tcp -> :
[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;"
ef73f47d1bd419f9aebbc2aaecae494170a3733a59678a0fc6e5a52b502a6
# 檢視端口映射
[root@h1 my-first-Dockerfile]# docker port 726ef73f47d1bd419f9aebbc2aaecae494170a3733a59678a0fc6e5a52b502a6
/tcp -> :
之後就可以使用浏覽器通路測試了
1.5 将鏡像推送到DockerHub
就像github一樣的版本控制一樣。自己的docker鏡像也可以送出到DockerHub。
# 要先登入DockerHub
# 此處的hylexus/nginx-1即是鏡像名稱:<user-name>/<image-name>
docker push hylexus/nginx-
2 建構過程中的幾個問題
2.1 Dockerfile大緻流程
從建構時的輸出,可以看出每條RUN指令都會生成一個新的鏡像層。
并且預設情況下RUN指令都會以
/bin/sh -c
來包裝執行。
大緻流程如下:
- Dockerfile的第一條指令一般都是FROM,表示從一個基礎鏡像開始建構
- 執行一條指令對鏡像做出修改
- 送出更新
- 基于本次更新,運作新的容器
- 繼續執行下一條指令
- 如此反複執行……
2.2 緩存
在建構過程中每次生成一層新的鏡像的時候這個鏡像就會被緩存。即使是後面的某個步驟導緻建構失敗,再次建構的時候就會從失敗的那層鏡像的前一條指令繼續往下執行。
如果不想使用這種緩存功能,可以在建構的時候加上
--no-cache
選項:
下一篇将介紹Dockerfile常用指令。