構建自己的dockerfile
一)創建自己的CentOS
構建dockerfile的原理圖
Docker Hub中99%的鏡像都是從FROM scratch這個基礎鏡像過來的。然後配置需要的軟體和配置來進行構建。
FROM #基礎鏡像、一切從這裡開始構建
MAINTAINER #鏡像作者資訊 姓名+郵箱
RUN #鏡像構建時需要運行的命令
EXPOSE #暴露埠配置
WORKDIR #鏡像的工作目錄
ENY #用來在構建鏡像過程中設置環境變數
ADD #添加壓縮包、如添加tomcat鏡像、這個tomcat壓縮包。add可以解壓添加tar包
COPY #類似add、拷貝文件和目錄到鏡像中。copy不可以解壓tar。
VOLUME #掛載的目錄
CMD #指定這個容器啟動的時候將要運行的命令 cmd ehco”6666″。只有最後一個會生效、可被替代。
ENTRYPOINT #指定這個容器啟動的時候將要運行的命令、可以追加命令。設置容器的入口程式。
ONBUILD #當構建一個被繼承dockerfile這個時候就會運行onbuild指令、為觸髮指令。
二)對比DockerHub的原CentOS
構建自己的mycentos、對比之前從DockerHub上pull下來的原生的centos、多了vim 和 network命令後、容量也變大了
docker build -f dockerfile -t mycentos:0.1 .(注意後面有個點.)
[root@xiaozhang1999 ~]# cd /home
[root@xiaozhang1999 home]# ll
total 0
drwxr-xr-x 2 root root 37 Jun 24 15:27 ceshi
drwxr-xr-x 2 root root 25 Jun 28 15:51 docker-test-volume
drwxr-xr-x 4 root root 30 Jun 25 14:55 mysql
[root@xiaozhang1999 home]# mkdir dockfile
[root@xiaozhang1999 home]# ll
total 0
drwxr-xr-x 2 root root 37 Jun 24 15:27 ceshi
drwxr-xr-x 2 root root 25 Jun 28 15:51 docker-test-volume
drwxr-xr-x 2 root root 6 Jun 29 14:04 dockfile
drwxr-xr-x 4 root root 30 Jun 25 14:55 mysql
[root@xiaozhang1999 home]# cd dockfile
# 編寫dockerfile文件
[root@xiaozhang1999 dockfile]# vim dockerfile
[root@xiaozhang1999 dockfile]# cat dockerfile
FROM centos
MAINTAINER ztsq<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUn yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "end"
CMD /bin/bash
# 構建鏡像
[root@xiaozhang1999 dockfile]# docker build -f dockerfile -t mycentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 300e315adb2f
Step 2/10 : MAINTAINER ztsq<[email protected]>
---> Running in 8cddf9155f19
Removing intermediate container 8cddf9155f19
---> f11b3a959525
Step 3/10 : ENV MYPATH /usr/local
---> Running in 8ed0f33e60ac
Removing intermediate container 8ed0f33e60ac
---> 8681a8a7cf17
Step 4/10 : WORKDIR $MYPATH
---> Running in 171c056e7c56
Removing intermediate container 171c056e7c56
---> 4263a8ec2187
Step 5/10 : RUN yum -y install vim
---> Running in bc36de56fec5
# 查看自己構建的mycentos
[root@xiaozhang1999 dockfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 a4023f177785 13 minutes ago 295MB
# 啟動mycentos
[root@xiaozhang1999 dockfile]# docker run -it a4023f177785 /bin/bash
# 驗證有vim命令
[root@dadc1e42f05f local]# vim
# 工作目錄為workdir指定的.而不是根目錄/。
[root@dadc1e42f05f local]# pwd
/usr/local
構建成功
Successfully built a4023f177785
Successfully tagged mycentos:0.1
對比之前原生的centos
查看鏡像的變更歷史:docker history containerid