Dockerfile格式基本命令
- FROM //指定基于哪个基础镜像
格式 FROM image:FROM centos(从docker仓库拉镜像)
或者 FROM tag:image - MAINTAINER(maintainer) //指定作者信息
格式 MAINTAIN name - RUN //镜像操作指令
格式为 RUN command
或者 RUN [“executable”, “param1”, “param2”] - CMD // 三种格式
CMD [“executable”, “param1”, “param2”]
CMD command param1 param2
CMD [“param1”, “param2”]
RUN和CMD看起来挺像,但是CMD用来指定容器启动时用到的命令,只能有一条
CMD ["/bin/bash", “/usr/local/nginx/sbin/nginx”, “-c”, “/usr/local/nginx/conf/nginx.conf”] - EXPOSE
格式为 EXPOSE […]
EXPOSE 22 80 8443 - ENV
格式 ENV key value
它主要是为后续的RUN指令提供一个环境变量,我们也可以定义一些自定义的变量 - ADD
格式 add src dest
将本地的一个文件或目录拷贝到容器的某个目录里。 其中src为Dockerfile所在目录的相对路径,它也可以是一个url - COPY
格式同add
使用方法和add一样,不同的是,它不支持url - ENTRYPOINT 格式类似CMD
容器启动时要执行的命令,它和CMD很像,也是只有一条生效,如果写多个只有最后一条有效
CMD不同是: CMD 是可以被 docker run 指令覆盖的,而ENTRYPOINT不能覆盖
例如: 我们在Dockerfile中指定如下CMD:
CMD ["/bin/echo", “test”]
启动容器的命令是 docker run aming 这样会输出 test
假如启动容器的命令是 docker run -it aming /bin/bash 什么都不会输出
ENTRYPOINT不会被覆盖,而且会比CMD或者docker run指定的命令要靠前执行 - VOLUME
格式 VOLUME ["/data"]
创建一个可以从本地主机或其他容器挂载的挂载点 - USER
格式 USER daemon
指定运行容器的用户 - WORKDIR
格式 WORKDIR /path/to/workdir
为后续的RUN、CMD或者ENTRYPOINT指定工作目录
Dockerfile示例-httpd
[root@localhost ~]# curl http://192.168.200.90:5000/v2/_catalog 访问仓库(查看私有仓库有什么镜像)
{“repositories”:[“nginx”]}
[root@localhost ~]# docker images 查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
centos_with_http latest 3b158573f1fa 3 hours ago 250MB
10.10.10.10:5000/nginx latest f949e7d76d63 14 months ago 126MB
192.168.200.90:5000/nginx latest f949e7d76d63 14 months ago 126MB
nginx latest f949e7d76d63 14 months ago 126MB
registry latest c9bd19d022f6 4 years ago 33.3MB
[root@localhost ~]# docker tag 3b158573f1fa 192.168.200.90:5000/centos:latest 打标签
[root@localhost ~]# docker push 192.168.200.90:5000/centos:latest 拉镜像
The push refers to repository [192.168.200.90:5000/centos]
1ed00c89b894: Pushed
2653d992f4ef: Pushed
latest: digest: sha256:c40d9e5f99e8a87e12f128ee4f401f2a7fa1f142073f2ee4771d4d84c9a01b14 size: 741
编辑dockerfile文件
[root@localhost ~]# vi Dockerfile
FROM 192.168.200.90:5000/centos:latest 指定基础镜像
MAINTAINER zuozhe 维护者信息
WORKDIR /root 工作目录,类似于cd命令
RUN yum install httpd net-tools -y 构建镜像时执行的命令
EXPOSE 80 指定于外界交互的端口
ADD start.sh /root 将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问网络资源
RUN chmod +x /root/start.sh
CMD /root/start.sh
[root@localhost ~]# vi start.sh 编辑脚本文件
#! /bin/bash
httpd
while true
do
sleep 1000
done
[root@localhost ~]# docker build --help
[root@localhost ~]# docker build -t centos_httpd . 制作镜像
Sending build context to Docker daemon 3.348GB
Step 1/8 : FROM 192.168.200.90:5000/centos:latest
—> 3b158573f1fa
Step 2/8 : MAINTAINER zuozhe
—> Running in d165ed9dd4bf
Removing intermediate container d165ed9dd4bf
—> 3ecea35c1d2c
Step 3/8 : WORKDIR /root
—> Running in d7abd0921e22
Removing intermediate container d7abd0921e22
—> 818ffe324f44
Step 4/8 : RUN yum install httpd net-tools -y
—> Running in 182740a703a4
Last metadata expiration check: 3:26:09 ago on Thu Nov 26 13:20:27 2020.
Package httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64 is already installed.
Package net-tools-2.0-0.52.20160912git.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
Removing intermediate container 182740a703a4
—> 3e03fc0dae43
Step 5/8 : EXPOSE 80
—> Running in 4170d537fdcc
Removing intermediate container 4170d537fdcc
—> dad3cda9fc93
Step 6/8 : ADD start.sh /root
—> 74bffc9df175
Step 7/8 : RUN chmod +x /root/start.sh
—> Running in 44be0be8e29d
Removing intermediate container 44be0be8e29d
—> cebe5e10caf0
Step 8/8 : CMD /root/start.sh
—> Running in 0157512ffc9d
Removing intermediate container 0157512ffc9d
—> a20cfa3241ea
Successfully built a20cfa3241ea
Successfully tagged centos_httpd:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
centos_httpd latest a20cfa3241ea 12 seconds ago 252MB
192.168.200.90:5000/centos latest 3b158573f1fa 3 hours ago 250MB
centos_with_http latest 3b158573f1fa 3 hours ago 250MB
192.168.200.90:5000/nginx latest f949e7d76d63 14 months ago 126MB
nginx latest f949e7d76d63 14 months ago 126MB
10.10.10.10:5000/nginx latest f949e7d76d63 14 months ago 126MB
registry latest c9bd19d022f6 4 years ago 33.3MB
[root@localhost ~]# docker run -itd --name centos_httpd -p 8888:80 centos_httpd:latest 启动容器
6735665f61aae8583bfdde43cdc7611176488602903ab27440d4cbf4c526f21e
打开浏览器,访问192.168.200.90:8888