编写Dockerfile来构建nginx:latest镜像
Posted LiuJun2Son
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写Dockerfile来构建nginx:latest镜像相关的知识,希望对你有一定的参考价值。
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
[root@VM_0_6_centos Dockerfile]# cat Dockerfile
From nginx18:v1
CMD /usr/local/webserver/nginx/sbin/nginx
[root@VM_0_6_centos Dockerfile]# docker build -t nginx:latest .
Sending build context to Docker daemon 2.048kB
Step 1/2 : From nginx18:v1
---> 828b659f7a77
Step 2/2 : CMD /usr/local/webserver/nginx/sbin/nginx
---> Running in 9f911177d905
Removing intermediate container 9f911177d905
---> d589942fb7ff
Successfully built d589942fb7ff
Successfully tagged nginx:latest
[root@VM_0_6_centos Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d589942fb7ff 6 seconds ago 487MB
nginx18 v1 828b659f7a77 3 days ago 487MB
centos latest 0d120b6ccaa8 13 days ago 215MB
[root@VM_0_6_centos Dockerfile]#
3.运行nginx:latest镜像
docker run -itd -p 9090:80 --name nginx nginx:latest
执行run命令时,会自动启动 nginx 服务器
[root@VM_0_6_centos Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM_0_6_centos Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d589942fb7ff 2 minutes ago 487MB
nginx18 v1 828b659f7a77 3 days ago 487MB
centos latest 0d120b6ccaa8 13 days ago 215MB
[root@VM_0_6_centos Dockerfile]# curl localhost:9090
curl: (7) Failed to connect to ::1: No route to host
[root@VM_0_6_centos Dockerfile]# docker run -itd -p 9090:80 --name nginx nginx:latest
b255c5fa0293c71c6cdfe6741d011889f0c51402b3f3d7a20c20794b1527fd9c
[root@VM_0_6_centos Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b255c5fa0293 nginx:latest "/bin/sh -c /usr/loc…" 2 minutes ago Up 2 minutes 0.0.0.0:9090->80/tcp nginx
[root@VM_0_6_centos Dockerfile]#
4.验证Nginx是否已启动
curl localhost:9090
[root@VM_0_6_centos Dockerfile]# curl localhost:9090
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@VM_0_6_centos Dockerfile]#
5.将nginx:latest镜像保存成 tar 归档文件
docker save -o nginx.tar nginx:latest
-o :输出到的文件。
[root@VM_0_6_centos images-bak]# pwd
/var/lib/docker/images-bak
[root@VM_0_6_centos images-bak]# ls
centos8.tar
[root@VM_0_6_centos images-bak]# docker save -o nginx.tar nginx:latest
[root@VM_0_6_centos images-bak]# ls
centos8.tar nginx.tar
[root@VM_0_6_centos images-bak]#
以上是关于编写Dockerfile来构建nginx:latest镜像的主要内容,如果未能解决你的问题,请参考以下文章
如何编写 Dockerfile 来运行 Python3 + PyQt5
linux12 - dockerfile01 -->构建镜像指令与编写案例