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:
1.2 Create a docker image based on a custom dockerfile
Enter the directory:
Execute the instruction. .
here refers to the directory where the Dockerfile file is located.
docker build -t docker-demo .
1.3 Create container
docker run -d --name dd -p 8088:8080 docker-demo
1.4 Test
Enter in the host browser: http://localhost:8008/hello
2. Install Nginx in docker
2.1 Install Nginx image
docker pull nginx
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
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
It is best to create the directory in advance here: E:\DockerContainersTest\\
ginx
Delete nginx container
docker stop nginxdemo docker rm nginxdemo
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
2.4 Test
Directly modify the nginx default welcome page Nginx\html\index.html
on the host machine
After restarting the container, access it on the host machine
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.
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
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.
Restart the Nginx container to make the configuration take effect
3.2 Test
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