版权声明:本文为博主原创文章,未经博主允许欢迎转载,但请注明出处。 https://blog.csdn.net/liumiaocn/article/details/82251463
这篇文章整理一下根据nginx官方镜像进行自定义相关的注意事项。
nginx的CMD
查看一下nginx官方镜像的dockerfile,无论是alpine方式还是普通方式,CMD的设定都是这样的:
CMD [“nginx”, “-g”, “daemon off;”]
使用daemon off的方式将nginx运行在前台保证镜像不至于退出
为何需要修改官方镜像
nginx的使用虽然官方镜像将设定文件等都可以挂出来,但是在实际的使用中往往会有些实际的使用方式,往往需要定制一些设定或者动态进行设定。比如我们希望动态的修改nginx镜像运行起来之后的反向代理的设定,而这些设定是通过环境变量传入,在容器生成阶段会有所不同,当然可以通过对nginx.conf进行设定,结合nginx reload等来实现。
一般用的比较多的方式,则是在CMD使用自定义的启动程序或者脚本,通过进行预先设定,设定完毕之后再调用nginx -g daemon off则能将nginx服务启动起来。
事前准备
[root@liumiaocn nginx]# cat Dockerfile
FROM nginx:1.15
ADD start-nginx-server.sh /usr/local/bin/
CMD ["/usr/local/bin/start-nginx-server.sh"]
[root@liumiaocn nginx]# cat start-nginx-server.sh
#!/bin/sh
nginx -g "daemon off"
[root@liumiaocn nginx]# chmod 755 start-nginx-server.sh
[root@liumiaocn nginx]#
构建&启动
构建nginx镜像
[root@liumiaocn nginx]# docker build -t nginx:test .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM nginx:1.15
---> 06144b287844
Step 2/3 : ADD start-nginx-server.sh /usr/local/bin/
---> 9fc838fb4455
Removing intermediate container 154f34ce7202
Step 3/3 : CMD /usr/local/bin/start-nginx-server.sh
---> Running in 79ee81f4285c
---> 614634c88135
Removing intermediate container 79ee81f4285c
Successfully built 614634c88135
[root@liumiaocn nginx]#
可以看到成功构建,但是却无法启动nginx服务
[root@liumiaocn nginx]# docker run -p 8848:80 nginx:test
2018/09/08 14:13:20 [emerg] 5#5: unexpected end of parameter, expecting ";" in command line
nginx: [emerg] unexpected end of parameter, expecting ";" in command line
[root@liumiaocn nginx]#
对应方法
上面提示缺少”;”,很有可能是使用方式导致nginx.conf出现问题。所以直接修改nginx.conf将daemon off的方式写入,算是一种暂定对应方法。
[root@liumiaocn nginx]# vi Dockerfile
[root@liumiaocn nginx]# cat Dockerfile
FROM nginx:1.15
ADD start-nginx-server.sh /usr/local/bin/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
CMD ["/usr/local/bin/start-nginx-server.sh"]
[root@liumiaocn nginx]# vi start-nginx-server.sh
[root@liumiaocn nginx]# cat start-nginx-server.sh
#!/bin/sh
nginx
[root@liumiaocn nginx]#
构建镜像&运行
[root@liumiaocn nginx]# docker build -t nginx:test .
Sending build context to Docker daemon 3.072 kB
Step 1/4 : FROM nginx:1.15
---> 06144b287844
Step 2/4 : ADD start-nginx-server.sh /usr/local/bin/
---> b0cb6ec8150f
Removing intermediate container 96bb7731968a
Step 3/4 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
---> Running in 2a504d56afd7
---> 887b5f391096
Removing intermediate container 2a504d56afd7
Step 4/4 : CMD /usr/local/bin/start-nginx-server.sh
---> Running in fbff5cc58c0f
---> dfba70dacc0a
Removing intermediate container fbff5cc58c0f
Successfully built dfba70dacc0a
[root@liumiaocn nginx]#
[root@liumiaocn nginx]# docker run -p 8848:80 nginx:test
通过使用curl或者浏览器访问
[root@liumiaocn ~]# curl http://localhost:8848
<!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@liumiaocn ~]#
从nginx的日志和上述的返回结果可以看到其已经开始正常工作
[root@liumiaocn nginx]# docker run -p 8848:80 nginx:test
172.17.0.1 - - [08/Sep/2018:14:32:33 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"