本站文章总数为:165
Search Posts

Dockerfile基础镜像centos7上部署nginx并自启动

内容纲要

Dockerfile内容

因为是前后端的 还有java 所以还有很多多余的命令,大家看nginx的部分就好了

FROM centos:7
#作者
MAINTAINER qiuf<qiufuzi@gmail.com>
RUN rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN yum -y install dos2unix mysql net-tools rsync vim kde-l10n-Chinese nginx
RUN mkdir -p /usr/local/java&&mkdir -p /usr/local/share/java/&&mkdir -p /gldata1/server/lv2
ADD jdk-8u202-linux-x64.tar.gz /usr/local/java
ADD lib.tar.gz /usr/local/lib
ADD zmq.tar.gz /usr/local/share/java/
ADD html.tar.gz /usr/share/nginx/html/
ADD mime.types /etc/nginx/
ADD default.conf  /etc/nginx/conf.d/
EXPOSE 80
ADD lv2 /gldata1/server/lv2/
ADD start.sh /
ENV JAVA_HOME=/usr/local/java/jdk1.8.0_202
ENV CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
ENV PATH $JAVA_HOME/bin:$PATH
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
RUN ldconfig
#设置时区
ENV TIME_ZONE Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime
#设置中文支持
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
RUN echo LANG= " zh_CN.UTF-8 " >>/etc/locale.conf
ENV LC_ALL=zh_CN.UTF-8
#RUN echo "daemon off;" >> /etc/nginx/nginx.conf 
CMD ["sh","-c","/usr/sbin/nginx -g 'daemon off;'"]

关键是最后一行来之不易,容器启动后nginx的自启动

CMD ["sh","-c","/usr/sbin/nginx -g 'daemon off;'"]

最开始是写脚本但是写了一个nginx.sh,内容为

#!/bin/bash
# 启动nginx服务
cd /usr/sbin/
./nginx
while [[ true ]];do
  sleep 1
done

然后dockerfile最后一行变为下面的几个版本

ADD nginx.sh /
CMD ["sh","-c","/nginx.sh"]
```shell
CMD ["/usr/sbin/nginx","-c","/etc/nginx/nginx.conf"]
```shell
CMD ["/usr/sbin/nginx","-c","/etc/nginx/nginx.conf"]

启动后进容器并无nginx进程
百度原理后
看到文章https://www.cnblogs.com/JulianHuang/p/15753732.html

为什么要加 nginx -g "daemon off;"
这句话是什么意思?

在常规的虚机上,nginx默认是以守护进程来运行的(daemon on),在后台默默提供服务,同时部署多个ngxin服务也不会相互干扰。

nginx -g directives: set global directives out of configuration file.

在容器环境,one container == one process,容器要能持续运行,必须有且仅有一个前台进程,所以对nginx进程容器化,需要将nginx转为前后进程( daemon off)。

我们能顺利执行docker run nginx,启动容器并不退出,是因为nginx的官方镜像Dockerfile 已经指定 nginx -g "daemon off;"

再回到上文,为什么此处脚本中要加"nginx -g "daemon off;" 呢?

If you add a custom CMD in the Dockerfile, be sure to include -g daemon off; in the CMD in order for nginx to stay in the foreground, so that Docker can track the process properly (otherwise your container will stop immediately after starting)!为什么要加 nginx -g "daemon off;"
这句话是什么意思?

在常规的虚机上,nginx默认是以守护进程来运行的(daemon on),在后台默默提供服务,同时部署多个ngxin服务也不会相互干扰。

nginx -g directives: set global directives out of configuration file.

在容器环境,one container == one process,容器要能持续运行,必须有且仅有一个前台进程,所以对nginx进程容器化,需要将nginx转为前后进程( daemon off)。

我们能顺利执行docker run nginx,启动容器并不退出,是因为nginx的官方镜像Dockerfile 已经指定 nginx -g "daemon off;"

再回到上文,为什么此处脚本中要加"nginx -g "daemon off;" 呢?

If you add a custom CMD in the Dockerfile, be sure to include -g daemon off; in the CMD in order for nginx to stay in the foreground, so that Docker can track the process properly (otherwise your container will stop immediately after starting)!

所以我修改为以下几个版本,都不好使,容器正常存活,nginx无法访问

CMD ["nginx", "-g", "daemon off;"]
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
CMD ["nginx -g 'daemon off;'"]

最后看到 sh -c的定义

/bin/sh -c 的作用 
背景#
在kubernetes的yaml文件的command字段中,经常使用sh -c的形式:

command: ["/bin/sh", "-c", "echo postStart... > /usr/share/nginx/html/index.html"]

那么,/bin/sh -c 是什么意思呢?为什么不直接使用 echo 命令?

那是因为命令解释器(这里是/bin/sh) 有两种工作模式:交互模式和非交互模式。

1. 交互模式#
交互模式就是使用 ssh 连接到 Linux 服务器上,然后在终端上敲入命令就可以显示对应结果。这样与终端进行交互执行命令的方式就称为交互模式。

ls -l

2. 非交互模式#
非交互模式就是调用 bash 解释器,通过 bash -c 后接命令的形式来解释执行命令。

sh -c "ls -l"

而在 kubernetes 的 yaml 文件中,command 字段(底层也就是 golang 程序) 显然需要使用非交互模式,通过命令解释器来执行命令,而不是通过ssh登录之后与终端交互。

最后改为

CMD ["sh","-c","/usr/sbin/nginx -g 'daemon off;'"]

容器启动后nginx正常自启动
提取nginx相关dockerfile命令,只运行nginx的可以百度使用nginx官方容器基础镜像的dockerfile

FROM centos:7
#作者
MAINTAINER qiuf<qiufuzi@gmail.com>
RUN rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN yum -y install  nginx
EXPOSE 80
CMD ["sh","-c","/usr/sbin/nginx -g 'daemon off;'"]

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注