dockerfile自動構建docker鏡像nginx
特色:
1: dockerfile 相似ansible的playbook劇本
2: dockerfile 更適合傳輸,實現更多的定製化
3:dockerfile 能夠指定鏡像的初始命令docker
dockerfile主要組成部分:
基礎鏡像信息 FROM centos:6.8
製做鏡像操做指令 RUN yum install openssh-server -y
容器啓動時執行指令 CMD ["/bin/bash"]
dockerfile經常使用指令:
FROM 這個鏡像的媽媽是誰?(指定基礎鏡像)
MAINTAINER (指定維護者信息,能夠沒有)
RUN 你想讓它幹啥(在命令前面加上RUN便可)
ADD 給它點創業資金(COPY文件,會自動解壓)
WORKDIR 我是cd,今天剛化了妝(設置當前工做目錄)
VOLUME 給它一個存放行李的地方(設置卷,掛載主機目錄)
EXPOSE 它要打開的門是啥(指定對外的端口)(-P 隨機端口)
CMD 奔跑吧,兄弟!(指定容器啓動後的要乾的事情)(容易被替換)
dockerfile其餘指令:
COPY 複製文件
ENV 環境變量
ENTRYPOINT 容器啓動後執行的命令(沒法被替換,啓容器的時候指定的命令,會被當成參數)vim
生產中如何編寫dockerfile:
1: 參考其餘的dockerfile
2: 官方dockerfile或者時速雲鏡像廣場centos
小試牛刀:寫一個加單的dockerfile,構建ssh的鏡像
(前面咱們是手動的yum安裝ssh,打包成ssh的鏡像的)bash
1: 建立dockerfile 的centos_ssh目錄
[root@k8s129 opt]# mkdir dockerfile
[root@k8s129 opt]# cd dockerfile/
[root@k8s129 dockerfile]# mkdir centos_ssh
[root@k8s129 dockerfile]# cd centos_ssh/ssh
2: 獲取系統鏡像列表,找到基礎鏡像(centos:latest)
[root@k8s129 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_ssh v1.2 275e0f02e6b1 9 days ago 265MB
nginx latest 5a9061639d0a 12 days ago 126MB
centos latest 0f3e07c0138f 3 weeks ago 220MB
busybox latest 19485c79a9bb 7 weeks ago 1.22MB
[root@k8s129 ~]# tcp
3:編寫dockerfile
#注意,文件名必須叫dockerfile ,不然系統不識別
[root@k8s129 centos_ssh]# vim dockerfile
FROM centos:latest
RUN yum install -y openssh-server
RUN yum install -y net-tools
RUN yum install -y passwd
RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
RUN sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
RUN sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
RUN echo "123456"|passwd --stdin root
CMD ["/usr/sbin/sshd","-D"]測試
4:執行dockerfile ,自動構建鏡像
#docker build -t 構建後的鏡像名字 . 表明從當前路徑下的dockerfile構建(/opt/dockerfile/centos_ssh)
[root@k8s129 centos_ssh]# docker build -t dockerfile_centos:v1.1 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos:latest
---> 0f3e07c0138f
Step 2/10 : RUN yum install -y openssh-server
---> Running in 858766ea9002
...
Removing intermediate container 633e756e6040
---> fb540a4947a1
Successfully built fb540a4947a1
Successfully tagged dockerfile_centos:v1.1
#查看鏡像已經制做出來了
[root@k8s129 centos_ssh]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerfile_centos v1.1 fb540a4947a1 6 seconds ago 296MB
centos_ssh v1.2 275e0f02e6b1 9 days ago 265MB
nginx latest 5a9061639d0a 12 days ago 126MB
centos latest 0f3e07c0138f 3 weeks ago 220MB
busybox latest 19485c79a9bb 7 weeks ago 1.22MB
[root@k8s129 centos_ssh]# ui
5: 測試docker鏡像是否可用
[root@k8s129 centos_ssh]# docker run -d -p 222:22 dockerfile_centos:v1.1
c5bbc44d2ac704c69ba197a58f25ccbcafa9371bfe2972f033b8293a1861a400
[root@k8s129 centos_ssh]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5bbc44d2ac7 dockerfile_centos:v1.1 "/usr/sbin/sshd -D" 5 seconds ago Up 4 seconds 0.0.0.0:222->22/tcp gracious_driscoll
[root@k8s129 centos_ssh]# spa
6:ssh 鏈接222端口,看是否能進入容器(容器的root密碼是123456)
[root@k8s129 centos_ssh]# ssh root@192.168.6.129 -p 222
The authenticity of host '[192.168.6.129]:222 ([192.168.6.129]:222)' can't be established.
ECDSA key fingerprint is SHA256:xP6/H8rCv0vwW9B9FJxCjw/a5yejteFtINNxt886f1s.
ECDSA key fingerprint is MD5:f7:73:97:9c:96:6c:09:9f:f8:c4:d5:57:c5:07:08:54.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.6.129]:222' (ECDSA) to the list of known hosts.
root@192.168.6.129's password:
[root@c5bbc44d2ac7 ~]# hostname
c5bbc44d2ac7
[root@c5bbc44d2ac7 ~]#
總結:
dockerfile構建docker鏡像,三部曲:
1:編寫dockerfile
2:docker build 構建鏡像
3:啓動容器測試
======擴展=========
dockerfile 啓動多個服務
[root@k8s129 centos_ssh]# vim dockerfile
FROM centos:latest
RUN yum install -y openssh-server httpd
RUN yum install -y net-tools
RUN yum install -y passwd
RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
RUN sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
RUN sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
RUN echo "123456"|passwd --stdin root
ADD init.sh /init.sh # 把dockerfile所在目錄,init.sh copy到容器的init.sh,在init.sh裏面啓動httpd和sshd -D
CMD ["/bin/bash","/init.sh"]
編寫:vim init.sh
#!/bin/bash
systemctl start httpd
/usr/sbin/sshd -D
[root@k8s129 centos_ssh]# ls
dockerfile init.sh
[root@k8s129 centos_ssh]#
以後構建:
docker build -t dockerfile_centos:v2.1 .