2.docker學習筆記:製作docker映象
製作docker映象
構建映象的方式
上篇章節介紹瞭如何從docker hub上拉取映象,同時我們也可以製作映象上傳到docker hub上。
首先我們需要做一些準備工作:
2.登入docker hub:
可以選擇在官網進行登入,也可以使用命令列進行登入。我選擇在命令列登入,輸入使用者名稱密碼即可。
[email protected]:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ********
Password:
Login Succeeded
如果成功會提示“Login Succeeded”
製作一個映象
我們所謂的製作映象並不是從無到有製作,而是在官方提供的docker映象基礎上安裝應用程式,生成基礎的專有映象,這個是微服務的基本元件。
檢視docker進行中的容器,或者從映象中開啟一個容器然後,我們選擇後者進行示例:
root@zhangchi-ThinkPad-T450s:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17c4fdee15b0 jlinoff/centos-6.5-x86_64-base "/bin/bash" 27 seconds ago Up 27 seconds centos_base
docker commit
我們以上述的容器為例,這是一個比較新的centos容器,我們在此基礎上安裝httpd服務,然後關閉該容器:
root@zhangchi -ThinkPad-T450s:~# docker attach 17c4fdee15b0
[root@17c4fdee15b0 /]#
[root@17c4fdee15b0 /]#
[root@17c4fdee15b0 /]# yum install httpd -y
[root@17c4fdee15b0 /]# exit
exit
提交制定的容器(算是從容器—–>映象):
root@zhangchi-ThinkPad-T450s:~# docker commit 17c4fdee15b0 zhangchiwd371/centos_httpd
然後檢視剛才提交的映象的詳細資訊:
root@zhangchi-ThinkPad-T450s:~# docker inspect zhangchiwd371/centos_httpd
[
{
"Id": "sha256:52d55d6a2d92472dd7e57e5c0cae3815c5e676c1ad321f007698781755c38d32",
"RepoTags": [
"zhangchiwd371/centos_httpd:latest"
],
"RepoDigests": [],
"Parent": "sha256:3d6541b04d52d5d3300fa286e6db54144f12f4fb4b0dff3bd3dff967c49a9e8f",
"Comment": "",
"Created": "2016-11-10T14:50:30.774207025Z",
"Container": "17c4fdee15b0d23234fa27c3b9213d1881c2af17fff78638d2966fe80f945407",
"ContainerConfig": {
"Hostname": "17c4fdee15b0",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [],
"Cmd": [
"/bin/bash"
],
"Image": "jlinoff/centos-6.5-x86_64-base",
"Volumes": {},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "1.12.1",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [],
"Cmd": [
"/bin/bash"
],
"Image": "",
"Volumes": {},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 247891414,
"VirtualSize": 247891414,
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:6409da76fbfaf0112552d11d3f726a5cf576e1f03acc81753baa41270254d513",
"sha256:56c1b43489bdba0d9f8a140d0ec52e6d9994dbfd8235d709527807e5a8ade424"
]
}
}
]
檢視當前docker的映象列表,發現剛才建立的映象已經顯示出來了:
root@zhangchi-ThinkPad-T450s:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhangchiwd371/centos_httpd latest 52d55d6a2d92 7 minutes ago 247.9 MB
jenkins latest 7e7d1b9dc0c8 41 hours ago 714.1 MB
zhangchiwd371/static_web latest 46da60b77b45 47 hours ago 228.3 MB
konstruktoid/ubuntu latest 42ec6b22c6b8 3 days ago 82.78 MB
centos latest 0584b3d2cf6d 7 days ago 196.5 MB
ubuntu latest f753707788c5 3 weeks ago 127.2 MB
kalilinux/kali-linux-docker latest b0d9d7dfbd0a 9 weeks ago 1.021 GB
jlinoff/centos-6.5-x86_64-base latest 3d6541b04d52 2 years ago 178.3 MB
docker build
除了上述的方式docker commit可以將容器導成映象之外,還可以通過docker build進行操作,不過兩者有如下區別:
(1)docker commit 是往版本控制系統裡提交一次變更。使用這種方式製作映象,本質上是執行一個基礎映象,然後在基礎映象上進行軟體安裝和修改。最後再將改動提交到版本系統中。
(2)使用docker build建立映象需要編寫Dockerfile.這個相當於是自己編寫策略,相當於是docker裡的指令碼,它具有可重複性、透明性。而且構建時是以層級進行構建的。非常方便。
下面就以Dockerfile的形式來編寫構建指令碼。
1.首先建立一個目錄來存放配置映象所需的資訊。
root@zhangchi-ThinkPad-T450s:~# mkdir /centos_vsftpd
root@zhangchi-ThinkPad-T450s:~# cd /centos_vsftpd/
首先我們檢視下從那個已有的映象進行拓展:
root@zhangchi-ThinkPad-T450s:/centos_vsftpd# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhangchiwd371/centos_httpd latest 52d55d6a2d92 35 minutes ago 247.9 MB
jenkins latest 7e7d1b9dc0c8 41 hours ago 714.1 MB
zhangchiwd371/static_web latest 46da60b77b45 47 hours ago 228.3 MB
konstruktoid/ubuntu latest 42ec6b22c6b8 3 days ago 82.78 MB
centos latest 0584b3d2cf6d 7 days ago 196.5 MB
ubuntu latest f753707788c5 3 weeks ago 127.2 MB
kalilinux/kali-linux-docker latest b0d9d7dfbd0a 9 weeks ago 1.021 GB
jlinoff/centos-6.5-x86_64-base latest 3d6541b04d52 2 years ago 178.3 MB
還是選擇最後一個centos-6.5,下面編寫Dockerfile檔案。
#create the vsftpd serivce images(centos-6.5)
FROM jlinoff/centos-6.5-x86_64-base
#install the vsftpd service
RUN yum install vsftpd -y
#copy vsftpd.conf to service
COPY vsftpd.conf /etc/vsftpd/vsftpd.conf
#touch file say success
RUN echo "i have been installed the vsftpd service " >> /mnt/show_log
EXPOSE 21
這裡有幾個需要說明的:
(1)在Dockerfile中,#開頭的被認作是註釋;
(2)FROM代表這從哪個指定的映象為基礎處理接下來的命令,如果該映象不在本地,則docker會從dockerhub上進行下載。
(3)RUN代表著所需要執行的命令。
(4)COPY可以把指定的檔案或者目錄在建立映象的時候拷貝到映象的指定位置。
接下來執行Dockerfile指令碼內容,命令如下所示:
[email protected]:/centos_vsftpd# docker build -t="zhangchiwd371/centos_vsftpd" .
Sending build context to Docker daemon 8.704 kB
Step 1 : FROM jlinoff/centos-6.5-x86_64-base
---> 3d6541b04d52
Step 2 : RUN yum install vsftpd -y
---> Using cache
---> fbf104aa61ec
Step 3 : COPY vsftpd.conf /etc/vsftpd/vsftpd.conf
---> df58035319d4
Removing intermediate container 47d19892ab2c
Step 4 : RUN echo "i have been installed the vsftpd service " >> /mnt/show_log
---> Running in 343036ae438d
---> c73a6fc7ae65
Removing intermediate container 343036ae438d
Step 5 : EXPOSE 21
---> Running in 632f973833d0
---> 8055ae6f1b21
Removing intermediate container 632f973833d0
Successfully built 8055ae6f1b21
執行完上述指令碼之後,我們新的映象中應該已經安裝了vsftpd服務,並且vsftpd.conf配置檔案內容也應該是我們設定的,還有在/mnt目錄中建立一個show_log檔案。
同時也可以看到docker build的執行是按照步驟分步執行的,這種建立映象的方式非常的快捷,而且一旦出錯可以快速定位有問題的步驟(後續介紹)。
執行完成後,在映象列表中可以看到新建立的映象(第一個):
root@zhangchi-ThinkPad-T450s:/centos_vsftpd# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhangchiwd371/centos_vsftpd latest 8055ae6f1b21 3 minutes ago 228.3 MB
zhangchiwd371/centos_httpd latest 52d55d6a2d92 44 minutes ago 247.9 MB
jenkins latest 7e7d1b9dc0c8 41 hours ago 714.1 MB
zhangchiwd371/static_web latest 46da60b77b45 2 days ago 228.3 MB
konstruktoid/ubuntu latest 42ec6b22c6b8 3 days ago 82.78 MB
centos latest 0584b3d2cf6d 7 days ago 196.5 MB
ubuntu latest f753707788c5 3 weeks ago 127.2 MB
kalilinux/kali-linux-docker latest b0d9d7dfbd0a 9 weeks ago 1.021 GB
jlinoff/centos-6.5-x86_64-base latest 3d6541b04d52 2 years ago 178.3 MB
以新建立的映象去安裝一個容器:
root@zhangchi-ThinkPad-T450s:/centos_vsftpd# docker run -i -t --name ttttt zhangchiwd371/centos_vsftpd /bin/bash
在新的容器中可以看到和我們預測的情況是相同的,vsftpd服務已經安裝,目標檔案也是存在的。
[root@965196e66d43 /]# yum install vsftpd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
Setting up Install Process
Package vsftpd-2.2.2-21.el6.x86_64 already installed and latest version
Nothing to do
[root@965196e66d43 /]#
[root@965196e66d43 /]#
[root@965196e66d43 /]# cd /mnt
[root@965196e66d43 mnt]# ls
show_log
小結
上述介紹了兩種製作映象的方式,我們在日常工作中更推薦第二種我們編寫大量的Dockerfile檔案,下節將會介紹Dockerfile的編寫規則。