一、Dockerfile 簡介及書寫規則
Dockfile 是一種被Docker 程序解釋的腳本,Dockerfile 由一條一條的指令組成,每條指令對應Linux 下面的一條命令。Docker 程序將這些Dockerfile 指令翻譯真正的Linux 命令。Dockerfile 有自己書寫格式和支持的命令,Docker 程序解決這些命令間的依賴關係,Docker 程序將讀取Dockerfile,根據指令生成定製的p_w_picpath。
Dockerfile 的指令是忽略大小寫的,建議使用大寫,使用 # 作爲註釋,每一行只支持一條指令,每條指令可以攜帶多個參數。
Dockerfile 的指令根據作用可以分爲兩種,構建指令和設置指令。構建指令用於構建p_w_picpath,其指定的操作不會在運行p_w_picpath 的容器上執行;設置指令用於設置p_w_picpath 的屬性, 其指定的操作將在運行p_w_picpath 的容器中執行
1、FROM(指定基礎p_w_picpath) 構建指令
必須指定且需要在Dockerfile 其他指令的前面。後續的指令都依賴於該指令指定的p_w_picpath。FROM 指令指定的基礎p_w_picpath 可以是官方遠程倉庫中的,也可以位於本地倉庫。
格式:FROM 鏡像 | FROM 鏡像:tag
2、MAINTAINER(用來指定鏡像創建者信息) 構建指令
用於將p_w_picpath 的製作者相關的信息寫入到p_w_picpath 中。當我們對該p_w_picpath 執行docker inspect 命令時,輸出中有相應的字段記錄該信息。
3、RUN(安裝軟件用) 構建指令
RUN 可以運行任何被基礎p_w_picpath 支持的命令。如基礎p_w_picpath 選擇了ubuntu, 那麼軟件管理部分只能使用ubuntu 的命令
4、CMD(設置container 啓動時執行的操作) 設置指令
用於container(容器)啓動時指定的操作。該操作可以是執行自定義腳本, 也可以是執行系統命令。該指令只能在文件中存在一次,如果有多個,則只執行最後一條
5、ENTRYPOINT(設置container 啓動時執行的操作) 設置指令
指定容器啓動時執行的命令,可以多次設置,但是隻有最後一個有效
6、USER(設置container 容器的用戶) 設置指令
設置啓動容器的用戶,默認是root 用戶
7、EXPOSE(指定容器需要映射到宿主機器的端口)設置指令
該指令會將容器中的端口映射成宿主機器中的某個端口。
8、ENV(用於設置環境變量) 構建指令
在p_w_picpath 中設置一個環境變量
9、ADD(從src 複製文件到container 的dest 路徑) 構建指令
所有拷貝到container 中的文件和文件夾權限爲0755,uid 和gid 爲0;如果是一個目錄,那麼會將該目錄下的所有文件添加到container 中,不包括目錄;如果文件是可識別的壓縮格式,則docker 會幫忙解壓縮(注意壓縮格式);如果<src>是文件且<dest>中不使用斜槓結束,則會將<dest>視爲文件,<src>的內容會寫入<dest>;如果<src>是文件且<dest> 中使用斜槓結束,則會<src>文件拷貝到<dest>目錄下。
10、VOLUME(指定掛載點)) 設置指令
使容器中的一個目錄具有持久化存儲數據的功能,該目錄可以被容器本身使用,也可以共享給其他容器使用
11、WORKDIR(切換目錄) 設置指令
可以多次切換(相當於cd 命令),對RUN,CMD,ENTRYPOINT 生效
12、ONBUILD(在子鏡像中執行)
ONBUILD 指定的命令在構建鏡像時並不執行,而是在它的子鏡像中執行
二、使用Dockerfile 創建sshd 鏡像模板
導入鏡像
[root@localhost ~]# cat centos-7-x86_64.tar.gz |docker import - centos:7
sha256:b19b83cf0649f2e4ed3819ba57c62745c53e537c498a117a3e5c6c033778e2da
[root@localhost ~]# docker p_w_picpaths
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:
ab:04:76:36:a0:76:6a:d5:ad:a8:6e:ed:07:ca:96:01 [email protected]
The key's randomart p_w_picpath 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
MAINTAINER from [email protected] //設置基礎鏡像
#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
Step 2 : MAINTAINER from [email protected]
---> Using cache
---> d78926f7aa7a
Step 3 : RUN mkdir -p /var/run/sshd
---> Using cache
---> 57f134b7c800
Step 4 : RUN mkdir -p /root/.ssh
---> Using cache
---> 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.
The key fingerprint is:
0b:ef:a6:92:68:e2:3a:7a:fb:78:41:af:e3:c9:e0:6a root@537659b149a8
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| |
| |
| |
| . |
| . .. 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.
The key fingerprint is:
67:cb:e0:33:84:26:d0:9f:e3:0c:9d:7d:bf:43:d2:ac root@537659b149a8
The key's randomart p_w_picpath is:
+--[ 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 p_w_picpaths
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.
RSA key fingerprint is 0b:ef:a6:92:68:e2:3a:7a:fb:78:41:af:e3:c9:e0:6a.
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