Use docker-compose to deploy SpringBoot project & nginx to deploy front-end

Install Docker

Automatic download

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

Check if the installation is successful

docker -v

image-20231031135739923

Configuring the mirror warehouse

Replace the image accelerator (Alibaba Cloud is recommended)

Container Image Service (aliyun.com)

Scan the QR code to log in and find the mirror warehouse

Everyone is different, copy your own mirror warehouse

image-20230618171351652

After copying the above address, edit it into daemon.json

vi /etc/docker/daemon.json

image-20231031140340869

Press ESC and use :wq to save

Start docker

systemctl start docker

Install docker-compose

Installation command line

curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/ docker-compose

Set file permissions

chmod + x /usr/local/bin/docker-compose

View version

docker-compose -v

image-20231031140815313

Writing Dockerfile

Dockerfile can convert the jar package packaged by our project into an image

Here I put dockerfile and docker-compose in the project package. The location here can be arbitrary. Finally, it needs to be uploaded to the server.

image-20231031141103201

File content

The file content of Dockerfile is

# Set jdk version
FROM java:8

# Set our personal information, you can write whatever you want according to your needs
MAINTAINER daqi <[email protected]>

# These two lines are to set the time in our container and the jvm time (the default is not GMT)
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone

# The former [Esurvey-backend-1.1.jar] is the name of the jar package we uploaded (the jar package packaged in the target directory), and the latter [springboot.jar] is our newly named jar package
ADD Esurvey-backend-1.1.jar springboot.jar

# After the image is started as a container, the port exposed to the outside world
EXPOSE 9996

# Image initialization memory when creating a container, maximum memory, and profile used when starting. -c is to clear previously started data
ENTRYPOINT ["java","-Xms1024m","-Xmx1024m","-jar","/springboot.jar","--spring.profiles.active=prod\ ","-c"]

illustrate:

**FROM java:8:** This is a FROM instruction, specifying the base image as java:8, which is the image that contains the Java 8 environment

**MAINTAINER daqi [email protected]:**This is a MAINTAINER instruction that specifies the author and contact information of the image

**RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime: **This is a RUN instruction that executes a command and saves the result to the image. This command copies the Asia/Shanghai time zone file to the /etc/localtime file and sets the time zone in the container to Asia/Shanghai.

**ADD Esurvey-backend-1.1.jar springboot.jar: **This is an ADD instruction that copies a local or remote file or directory into the image. This command copies the Esurvey-backend-1.1.jar file to the image and renames it to springboot.jar. If you only need to copy local files or directories, it is recommended to use the COPY instruction instead of the ADD instruction.

**EXPOSE 9996:** This is an EXPOSE directive that declares the port that the container is listening to when running. This directive does not actually open the port, it is just a documented effect that requires using the -p or -P parameter to map the port when running the container. This command declares the container to listen to port 9996

**ENTRYPOINT [“java”,”-Xms1024m”,”-Xmx1024m”,”-jar”,”/springboot.jar”,”–spring.profiles.active=prod”,”-c”]: **This It is an ENTRYPOINT directive that defines the executable program and parameters to be run when the container starts. This command can be used in conjunction with the CMD command. The parameters in the CMD command will be appended to the ENTRYPOINT command. This command defines the java program to run when the container starts, and sets parameters such as memory, jar package, and profile.

Write docker-compose file

Introduction

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YAML files to configure all the services your application needs, and then create and start them all with a single command. Docker Compose can help you simplify container management and achieve rapid deployment, expansion and upgrade of services.

The main functions of Docker Compose are as follows:

  • Create and start multiple containers at once to achieve multi-service collaboration for applications
  • Use YAML files to define service configurations to avoid repeatedly entering command line parameters
  • Isolate different application environments through project names to avoid conflicts between containers, networks, and volumes
  • Provides a series of commands to manage the life cycle of containers, such as building, starting, stopping, restarting, destroying, etc.
  • Supports the use of variables and templates to parameterize and reuse configuration files

File content

Find a place to create the file docker-compose.yml

# Specify our docker-compose version
version: "3"

