[TOC]
上一篇文章介紹了使用docker commit
命令來構(gòu)建自己的鏡像藤滥。
本篇文章將使用Dockerfile實現(xiàn)上篇文章中的需求咐蚯。
1 構(gòu)建自己的鏡像
此處我們打算,給一個centos:6.8容器安裝nginx服務(wù)器。
并將其狀態(tài)保留,以便不用每次啟動新容器都要再次安裝nginx冗栗。
1.1 構(gòu)建Dockerfile上下文
先來隨便找個位置建個目錄當(dāng)做Dockerfile的上下文。
此處本人在/root/workspace/docker/建立目錄my-first-Dockerfile怠苔,如下所示:
[root@h1 my-first-Dockerfile]# pwd
/root/workspace/docker/my-first-Dockerfile
[root@h1 my-first-Dockerfile]# tree
.
├── Dockerfile #Dockerfile文件
└── nginx.repo #安裝nginx用的yum源
0 directories, 2 files
[root@h1 my-first-Dockerfile]#
文件內(nèi)容如下:
# nginx.repo文件內(nèi)容
[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文件內(nèi)容
# Dockerfile內(nèi)容
[root@h1 my-first-Dockerfile]# cat Dockerfile
# version 0.0.1-snapshot
# 從一個基礎(chǔ)鏡像centos:6.8開始構(gòu)建
FROM centos:6.8
# 維護者信息
MAINTAINER hylexus "hylexus@163.com"
# 將Dockerfile上下文中的nginx.repo復(fù)制到容器中的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
[root@h1 my-first-Dockerfile]#
1.3 構(gòu)建鏡像
執(zhí)行命令開始構(gòu)建:
docker build -t="hylexus/nginx-1" .
過程如下:
# 命令最后的點表示Dockerfile所在目錄
[root@h1 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 "hylexus@163.com"
---> 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) <centos-6-key@centos.org>
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
[root@h1 my-first-Dockerfile]#
1.4 啟動容器
# 查看剛才構(gòu)建的鏡像
[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]#
#####################################################
#####################################################
# 當(dāng)然也可以在啟動的時候指定端口映射
[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 構(gòu)建過程中的幾個問題
2.1 Dockerfile大致流程
從構(gòu)建時的輸出羡洛,可以看出每條RUN指令都會生成一個新的鏡像層挂脑。
并且默認情況下RUN指令都會以/bin/sh -c
來包裝執(zhí)行。
大致流程如下:
- Dockerfile的第一條指令一般都是FROM,表示從一個基礎(chǔ)鏡像開始構(gòu)建
- 執(zhí)行一條命令對鏡像做出修改
- 提交更新
- 基于本次更新欲侮,運行新的容器
- 繼續(xù)執(zhí)行下一條命令
- 如此反復(fù)執(zhí)行……
2.2 緩存
在構(gòu)建過程中每次生成一層新的鏡像的時候這個鏡像就會被緩存崭闲。即使是后面的某個步驟導(dǎo)致構(gòu)建失敗,再次構(gòu)建的時候就會從失敗的那層鏡像的前一條指令繼續(xù)往下執(zhí)行威蕉。
如果不想使用這種緩存功能刁俭,可以在構(gòu)建的時候加上--no-cache
選項:
docker build --no-cache -t="hylexus/nginx-1" .
下一篇將介紹Dockerfile常用指令。