Docker configure Nginx reverse proxy

Article directory

  • 1. Deploy the microprogram into docker
    • 1.1 dockerfile
    • 1.2 Create docker image based on customized dockerfile
    • 1.3 Create container
    • 1.4 Testing
  • 2. Install Nginx in docker
    • 2.1 Install Nginx image
    • 2.2 Obtain the Nginx configuration file and synchronize it to the specified location on the host computer
      • Install nginx container
      • Delete nginx container
    • 2.3 Install Nginx container and mount data
    • 2.4 Testing
  • 3. Reverse proxy
      • 3.1 Configuration
      • 3.2 Testing
  • refer to:

1. Deploy micro-programs into docker

1.1 dockerfile

Start building from the basic image openjdk, where the openjdk image includes The most basic environment for running Java and tomcat, debian system

# Base image
FROM openjdk:11.0-jre-buster
# Set time zone to prevent time confusion when viewing database logs
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime & amp; & amp; echo $TZ > /etc/timezone
#Copy jar package
COPY helloworld-0.0.1-SNAPSHOT.war /app.jar
# Entrance When starting the container, the java -jar /app.jar command will be executed.
ENTRYPOINT ["java", "-jar", "/app.jar"]
  • helloworld-0.0.1-SNAPSHOT.war is my springboot simple microprogram package. The main functions are:

    image-20231107142935894

    image-20231107144317567

1.2 Create a docker image based on a custom dockerfile

Enter the directory:

image-20231106235420797

Execute the instruction. . here refers to the directory where the Dockerfile file is located.

docker build -t docker-demo .

image-20231107144449803

1.3 Create container

docker run -d --name dd -p 8088:8080 docker-demo

image-20231107144522215

1.4 Test

Enter in the host browser: http://localhost:8008/hello

image-20231107152049096

2. Install Nginx in docker

2.1 Install Nginx image

docker pull nginx

image-20231107145100710

2.2 Obtain the Nginx configuration file and synchronize it to the specified location on the host computer

Install nginx container

docker run --name nginxdemo -p 8880:80 -d nginx

image-20231107145516785

Copy the container nginx configuration file to the host computer (the storage directory here is: E:\DockerContainersTest\\
ginx
)

docker cp nginx:/etc/nginx/nginx.conf E:\DockerContainersTest\\
ginx #nginx default configuration folder
docker cp nginx:/etc/nginx/conf.d E:\DockerContainersTest\\
ginx #nginx configuration folder
docker cp nginx:/usr/share/nginx/html E:\DockerContainersTest\\
ginx\html #nginx’s html folder

image-20231107150153427

It is best to create the directory in advance here: E:\DockerContainersTest\\
ginx

Delete nginx container

docker stop nginxdemo
docker rm nginxdemo

image-20231107150708887

At this point, you have obtained the nginx configuration file in the container and saved it to the designated location on the host computer. The main purpose of this step is to facilitate the mounting of the data volume. Later modifications to the configuration can be done directly on the host computer without having to on the container.

2.3 Install Nginx container and mount data

docker run --name nginxproxy -d -p 8880:80 -v E:\DockerContainersTest\\
ginx\\
ginx.conf:/etc/nginx/nginx.conf -v E:\DockerContainersTest\\
ginx \conf.d:/etc/nginx/conf.d -v E:\DockerContainersTest\\
ginx\html:/usr/share/nginx/html -v E:\DockerContainersTest\\
ginx\cert: /etc/nginx/cert -v E:\DockerContainersTest\\
ginx\logs:/var/log/nginx -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 --privileged=true - -restart=always nginx

image-20231107151620978

image-20231107151648317

2.4 Test

Directly modify the nginx default welcome page Nginx\html\index.html on the host machine

image-20231107152512975

After restarting the container, access it on the host machine

image-20231107153335508

3. Reverse proxy

Specific idea: Access the Nginx port through the browser on the host, and Nginx sends the request to the container equipped with the microprogram. The container then returns the return data to Nginx, and Nginx returns the data to the host.

image-20231107155500696

The routing address of the container with the microprogram is: 172.17.0.1, and the direct IP address is: 172.17.0.3

I have run into a trap here. When setting the address to a direct IP address, a 502 routing error occurs

3.1 Configuration

Modify Nginx proxy configuration: Nginx\conf.d\default.conf

image-20231107155825232

New configuration:

 location /hello {
        proxy_pass http://172.17.0.1:8008;#Reverse proxy configuration, forward the request to the specified service
    }

Here 172.17.0.1 and 8008 are the routing address and externally exposed port number of the dd container respectively.

image-20231107160112172

Restart the Nginx container to make the configuration take effect

3.2 Test

image-20231107160234602

Reference:

https://blog.csdn.net/lly576403061/article/details/129452986

https://www.bilibili.com/video/BV13a411q753/?p=182

https://www.bilibili.com/video/BV1HP4118797/?p=12