[Docker] Production mirror and run Nginx

surroundings

  • Operating system (cat / etc / redhat-release): CentOS Linux release 7.6.1810 (Core)
  • Docker:18.09.6

file

  • Dockerfile
FROM nginx:1.14-alpine
LABEL maintainer="chenjo <[email protected]>"

ENV NGX_DOC_ROOT="/data/web/html/"

ADD index.html ${NGX_DOC_ROOT}
ADD entrypoint.sh /bin/

CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
ENTRYPOINT ["/bin/entrypoint.sh"]
#以下写法在实验中均不行,容器一旦运行就立马退出
#ENTRYPOINT ["/bin/sh", "-c", "/bin/entrypoint.sh"]
#ENTRYPOINT ["/bin/sh", "-c", "/bin/sh"]
  • entrypoint.sh
[root@appsvr img3]# cat entrypoint.sh
#!/bin/sh
cat > /etc/nginx/conf.d/www.conf << EOF
server {
        server_name ${HOSTNAME};
        listen ${IP:-0.0.0.0}:${PORT:-80};
        root ${NGX_DOC_ROOT:-/usr/share/nginx/html/};
}
EOF

exec "$@"
  • index.html
<h1>New Doc for Nginx</h1>

Related Commands

# 生成镜像
docker build -t myweb3:v0.3-1 ./
# 运行容器
docker run --name myweb --rm -P myweb3:v0.3-1
# 访问 Nginx 的默认主页
curl 10.0.0.2
# 通过另一个终端访问该容器
docker exec -it myweb /bin/sh
# 查看主机名
cat /etc/nginx/conf.d/www.conf
# 通过主机名访问自定义的主页
wget -O - -q 6a04e5550bce

Key Points

CMD is passed as a parameter ENTRYPOINT, just for this case, it is equivalent to /bin/entrypoint.sh /usr/sbin/nginx -g 'daemon off;'.
In entrypoint.sh in exec "$@", $ @ represent all the incoming parameters, exec represent the child process so that the PID of the parent process and, instead, the parent process exits. Here, the parent process is the shell.

Guess you like

Origin www.cnblogs.com/chenjo/p/10935965.html