Dockfile 是一種被Docker 程式解釋的腳本,Dockerfile 由一條一條的指令組成,每條指令對應Linux 下面的一條指令。Docker 程式将這些Dockerfile 指令翻譯真正的Linux 指令。Dockerfile 有自己書寫格式和支援的指令,Docker 程式解決這些指令間的依賴關系,Docker 程式将讀取Dockerfile,根據指令生成定制的image。
Dockerfile 的指令是忽略大小寫的,建議使用大寫,使用 # 作為注釋,每一行隻支援一條指令,每條指令可以攜帶多個參數。
Dockerfile 的指令根據作用可以分為兩種,建構指令和設定指令。建構指令用于建構image,其指定的操作不會在運作image 的容器上執行;設定指令用于設定image 的屬性, 其指定的操作将在運作image 的容器中執行
必須指定且需要在Dockerfile 其他指令的前面。後續的指令都依賴于該指令指定的image。FROM 指令指定的基礎image 可以是官方遠端倉庫中的,也可以位于本地倉庫。
格式:FROM 鏡像 | FROM 鏡像:tag
用于将image 的制作者相關的資訊寫入到image 中。當我們對該image 執行docker inspect 指令時,輸出中有相應的字段記錄該資訊。
RUN 可以運作任何被基礎image 支援的指令。如基礎image 選擇了ubuntu, 那麼軟體管理部分隻能使用ubuntu 的指令
用于container(容器)啟動時指定的操作。該操作可以是執行自定義腳本, 也可以是執行系統指令。該指令隻能在檔案中存在一次,如果有多個,則隻執行最後一條
指定容器啟動時執行的指令,可以多次設定,但是隻有最後一個有效
設定啟動容器的使用者,預設是root 使用者
該指令會将容器中的端口映射成主控端器中的某個端口。
在image 中設定一個環境變量
所有拷貝到container 中的檔案和檔案夾權限為0755,uid 和gid 為0;如果是一個目錄,那麼會将該目錄下的所有檔案添加到container 中,不包括目錄;如果檔案是可識别的壓縮格式,則docker 會幫忙解壓縮(注意壓縮格式);如果<src>是檔案且<dest>中不使用斜杠結束,則會将<dest>視為檔案,<src>的内容會寫入<dest>;如果<src>是檔案且<dest> 中使用斜杠結束,則會<src>檔案拷貝到<dest>目錄下。
使容器中的一個目錄具有持久化存儲資料的功能,該目錄可以被容器本身使用,也可以共享給其他容器使用
可以多次切換(相當于cd 指令),對RUN,CMD,ENTRYPOINT 生效
ONBUILD 指定的指令在建構鏡像時并不執行,而是在它的子鏡像中執行
導入鏡像
[root@localhost ~]# cat centos-7-x86_64.tar.gz |docker import - centos:7
<a href="http://sha256b19b83cf0649f2e4ed3819ba57c62745c53e537c498a117a3e5c6c033778e2da/" target="_blank">sha256:b19b83cf0649f2e4ed3819ba57c62745c53e537c498a117a3e5c6c033778e2da</a>
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 b19b83cf0649 About a minute ago 589.4 MB
[root@localhost ~]# mkdir sshd_dockerfile
[root@localhost ~]# cd sshd_dockerfile/
[root@localhost sshd_dockerfile]# vi run.sh
#!/bin/bash
/usr/sbin/sshd -D
[root@localhost sshd_dockerfile]# ssh-keygen -t rsa //在主控端上生成ssh 密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 回車
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 回車
Enter same passphrase again: 回車
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
|E . o . |
| .o = = S |
| ..=.= o . |
| .o=..o . |
| .*..... |
| +o.... |
+-----------------+
[root@localhost sshd_dockerfile]# cat ~/.ssh/id_rsa.pub > ./authorized_keys
[root@localhost sshd_dockerfile]# pwd
/root/sshd_dockerfile
[root@localhost sshd_dockerfile]# vi Dockerfile
FROM centos:7
#RUN yum -y instlal openssh-server // 作者資訊
RUN mkdir -p /var/run/sshd // 安裝ssh服務,此鏡像已安裝,可以注釋掉
RUN mkdir -p /root/.ssh
RUN sed -ri 's/session required pan_loginuid.so/#session required pan_loginuid.so/g' /etc/pan.d/sshd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
ADD authorized_keys /root/.ssh/authorized_keys //複制配置檔案到相應位置
ADD run.sh /run.sh
RUN chmod 755 /run.sh //保證有執行權限
EXPOSE 22 // 開放端口
CMD ["/run.sh"] // 運作容器時執行的腳本
[root@localhost ~]# grep pam_loginuid.so /etc/pam.d/sshd //可以再打開一個終端,找到檔案進行複制
session required pam_loginuid.so
[root@localhost sshd_dockerfile]# ls
authorized_keys Dockerfile run.sh
[root@localhost sshd_dockerfile]# docker build -t sshd:1 ./ //建立鏡像,取名為sshd,tag 為1,以目前目錄下的Dockerfile 為準
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM centos:7
---> b19b83cf0649
---> Using cache
---> d78926f7aa7a
Step 3 : RUN mkdir -p /var/run/sshd
---> 57f134b7c800
Step 4 : RUN mkdir -p /root/.ssh
---> 9c77b88dc36b
Step 5 : RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
---> Running in 537659b149a8
---> 07880e20fece
Removing intermediate container 537659b149a8
Step 6 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
---> Running in 87732838ce5a
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
| . .. S |
| . .o . |
| .. + o |
|oE++*. .. |
|OB+==o.o. |
---> 3e538a147e31
Removing intermediate container 87732838ce5a
Step 7 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
---> Running in 5f19b0617a2f
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
+--[ DSA 1024]----+
| . |
| . . |
| . o = |
| o O S +o |
| * + *.o+ |
| o + o+. |
| oE .. |
| .. |
---> 400b4f436b11
Removing intermediate container 5f19b0617a2f
Step 8 : ADD authorized_keys /root/.ssh/authorized_keys
---> f5d422a10648
Removing intermediate container d5d65efba938
Step 9 : ADD run.sh /run.sh
---> 59ded99a900d
Removing intermediate container 252405d68ebd
Step 10 : RUN chmod 755 /run.sh
---> Running in 8ebbf6f4bff4
---> 9dbbaaee5a5b
Removing intermediate container 8ebbf6f4bff4
Step 11 : EXPOSE 22
---> Running in d813e1d3a730
---> e96b802d43e8
Removing intermediate container d813e1d3a730
Step 12 : CMD /run.sh
---> Running in 36b6bdb72dcc
---> b84b454d5977
Removing intermediate container 36b6bdb72dcc
Successfully built b84b454d5977
[root@localhost sshd_dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd 1 b84b454d5977 35 seconds ago 589.4 MB
centos 7 b19b83cf0649 30 minutes ago 589.4 MB
[root@localhost sshd_dockerfile]# docker run -d -p 2222:22 sshd:1
824413feb77f3e7858525b86e0783e1b2d426ac0f854ef4bf27bdd8a70b66a13
[root@localhost sshd_dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
824413feb77f sshd:1 "/run.sh" 11 seconds ago Up 9 seconds 0.0.0.0:2222->22/tcp evil_meninsky
[root@localhost ~]# ssh 192.168.20.2 -p 2222
The authenticity of host '[192.168.20.2]:2222 ([192.168.20.2]:2222)' can't be established.
Are you sure you want to continue connecting (yes/no)? Yes //首次需要确認連接配接
Warning: Permanently added '[192.168.20.2]:2222' (RSA) to the list of known hosts.
[root@824413feb77f ~]# ss -anpt |grep 22 //ss 指令,類似于centos6 系的netstat 指令
LISTEN 0 128 *:22 *:* users:(("sshd",pid=5,fd=3))
ESTAB 0 0 172.17.0.2:22 192.168.20.2:39775 users:(("sshd",pid=6,fd=3))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=5,fd=4))
[root@824413feb77f ~]# 登出 //按ctrl+d 退出
Connection to 192.168.20.2 closed.
使用Dockerfile 制作的sshd 鏡像模闆以完成,生成鏡像,上傳至倉庫或儲存下來,供日後使用,此處采用docker save 方法導出到本地
[root@localhost ~]# docker save -o centos7_sshd.tar sshd:1
[root@localhost ~]# ls
anaconda-ks.cfg centos7_sshd.tar centos-aaa.tar mysql5.tar sshd_dockerfile
backup.tar centos-7-x86_64.tar.gz centos.tar nginx.tar tomcat.tar
[root@localhost ~]# ls centos7_sshd.tar
centos7_sshd.tar
[root@localhost ~]#
其他的鏡像模闆與以上相同,不同的是Dockerfile 以及run.sh,根據鏡像的系統與要做的服務鏡像裡的服務,編寫不同的配置Dockerfile 和run.sh
本文轉自Mr_chen 51CTO部落格,原文連結:http://blog.51cto.com/metis/1945272,如需轉載請自行聯系原作者