# Set up our service
services:

  # Configure mysql service
  mysql:

    # Specify the mysql image file here. The mysql version is 8.0.27. Here you need to use the docker command to pull the image.
    image: mysql:8.0.27

    # Configure container name
    container_name: esurvey-mysql

    # Map the 9995 port of our host to the 3306 port in the container. Because the network in the container is not interconnected with the outside world, only the host can access our container, so we can use the 9995 port of the host. Go to access the mysql of the container
    ports:
      - "9995:3306"

    # Configure the environment, mainly to configure the login password of our mysql. When the mysql container is created, it can be accessed by different IP addresses by default, so there is no need to configure it here.
    environment:
      - MYSQL_ROOT_PASSWORD=mysql2023

    # Configure the mounted folder, create the mysql folder under the current folder, which contains conf, logs, and data, and mount them to conf.d, logs, and mysql in the container mysql respectively, so that if our container does not , but our data is still there
    volumes:
      - ./mysql/conf:/etc/mysql/conf.d
      - ./mysql/logs:/logs
      - ./mysql/data:/var/lib/mysql

  #Configure redis service
  redis:

    # Set the image corresponding to the container, same as above
    image: redis:6.2.7

    #Set container name
    container_name: esurvey-redis

    # Setting command, here is used to set our redis login password. The root is the password. You can fill it in according to your needs.
    command: redis-server --requirepass redis2023

    # Same as above, as port mapping
    ports:
      - "9994:6379"

    # Same as above, mount the folder
    volumes:
      - ./redis:/data

  #Configure springboot service
  springboot:
    # To generate an image, use . It will automatically go to the current directory to find a Dockerfile file, and then generate an image based on the configuration inside.
    build: .

    #Specify the name and version number of the image we generated
    image: springboot:v1.1

    #Specify container name
    container_name: Survey-backend

    # Same as above, do port mapping
    ports:
      - "9996:9996"

    # Write clearly the services that our springboot depends on, including mysql and redis. The names here are service names.
    depends_on:
      - mysql
      - redis

Modify application-prod.yml

spring.profiles.active is used in the project to control the project environment. Here we modify some connection configurations on the application-prod.yml production environment.

  1. Modify the url of mysql to server ip:port
  2. Similarly, the host of redis is also changed to ip, and the port must be the same as configured in docker-compose.yml above.

Note: You can try the access methods of msyql:9995 and redis:9994. I did not access successfully. It may be because I am not in the same network. You can also try it.

image-20231031142742762

Use docker-compose to run containers

Use xhell to connect to the server (you can also use other remote connections), and place the packaged jar package, Dockerfile, and docker-compose in the folder

Taking me as an example, I put them under /home/server/Esurvey/Esurvey-backend. The directory structure is as follows

image-20231031143302680

The redis and mysql files are generated after running the container and map the data volumes in the container.

Go to the current directory

image-20231031143558846

run

docker-compose up -d

image-20231031143629359

In this way, the back-end java project will run successfully.

Front-end project Nginx deployment

Upload files

Similarly, upload the packaged files to the server

In my case, I passed it to /home/server/Esurvey/Esurvey-app

image-20231031143956291

Note that the request prefix in the front-end project is removed. We access it through nginx reverse proxy.

Install Nginx

I won’t go into details here about how to install it using the command line.

I use Pagoda to install nginx and install it in the software mall

Configuration file modification

Modify the content under http in the file

Pay attention to the location of the server. In http, port 9998 is monitored here. The homepage file is index.html, and the file path is the upload path.

location ^~ /api/ matches the /api/ path and then proxies it to http://127.0.0.1:9996/api/. This address is the backend address.

server {<!-- -->
  listen 9998;
  server_name xxxx.top;
  index index.html;
  root /home/server/Esurvey/Esurvey-app;
  
  location ^~ /api/ {<!-- -->
    proxy_pass http://127.0.0.1:9996/api/;
    add_header 'Access-Control-Allow-Origin' $http_origin;
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers '*';
    if ($request_method = 'OPTIONS') {<!-- -->
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Origin' $http_origin;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}
}

Reload nginx, the configuration will take effect, and the page can be accessed.