1.新建 Dockerfile 文件
1.在 Dockerfile 目录下,新建一个名为 Dockerfile 文件:
[root@VM_0_6_centos src]# pwd
/usr/local/src
[root@VM_0_6_centos src]# mdkir Dockerfile
[root@VM_0_6_centos src]# ls
Dockerfile
[root@VM_0_6_centos src]# cd Dockerfile/
[root@VM_0_6_centos Dockerfile]# touch Dockerfile
[root@VM_0_6_centos Dockerfile]# pwd
/usr/local/src/Dockerfile
[root@VM_0_6_centos Dockerfile]# ls
Dockerfile
[root@VM_0_6_centos Dockerfile]# vi Dockerfile
2.在Dockerfile 文件内添加以下内容:
From nginx18:v1
CMD /usr/local/webserver/nginx/sbin/nginx
FROM:定制的镜像都是基于 FROM 的本地镜像,这里的 nginx 就是定制需要的基础镜像。后续的操作都是基于 nginx。
RUN:用于执行后面跟着的命令行命令。RUN 是在 docker build 时运行。
CMD: 类似于 RUN 指令,用于运行程序,但二者运行的时间点不同。CMD 在docker run 时运行。
2.开始构建nginx:latest镜像
docker build -t nginx:latest .
以下示例,通过目录下的 Dockerfile 构建一个 nginx:latest(镜像名称:镜像标签)。
注:最后的 . 代表本次执行的上下文路径。
[root@VM_0_6_centos Dockerfile]# ls
Dockerfile
[root@VM_0_6_centos Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx18 v1 828b659f7a77 2 days ago 487MB
centos latest 0d120b6ccaa8 13 days ago 215MB