Home >Operation and Maintenance >Nginx >How to use Docker to mount volumes to deploy Nginx
nginx has many functions, such as forward proxy, reverse proxy, load balancing, transparent proxy, etc. Generally, reverse proxy is used more often. Proxies and load balancing. We only need to synchronize the configuration file locally to complete these operations. To create an nginx configuration file, you first need to create a new file named nginx.conf in the /etc/nginx/ directory
nginx1 directory also needs nginx.conf Create, just leave the content empty. Next, check whether the image is normal:
If you have not downloaded the image yet, use the following command to pull the image (default is the latest version):
docker pull nginx
Next run Container:
docker run -itd -p 80:80 --name nginx001 -v /docker/nginx1/nginx.conf:/etc/nginx/nginx.conf nginx
Explain:
-itd: Turn on interactive mode, simulate terminal, run in the background
Run After the above command, a random string will be returned to indicate that the container is successfully created:
Check the running container:
If you don’t see nginx001, there must be an error during running. Let’s take a look at the error message:
It probably means that the events node was not found in the configuration file. Because the content of the file we created is empty, when starting the container, the nginx.conf in the corresponding directory is directly mapped to nginx.conf in nginx001. Of course, nginx with an empty configuration cannot run.
Now vi just created nginx.conf locally and start configuring:
Before I deployed a simple .net core program on docker, Occupying local port 1500, now I use nginx reverse proxy to the program to a specified domain name and access it through port 80. After the configuration file is written, restart the container nginx001:
Now nginx001 has been successfully started. Let’s access the domain name just specified:
nginx configuration has taken effect.
Finally, let’s try to see if restarting docker will cause configuration initialization. Restart docker:
Start two containers:
Visit page:
The data volume has been successfully applied because the database has not been initialized after restarting Docker. In the future, you will no longer be afraid of docker crashes and data loss.
Supplement
The above is the detailed content of How to use Docker to mount volumes to deploy Nginx. For more information, please follow other related articles on the PHP Chinese website!