Docker deployment of Java Web project based on SpringBoot

Recently, I saw that servers on didi cloud are very cheap, 2G for one core, only 68 yuan for one year. Here is my process of deploying Java Web services based on Docker. At present, the cheapest server I have ever seen is not so cheap when alicloud discounts. Start decisively. If necessary, you can buy it through the following link.
Didi cloud's full line of standard cloud servers is time limited and preferential. The new cloud service package is 50% off in one month, 40% off in three months and as low as 30% off in six months. Sign up and send 2200 yuan as a gift bag for beginners. Get on the train at a speed. Click to view:

Java project description

Java project is based on SpringBoot and built with Maven, which is a common springbtoo web project, without any special configuration.

It includes configuring nginx requests and ssl certificates.

The domain name is applied for in Alibaba cloud, the certificate is also applied for in Alibaba cloud, and the server is purchased in didi cloud.

Install docker

Install and configure permissions

  • When using ordinary users to operate docker, an error will be reported. It's really troublesome to switch to the root user every time. So set the permissions here so that ordinary users can use docker. The principle is to add the current user to the docker group, refresh the permissions, and restart docker to use it.
# Install docker
sudo apt-get install
# View permission issues
ll /var/run/docker.sock
# Add the current user to the docker group
sudo usermod -aG docker ${USER}
# Update docker group
sudo newgrp docker
# Restart the service and log in the user again
sudo systemctl restart docker

Setting up alicloud acceleration for image

  • This can be configured according to the needs. If you do not configure it, it will not affect. If you configure the Alibaba cloud image, you will download the image faster.
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
  "registry-mirrors": [""]
sudo systemctl daemon-reload
sudo systemctl restart docker

Creating docker container based on mysql

  • When entering the Mysql container, you need to add the parameter LANG=C.UTF-8, otherwise, there will be garbled code when querying data.
docker pull mysql
# Create a docker container based on mysql. The default password is password
docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:5.7
# Enter Mysql container
docker exec -it mysql env LANG=C.UTF-8 /bin/bash
# Enter mysql console
mysql -uroot -ppassword

Creating docker container based on maven

  • When creating a container, remember to add the -- rm parameter, so that the container will be deleted automatically after maven builds the project, and will not occupy space. After the construction, the container is not needed at all.
docker pull maven
# Create path, java project exists
mkdir -p $HOME/projects
# Project directory / home / DC2 user / projects / x-lidaoyuan
# Create data volumes to share maven data
docker volume create --name maven-repo
# View data volume
docker volume ls
# Create container based on maven, use data volume Maven repo, compile the project, and generate jar package in the project directory
docker run -it --rm --name my-maven -v maven-repo:/root/.m2 -v "$HOME/projects/x-lidaoyuan":/usr/src/mymaven -w /usr/src/mymaven maven:3.6.3-jdk-8 mvn clean package

Creating docker container based on Java

  • Note that the port number of my java project is 7878, and the open port of the container also needs to be set to 7878.
  • When java commands start a project, remember to add a & symbol at the end to make the project run in the background.
docker pull java:8
# Create container with port set to 7878
docker run -it --name liaoliaoba -p 7878:7878 -v "$HOME/projects":/root/projects java:8 
# Enter the container (the first creation will automatically enter, this line of command is not required)
docker exec -it liaoliaoba env LANG=C.UTF-8 /bin/bash
# And start the project
java -Dfile.encoding=UTF-8 -jar /root/liaoliaoba/liaoliaoba-api/target/liaoliaoba-api-0.0.1-SNAPSHOT.jar &

Creating docker container based on nginx

  • All my configurations are placed under the project directory liaoliaoba/config
  • Listen to port 80 of http and port 443 of https
docker pull nginx
# Create a docker container based on nginx. The configuration file is placed in the conf.d directory
docker run -it --name nginx -p 80:80 -p 443:443 -v "$HOME/projects/x-lidaoyuan/x-liaoliaoba/config/nginx/html":/usr/share/nginx/html -v "$HOME/projects/x-lidaoyuan/x-liaoliaoba/config/nginx/conf.d":/etc/nginx/conf.d -v "$HOME/projects/x-lidaoyuan/x-liaoliaoba/config/nginx/cert":/etc/nginx/cert -d nginx

Configuration file under conf.d


  • Port number 7878 is the startup port of my java project. This port also needs to be opened in the Java container to access the Java project through this port
  • is the internal IP address of java container, which can be viewed through docker inspection liaoliaoba
  • . pem and. key are free certificates applied for through Alibaba cloud. The validity period is only one year
  • Other configurations are basically default
upstream tomcat_llba {
server {
    listen       443 ssl;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    ssl_certificate /etc/nginx/cert/;   #Replace domain name.pem with the filename of your certificate.
    ssl_certificate_key /etc/nginx/cert/;   #Replace domain name.key with the key file name of your certificate.
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #Use this encryption suite.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #Use this protocol for configuration.
    ssl_prefer_server_ciphers on;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

    location /llba/ {
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://tomcat_llba;

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;

    # proxy the PHP scripts to Apache listening on
    #location ~ \.php$ {
    #    proxy_pass;

    # pass the PHP scripts to FastCGI server listening on
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #location ~ /\.ht {
    #    deny  all;

Creating a Redis based Docker container

docker pull redis
docker run -itd --name redis -p 6379:6379 redis

Finishing the script

  • Note that you can configure the key when you pull the project through git, so that you don't need to enter the user name and password every time you pull it.

Create Java program startup script

java -Dfile.encoding=UTF-8 -Duser.timezone=GMT+08 -jar /root/projects/x-lidaoyuan/x-liaoliaoba/target/x-liaoliaoba-1.0.0-SNAPSHOT.jar &

Create Java program

kill `jps -l | grep x-liaoliaoba | awk '{print $1}'`

Restart script


Create public and private keys on the host

# Press enter three times in a row to generate the public and private keys, and then put the public key '' on GitHub.
ssh-keygen -t rsa -C ""
git config --global "examplePC"
git config --global ""
# Check whether the configuration is effective
git config -l

Host script into `

# Switch to directory
cd /home/dc2-user/projects/liaoliaoba
# Pull mirror image
git pull
# Construction project
docker run -it --rm --name my-maven -v maven-repo:/root/.m2 -v "$HOME/projects/x-lidaoyuan":/usr/src/mymaven -w /usr/src/mymaven maven:3.6.3-jdk-8 mvn clean package
# Enter the java container and restart the project
docker exec -it liaoliaoba env LANG=C.UTF-8 /bin/bash

If you like, you can pay close attention to the personal public number.

Tags: Linux Docker Java Nginx Maven

Posted on Sat, 11 Jan 2020 23:41:15 -0800 by jariizumi