The environment

  • docker
  • Nginx container

Configure the nginx proxy

If you don’t know the basics of creating nginx containers, you can read this blog: Nginx (1) — Docker creates nginx containers

1. Edit default.conf, which I mapped to the host’s/usr/local/docker/nginx/config/conf.d/default.conf

vi /usr/local/docker/nginx/config/conf.d/default.conf
Copy the code

As follows:

# will be treated as forward/springboot - service request to http://127.0.0.1:8081/springboot-service # IP cannot be set to 127.0.0.1 here, because this is in nginx containers, So 127.0.0.1 is the IP address of the nginx container, # and the project to be proxied is running on the host, So here's IP address should be your hosting IP location/springboot - service {proxy_pass http://ip:8888/springboot-service; Forwarded_for $proxy_add_X_forwarded_for ($forwarded_forwarded_for); proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; }Copy the code

2. Modify the Application. yml file of the Springboot project and add the following content

servlet:
  context-path: /springboot-service
Copy the code

Check the nginx configuration file and run it again

docker exec nginx nginx -t
docker exec nginx nginx -s reload
Copy the code

The requested interface must be specified with the reverse proxy path /springboot-service, which corresponds to location /springboot-service

Package the SpringBoot project

1, use maven command package, edit the package command

Then copy the packaged JAR to the server, I stored in the server/TMP /test/docker-springboot directory.

Because the host does not have JDK installed, so there is no Java program running environment, and I want to be able to container run, so I will put the SpringBoot project into the JDK container to run, JDK container contains JDK, that is, Java project running environment.

2. Create a Dockerfile file in the/TMP /test/docker-springboot directory

cd /tmp/test/docker-springboot
vi Dockerfile
Copy the code

Dockerfile contains the following contents:

# Base image FROM OpenJDK: 8-JDK -alpine # author information MAINTAINER "**@qq.com" # EXPOSE port 8888, do not write default and keep the same as the project port # EXPOSE Jar = jar = jar = jar = jar = jar App.jar ENTRYPOINT [" Java ","-jar","/app.jar"]Copy the code

3. Package the springBoot project image

Docker build-t springboot-app:1.0Copy the code

4. View the mirror

docker images
Copy the code

You can see that the springboot-app image already exists

5. Create a container

Docker run -d --name springboo-app -p 8888:8888 Springboot-app :1.0 docker psCopy the code

6. Interface test

Open your browser and enter:http://ip/springboot-service/user/index You can see that the reverse proxy deployment is successful

7. Let the project run as soon as it is turned on

docker update --restart=always springboot-app
Copy the code