Docker learning notes 1 – deploy the. Net core 3.1 project to the docker container and use nginx reverse proxy (centos7) (2)


Next, we will continue with the previous sectionDocker learning notes – deploy the. Net core 3.1 project to the docker container and use nginx reverse proxy (centos7) (1)

Demonstration steps:

1. Get the nginx image and run the nginx container

2. Generate the image file according to the uploaded project, and run the project container

3. Configure the mapping of nginx reverse proxy and map it to the project container to realize the reverse proxy

4. Final display effect

Step 1: get the nginx image and run the nginx container

To run a container, you first need to have a corresponding image. Use the following command to pull the nginx image: if you do not specify a version number, the latest version is pulled by default

docker pull nginx

After obtaining the nginx image, you can run the nginx container according to the image. Use the following command to run the container

docker run --name=nginx -d -p 4043:80 nginx

The above command is interpreted as follows:

  1. –Name: set the name of the container.
  2. -d: Indicates that the container runs in the background.
  3. -p: Specifies the port mapping. 4043 is the host port for external access, and 80 is the internal port of nginx container.
  4. Nginx: indicates that the container is run according to the nginx image.
  5. Run means the running container. If the image does not exist, it will automatically call the pull command to pull the image

The above figure shows that the nginx container is already running, and accessing port 4043 through external access is successful

Additional supplement: if you are installing on alicloud server, you need to configure the security group, as shown in the following figure:

Step 2: generate the project image file and run the project container

First find the project file directory, enter the project root directory, and then use the following command to generate the image

Ls -- view local file list
Enter the netemo root directory
Docker build - t myapidemo

Explanation of the above command:

  1. -t: To give the image a name, for example: myapidemo
  2. The “.” in the last English status of the command cannot be omitted, which means to search for the dockerfile file in the current directory

Through the command docker images to view the currently running image, you can see that the image above has been created successfully, and the dependent image is automatically created.

After creating the image, use the following command to run the container

docker run --name=myapidemo -d -p 9020:80 myapidemo


1. Container name: myapidemo

2. Open port: 9020

As can be seen from the above figure, the container has been running successfully. Up to now, the project can be accessed remotely through port 9020, as follows: call API project interface:

However, for security, the production environment does not allow such direct access to the container, so you need to use nginx to proxy access!

Step 3: configure nginx related mapping and map to project container to realize reverse proxy

First of all, enter the nginx container and check that the default configuration of nginx is Shenma content. Use the following command to enter the container

docker exec -it nginx /bin/bash


  1. -It: to assign a pseudo terminal.
  2. Nginx: represents the name of the container, and the container ID can also be used here.
  3. /Bin / bash: indicates bash operations on the container.

If you see the error in the figure above, it indicates that your nginx container is not running. Use the command docker start nginx to start it!

Under normal circumstances, as shown in the figure below, you will find that the terminal account name has changed from the original @ centos7 to @ xxxxx5b, indicating that it has entered the container

Use the command exit to exit the container

Nginx will be installed under the etc directory by default. Through the combination of the command ls and the command CD, find down layer by layer!

Edit using the VIM command nginx.conf The document, at this time bond found that it was wrong!

The reason is that the VIM command needs to be installed. The system itself is not installed. You can directly execute the following command to update and install:

Apt get update -- update source first

Apt get install - y VIM -- install VIM

Note: the above two commands are not installed with the yum command, but with the apt get command!

Use the VIM command again to edit nginx.conf file

Look at the content in the last red box, include / etc / nginx / conf.d / *. Conf, which means that the conf file under the conf.d directory is used by default for configuration.

Next, our mapping content will also be expanded for the conf.d directory. You can see that there is a default.conf File!


The purpose of the so-called mapping is to hide the real local directory on the server, and use the user-defined alias for external access!

First of all, create some custom file storage directory of nginx on the server to hide the real directory or file!

Execute the following command to create three file directories: WWW, logs and conf

mkdir -p /root/nginx/www /root/nginx/logs /root/nginx/conf

View nginx container ID

Copy the configuration file under the conf.d directory in nginx container to local, and CP means copy

docker cp ea37c8eaef5b:/etc/nginx/conf.d/default.conf /root/nginx/conf

The picture above shows that it has been copied successfully!

To configure reverse proxy for NETCORE container, you need to know the IP address of NETCORE project container first. Therefore, before modifying the configuration, you need to obtain the IP address of myapidemo container.

docker inspect -f '{{range . NetworkSettings.Networks }}{{. IPAddress}} {end}} 'container name or container iddocker inspect container name

Additional supplement: by default, the container is connected to the bridge mode bridge;

If the current container is not connected to other custom bridges, the IP address under the default bridge will be used;

If the current container is connected to other custom bridges, the corresponding IP address under the custom bridge can be used and the container name can be viewed through the command docker inspect;

For example, I created a custom bridge: test_ Network, and the current container is also connected to the bridge, then use the IP under the bridge instead of the default one;

How to check which bridge the current container is connected to? The following section shows:Docker learning notes — net core project container connecting to MSSQL container (environment: centos7)

Then use the VIM command to edit default.conf File (under this directory / root / nginx / CONF), set the file contents as follows:

    listen 80;
    charset utf-8;

    location / {
       proxy_redirect default;

Because the container is internal access, the port set is 80 instead of the external port 9020 set when the container is running. After the modification, the: WQ saves and exits

Now re create a new nginx container named nginxapi, and map the WWW, logs, conf directory to the local, and set the external port to 4030

docker run -d -p 4030:80 --name nginxapi \
-v /root/nginx/www:/usr/share/nginx/html \
-v /root/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf \
-v /root/nginx/logs:/var/log/nginx \

As can be seen from the above figure, the new container is successfully created and running, and the port set for external setting is: 4030,

Now we access the project interface above us through port 4030, as shown in the figure below. The access is successful!

However, if you use the nginx container with no mapping set and port 4043 to access the interface, the return is < 404 not found > and

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]