Teach you how to use Docker to deploy a SpringBoot project from 0 to 1 with pictures and texts [latest version]

foreword

Docker is a very popular container technology now, which can help us quickly deploy a project and environment (such as: mysql database, mq, etc.)

This article will lead you to deploy a SpringBoot project from 0. After reading it, you can deploy your project and amaze your friends!

Directory

Basic environment deployment

SpringBoot jar package export

dockerfile writing

Common commands for dockerfile

Build and deploy the project

Configure mirror sources (may be required)

build deploy test


Basic environment deployment

When we deploy a SpringBoot project, we often need other environments (such as mysql, redis, etc.), if your Docker has not installed other environments, you can refer to this article

Quickly use Docker to install and deploy various common databases and software on the server (bug-free version, complete configuration and various software installations in 10 minutes)

You can complete the installation of the corresponding environment according to your needs. to support the program.

SpringBoot jar package export

Here I have prepared the simplest springboot interface, as shown in the figure

Will use this interface to test later

After the packaging is complete, you can see our products in the catalog

Next, our product has come out and needs to be deployed in docker.

dockerfile writing

ps: If we don’t use docker for deployment, how should we deploy it?

If docker is not used, we can execute the java command in the directory where the jar package is located to start the project.

Requires your server or virtual machine to have a java environment

java -jar xxx.jar

If deployed in this way, once you close the background window, the program will also end. So need to use

nohup java -jar xxx.jar &

Let the springboot program run in the background.

If you want to kill this process or close the execution of the jar package, you need to use

Close jar package execution
ps aux|grep xxx.jar to find out the pid
kill -9 [pid] close the process

Then let’s get to the point, how to deploy using docker?

First of all, you need to write dockerfile, which can be written in windows or created in linux, the effect is the same. Here is a template dockerfile for springboot project deployment. Note: the dockerfile does not require a suffix

# Set the JAVA version
FROM java:8-alpine
# Specify the storage volume, any information written to /tmp will not be recorded to the container storage layer
VOLUME /tmp
# Copy and run the JAR package
ADD demo.jar app.jar
# Set the JVM running parameters, here limit the memory size to reduce overhead
ENV JAVA_OPTS="\
-server\
-Xms256m\
-Xmx512m\
-XX:MetaspaceSize=256m \
-XX:MaxMetaspaceSize=512m"
#Empty parameters, convenient for passing parameters when creating containers
ENV PARAMS=""
# Entry point, execute the JAVA run command
ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS app.jar $PARAMS"]

There are many parameters and keywords in Dockerfile. Other commonly used keywords are shown here. (If you just want to deploy, you can skip this section of the form)

dockerfile common commands

keyword function note
FROM Specify the parent image Specify the dockerfile to build based on that image
MAINTAINER Author information is used to indicate who wrote this dockerfile
LABEL label is used to indicate the label of dockerfile You can use Label instead of Maintainer. In the end, you can view it in the basic information of docker image
RUN Execute a command Execute a command Default is /bin/sh format: RUN command or RUN [“command” , “param1″,”param2”]
CMD Container startup command Provide the default command when starting the container and use it with ENTRYPOINT. The format is CMD command param1 param2 or CMD [“command” , “param1″,”param2” ]
ENTRYPOINT Entrance Generally, it will be used in making some containers that are executed and closed
COPY Copy files Copy files to image during build
ADD Add files When building, adding files to image is not limited to the current build context can come from remote services
ENV Environmental variables Specify the environment variables at the time of build can pass -e override format ENV name=value
when starting the container ARG Build parameters Build parameters are only used when building. If there is ENV, the value of the same name of ENV will always override the parameter of arg
VOL UME Define external data volumes that can be mounted Specify the build image and those directories that can be mounted to the file system when starting the container Use -v binding format VOLUME [“Directory”]
EXPOSE Exposure port Define the port to monitor when the container is running to start the use of the container -p to bind exposed port format: EXPOSE 8080 or EXPOSE 8080/udp
WORKDIR Working directory Specify inside the container If the working directory is not created, it will be created automatically. If the specified / uses an absolute address, if it does not start with /, then it is a relative path to the path of the previous workdir
USER Specify the execution user Specify the user when the build or startup is executed in RUN CMD ENTRYPONT
HEALTHCHECK Health Check The command to specify the health monitoring of the current container is basically useless because many times the application itself has a health monitoring mechanism
ONBUILD Trigger When there is an image with the ONBUILD keyword as the base image, the ONBUILD command will be executed after the execution of FROM is completed, but it does not affect the current image and is not very useful
STOPSIGNAL Send semaphore to host The STOPSIGNAL directive sets the syscall signal that will be sent to the container to exit.
SHELL Specify the shell to execute the script Specify the shell used when RUN CMD ENTRYPOINT executes the command

Build and deploy project

We need to upload the Dockerfile and our jar package to the same directory of the server.

docker build -t demo .

Use this command to build a mirror, (demo after -t is the mirror name) Note that there is a . behind it, which means building under the current folder. If you want to specify a folder, you can use . as the corresponding folder , such as ./demo

Configure mirror source (may be required)

If you get the following error when building:

Then it may be that the docker image you configured does not have this java:8-alpine, and you may need to add some image sources in the daemon.json file. See also the article I cited above.

1. Edit the file on the host machine:

vim /etc/docker/daemon.json

2. Add the following content in the modified file

{
    "registry-mirrors": [
        "https://ustc-edu-cn.mirror.aliyuncs.com",
        "http://hub-mirror.c.163.com",
        "https://registry.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn"
    ]
}

3. Restart docker (this command is the command of centOS7, other operating systems are other restart commands)

systemctl restart docker

Build Deployment Test

After the configuration is complete, execute the build command again. We can see that

This completes the build. Check out our mirror

In addition to the demo itself, there is also a java image at the beginning of the dockerfile to provide a jdk environment.

Finally we need to run the image. implement

docker run -d \
--name=demo\
-p 8080:8080\
demo

This completes our deployment work. Let’s test it again.

It can be seen that the interface can also be accessed normally.

If you cannot access after deployment, you can check the log to see if the service starts normally.

Check the problem again according to the log. If the startup is normal, but cannot be accessed. You need to check whether server operator’s security group and server firewall have opened this port.

Finally, you can take a look at my open source project: ijida Campus (similar to a Weibo positioned as a campus)

The ijida campus software server is based on SpringCloud Alibaba micro-service components and some distributed technologies to realize the association and collaboration between services and separate front-end and back-end projects. It is planned to realize the synchronization between the WeChat applet and the app.

The technology stack used is: Spring Boot, Spring Cloud Alibaba, rabbitMQ, JWT, minIO, mysql, redis, ES, docker, Jenkins, mybatis-plus

The front end will be written using WeChat applets.

Welcome to participate in open source contributions and star projects!

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108682 people are studying systematically