文章最后更新时间:2025年06月12日
前言:
Dockerfile 是用于编写 Docker 镜像生成过程的文件,包含特定语法。在包含 Dockerfile 的文件夹中执行 docker build -t name:tag .
即可构建镜像。制作镜像需理解镜像分层概念,镜像由多层组成,理论上最多 127 层。
查看镜像层数的方法:
docker history
命令(只看 SIZE 大于 0 的行):[root@localhost docker_images]# docker history centos_nginx:1.20 IMAGE CREATED CREATED BY SIZE COMMENT ac20103ed758 3 days ago /bin/sh -c #(nop) CMD ["bin/sh" "-c" "ngin… 0B ec1fd1e45ed5 3 days ago /bin/sh -c #(nop) EXPOSE 80 0B 58618fd69d9c 3 days ago /bin/sh -c #(nop) WORKDIR /usr/local/nginx/ 0B 2da69b9e24e2 3 days ago /bin/sh -c #(nop) ENV PATH=/usr/local/sbin:… 0B 52ae7bfba773 3 days ago /bin/sh -c ./configure --prefix=/usr/local/n… 27.9MB db85c3ef27c6 3 days ago /bin/sh -c #(nop) WORKDIR /usr/local/nginx-1… 0B 52b0a917d776 3 days ago /bin/sh -c #(nop) ADD file:8c96cbcb0fc1f4a79… 6.4MB 930032c3aa9b 3 days ago /bin/sh -c ./configure && make && make insta… 9.69MB 47f1e266ba57 3 days ago /bin/sh -c #(nop) WORKDIR /usr/local/pcre-8.… 0B 03a8be5705b9 3 days ago /bin/sh -c #(nop) ADD file:e5f245127035870b4… 9.05MB f629d8acca91 3 days ago /bin/sh -c yum -y install make zlib zlib-dev… 374MB 6c9f58d58724 3 days ago /bin/sh -c #(nop) MAINTAINER LDX<xxx.163.co… 0B 25c1784b5045 10 days ago bash 548B time calibration 0c0f2dcf7afd 15 months ago RUN /bin/sh -c yum install wget -y && mv… 64.5MB buildkit.dockerfile.v0 <missing> 15 months ago MAINTAINER "author=yangdejun update=2021-08-… 0B buildkit.dockerfile.v0 <missing> 2 years ago /bin/sh -c #(nop) CMD ["bin/bash"] 0B <missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 2 years ago /bin/sh -c #(nop) ADD file:72b194edf3abedf51… 203MB
docker inspect
命令(查看 RootFS 中的 Layers 字段):[root@localhost docker_images]# docker inspect ac20103ed758 "RootFS": { "Type": "layers", "Layers": [ "sha256:fb82b029bea0a2a3b6a62a9c1e47e57fae2a82f629b2d1a346da4fc8fb53a0b6", "sha256:c0cd7924335682967ab4689ff4a93cbabca6a29a856b59c8afe9bd19f50acef9", "sha256:199048ec1cd0a30e395c8576bc50fb41521a828da6d254d6cb87fcdeb87cbf2e", "sha256:7d14007007c4116285020229b0b80ca905c9e5da3e02f4420aab9794187a3ba5", "sha256:54bbee18d5e57c57dee2fd9dc78190ff73a9332ff74ea2caa03d038aabd59a7b", "sha256:ff5a2abea1cc7a2b998f497e372a6c2dd31ad1fac0eea080702876887964816e", "sha256:d94be22f19c5704ba8aa0504ddf23a635f2f73f1f02f201027b9af9f74800d80", "sha256:ba35802c1d21bb162e940819c067629fcf619d397b378847cfaa6b13512f8f79" ] }, "Metadata": { "LastTagTime": "2022-11-04T11:50:22.594297543+08:00" } } ]
Dockerfile 指令简介:
指令 | 说明 |
---|---|
FROM |
基于哪个镜像构建新镜像 |
MAINTAINER |
镜像维护者信息 |
RUN |
构建时执行的命令 |
CMD |
容器运行时执行的命令 |
VOLUME |
指定容器挂载点 |
WORKDIR |
设置工作目录 |
EXPOSE |
声明服务端口 |
ENV |
设置环境变量 |
ADD/COPY |
拷贝文件到容器(ADD 支持解压/下载) |
任务环境:
基于 centos7.8 系统安装 Nginx 服务。
一、准备官方 centos7.8 镜像并校准时间
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
martonyang/centos7.8.2003 latest 0c0f2dcf7afd 14 months ago 268MB
[root@localhost ~]# docker run -it martonyang/centos7.8.2003 bash
[root@ec22e09ae181 /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
校准时区:
[root@localhost ~]# docker run -it --name centos7 martonyang/centos7.8.2003 bash
[root@f3a8b1015053 /]# date
Fri Oct 28 08:11:27 UTC 2022
[root@f3a8b1015053 /]# mv /etc/localtime /root/localtime.bak
[root@f3a8b1015053 /]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@f3a8b1015053 /]# date
Fri Oct 28 16:12:23 CST 2022
提交新镜像:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3a8b1015053 martonyang/centos7.8.2003 "bash" 4 minutes ago Up 4 minutes centos7
# 提交新镜像
[root@localhost ~]# docker commit -m="time calibration" -a="ldx" f3a8b1015053 centos:7.8
sha256:25c1784b504586e43b895a751280950a34e5bb33a632fb96f37c7cee82b10345
[root@localhost ~]# docker run --name centos7.8 -it 25c1784b5045 bash
# 时间准确
[root@b1874719f4d7 /]# date
Fri Oct 28 16:17:53 CST 2022
二、新建目录并编写 Dockerfile
注意:
Dockerfile 指令每执行一次会新建一层,避免无意义分层导致镜像膨胀。
[root@localhost ~]# mkdir /docker_images
[root@localhost ~]# cd /docker_images/
# 准备安装包
[root@localhost docker_images]# ls
nginx-1.20.2.tar.gz pcre-8.35.tar.gz
[root@localhost docker_images]# vim Dockerfile
# 基于该镜像构成新的镜像
FROM centos:7.8
# 作者维护信息
MAINTAINER LDX<xxx.163.com>
# 安装依赖包
RUN yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
# 导入并解压pcre-8.35文件
ADD pcre-8.35.tar.gz /usr/local/
# 切换到pcre-8.35目录,类似于CD效果
WORKDIR /usr/local/pcre-8.35
# 编译安装
RUN ./configure && make && make install
# 导入并解压nginx-1.20文件
ADD nginx-1.20.2.tar.gz /usr/local/
# 切换到nginx-1.20.2目录
WORKDIR /usr/local/nginx-1.20.2
# 编译安装
RUN ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35 && make && make install
# 设置环境变量
ENV PATH $PATH:/usr/local/nginx/sbin
# 切换到nginx目录
WORKDIR /usr/local/nginx/
# 展示80端口
EXPOSE 80
# 使得nginx运行在前台,所有信息直接输出控制台
CMD nginx -g 'daemon off;'
启动命令注意事项:
Dockerfile 启动命令 | docker run 启动结果 |
---|---|
CMD nginx |
端口无映射 |
CMD nginx -g 'daemon off;' |
端口正常映射 |
CMD ['nginx'] |
端口无映射 |
CMD ["nginx", "-g", "daemon off;"] |
端口正常映射 |
三、根据 Dockerfile 构建新 Nginx 镜像
# docker build -t 新镜像的名字:版本号 . (注意有一个点)
docker build -t centos_nginx:1.20 .
四、基于新镜像创建并运行 Nginx 容器
[root@localhost docker_images]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e687e521a1a centos_nginx:1.20 "/bin/sh -c 'nginx -…" 2 minutes ago Up 2 minutes 0.0.0.0:49153->80/tcp, :::49153->80/tcp naughty_sammet
效果图:
文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。