Docker10_2:DockerFile构建自己的centos
查看官方centos dockerfile
dockerhub查看dockerfile:
https://registry.hub.docker.com/_/centos?tab=description
点击就会跳转到GitHub
FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20201113" \
org.opencontainers.image.title="CentOS Base Image" \
org.opencontainers.image.vendor="CentOS" \
org.opencontainers.image.licenses="GPL-2.0-only" \
org.opencontainers.image.created="2020-11-13 00:00:00+00:00"
CMD ["/bin/bash"]
查看镜像的构建步骤
#查看镜像的构建步骤
docker history 镜像id
[root@VM-0-3-centos pdx_haokai]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 3 months ago 231MB
[root@VM-0-3-centos pdx_haokai]# docker hostory 5d0da3dc9764
docker: 'hostory' is not a docker command.
See 'docker --help'
[root@VM-0-3-centos pdx_haokai]# docker history 5d0da3dc9764
IMAGE CREATED CREATED BY SIZE COMMENT
5d0da3dc9764 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
[root@VM-0-3-centos pdx_haokai]#
可以看到FROM scratch,基础中的基础
编写自己的centos dockerfile
# 指定基础镜像
FROM centos
# 作者信息
MAINTAINER HaoKAi:2051574911@qq.com
# 配置环境变量
ENV MYPATH /usr/local/haokai
# 指定工作目录,进入容器的默认目录
WORKDIR $MYPATH
# 构建shell运行命令
RUN yum -y install vim
RUN yum -y install net-tools
# 声明容器运行的端口
EXPOSE 80
CMD echo $MYPATH
CMD echo "-----end-----"
CMD /bin/bash
将dockerfile打成镜像
docker build -f mydockerfile-centos -t mycentos:0.1 .
# -f dockerfile路径
# -t 指定镜像版本,TAG
# 结尾的.别少了
[root@VM-0-3-centos dockerffile]# docker build -f mydockerfile-centos -t mycentos:0.2 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 5d0da3dc9764
Step 2/10 : MAINTAINER HaoKAi:2051574911@qq.com
---> Using cache
---> 40c675b13c5e
Step 3/10 : ENV MYPATH /usr/local/haokai
---> Using cache
---> 3efe715863de
Step 4/10 : WORKDIR $MYPATH
---> Using cache
---> 119f5684dd91
Step 5/10 : RUN yum -y install vim
---> Using cache
---> 01d2b9f72af0
Step 6/10 : RUN yum -y install net-tools
---> Using cache
---> f78af1cf5fb0
Step 7/10 : EXPOSE 80
---> Using cache
---> 364eaf4bd204
Step 8/10 : CMD echo $MYPATH
---> Running in 524b7660d576
Removing intermediate container 524b7660d576
---> ecfbdac1256a
Step 9/10 : CMD echo "-----end-----"
---> Running in 6a5a75cac983
Removing intermediate container 6a5a75cac983
---> 33b9df0d5189
Step 10/10 : CMD /bin/bash
---> Running in 62de21edfbcf
Removing intermediate container 62de21edfbcf
---> 62d31e32190c
Successfully built 62d31e32190c
Successfully tagged mycentos:0.2
[root@VM-0-3-centos dockerffile]#
成功运行
Successfully built b0d4ce749f91
Successfully tagged mycentos:0.1
查看镜像
可以看到pwd的目录是我们的工作目录
[root@VM-0-3-centos pdx_haokai]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 b0d4ce749f91 40 minutes ago 326MB
centos latest 5d0da3dc9764 3 months ago 231MB
[root@VM-0-3-centos pdx_haokai]#
[root@VM-0-3-centos pdx_haokai]# docker run -it --name centos01 mycentos:0.1
[root@44c48f65f1f1 haokai]# pwd
/usr/local/haokai