Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker to mount volumes to deploy Nginx

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you how to use Docker mount volume deployment Nginx, the content is easy to understand, clear, hope to help you solve doubts, the following let me take you to study and learn "how to use Docker mount volume deployment Nginx" this article.

Nginx data volume

Nginx has many functions, such as forward proxy, reverse proxy, load balancing, transparent proxy and so on. Generally speaking, reverse proxy and load balancing are commonly used. These operations only need to be done by modifying the configuration file, so we just need to synchronize the configuration file locally. The path to the nginx configuration file is etc/nginx/nginx.conf. First, create the corresponding directories and files:

The nginx.conf under the nginx1 directory should also be created, and the content is empty. Next, check whether the image is normal:

If the image has not been downloaded, use the following command to pull the image (the latest version by default):

Docker pull nginx

Next, run the container:

Docker run-itd-p 80:80-name nginx001-v / docker/nginx1/nginx.conf:/etc/nginx/nginx.conf nginx

Explain:

-itd: enable interactive mode, simulate the terminal, and run in the background

-p 80:80: the corresponding port is mapped to the host

-- name nginx001: custom container name

-v / docker/nginx1/nginx.conf:/etc/nginx/nginx.conf: map the corresponding file of the host to the configuration file of nginx

Nginx: name of the image running

After running the above command, a random string is returned to indicate that the container has been created successfully:

View the containers that are running:

I didn't see nginx001. There must have been a mistake at run time. 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, we directly map the nginx.conf in the corresponding directory to the nginx.conf in the nginx001. Of course, the nginx with empty configuration cannot be run.

Now the nginx.conf that vi has just created locally, start to configure:

I previously deployed a simple .net core program on docker, occupying local port 1500, but now I reverse proxy the program to a specified domain name through nginx and access it through port 80. After the configuration file is written, restart the container nginx001:

Now that nginx001 is launched successfully, let's visit the domain name you just specified:

The configuration of nginx is already in effect.

Finally, let's try whether docker restart will cause configuration initialization and restart docker:

Start two containers:

Visit the page:

The database is not initialized as a result of docker restart, which proves that the volume is valid. In the future, I am no longer afraid of docker downtime and data loss.

Supplement

According to the proxy_pass attribute in the nginx configuration file, localhost or 127.0.0.1 can take effect normally when it is configured in a non-docker environment, but when the nginx container is configured with localhost or 127.0.0.1 under docker, there is a small pit that prevents the actual reverse proxy from being completed. Because when the container is running, docker will assign it a separate ip and port, and then map the ip occupied by docker to the container ip (for example, after mysql starts under docker, you can log in to the client using localhost). However, when nginx starts, it will read the configuration file first and run it only if it is configured correctly, so docker will map the local ip into it after the container runs successfully. If the configuration file is written as localhost or 127.0.0.1, then only the ip that accesses the container directly will trigger the reverse proxy. So if you use docker to configure nginx on the server, enter the server ip directly to complete the configuration.

The above is about "how to deploy Nginx using Docker mount volume". If this article is helpful and well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report