Docker install and configure Nginx service

Nginx

Docker has two fork versions: Docker CE and Docker EE, the Community Edition and the Enterprise Edition. This tutorial installs Docker CE based on CentOS 7.

environment

  • CentOS 7
  • Docker 20.10.10

Install Nginx

pull image

docker pull nginx


View mirror

docker images

Start a temporary nginx container

docker run -p 80:80 -p 443:443 --name nginx -d nginx

Create a configuration directory to mount

mkdir -p /mydata/nginx/conf

Set configuration directory permissions

sudo chmod -R 777 /mydata/nginx

Copy the configuration of the Nginx container

注意:将Nginx容器映射到本机目录下,这一步必须要操作,否则Nginx容器无法启动

Copy the nginx directory in the nginx container to the local /mydata/nginx/conf directory

docker container cp nginx:/etc/nginx /mydata/nginx/conf

Move the nginx directory and move all the files of that directory to the conf directory

mv /mydata/nginx/conf/nginx/* /mydata/nginx/conf/

Delete the redundant /mydata/nginx/conf/nginx directory

rm -rf /mydata/nginx/conf/nginx

Copy the html directory and copy all the files in the Nginx directory to the local nginx directory

docker container cp nginx:/usr/share/nginx/html /mydata/nginx/

Remove the temporary nginx container

# 停止运行 nginx 容器
docker stop nginx

# 删除 nginx 容器
docker rm nginx

Start the nginx container

docker run -p 80:80 -p 443:443 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf/:/etc/nginx \
-d nginx

View Nginx container

docker ps

Set up Nginx to start with Docker

docker update nginx --restart=always

Test Nginx

  • If you are a cloud server, then directly access your public IP

  • If you are running Linux as a virtual machine, access it directly with the IP address of your Linux host

Command to view IP address

ifconfig

test nginx

Tutorial is over!

Guess you like

Origin blog.csdn.net/qq_31762741/article/details/121806994