Linux server deploys docker version of nginx (nginx containerization)

For the sake of server security, the nginx port is not directly exposed. Now the server uses the docker version of nginx. Record the steps of use. The docker installation will not be described anymore. It was written before

Divided into 2 types applicable to the situation that the server can access the Internet and cannot access the Internet

A sh file and dockerfile written here are used to directly pull the nginx image of the online warehouse after docker starts

build_and_run.sh file content:

#!/bin/sh

echo "准备发布"
echo "\n"

echo "停掉旧容器..."
docker stop zw-nginx 
echo "完成\n"

echo "删除旧容器..."
docker rm zw-nginx 
echo "完成\n"

echo "删除旧镜像..."
docker image rm zw-nginx:latest
echo "完成\n"

echo "构建新镜像..."
docker build --rm -t zw-nginx:latest . -f nginx.dockerfile
echo "完成\n"

echo "启动新版本..."
docker run -d \
       --name zw-nginx \
	   -p 443:443 \
	   -p 8889:8889 \
	   -v /home/www:/home/www \
	   -v /home/nginx/conf.d:/etc/nginx/conf.d \
       --restart=always zw-nginx:latest
echo "完成\n"


Among them, 443 and 8889 are listenable ports after nginx starts

nginx.dockerfile file content:

from nginx:latest
MAINTAINER yushoji@gmail.com
ENV TZ=Asia/Shanghai \
    DIR=/etc/nginx

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
	&& echo $TZ > /etc/timezone

WORKDIR $DIR
CMD ["nginx", "-g", "daemon off;"]

Servers that can access the Internet can directly execute the build file to automatically pull nginx from the warehouse.
Servers that cannot access the Internet need to import the nginx image in advance. The import command: sudo docker load < nginx_latest.tar
image generation steps have been described before.

Guess you like

Origin blog.csdn.net/weixin_42857718/article/details/132037096