Writing in the front

Click “Follow” and “like” if you feel like you’re getting something, and thank you for supporting Spring Boot’s flexible packaging options when deploying applications. Spring Boot applications can be deployed to a variety of cloud platforms, container images (such as Docker), or virtual machines/real machines. Here we explore SpringBoot deployed in Docker.

As we all know, in the process of Kubernetes micro-service practice, Docker image is definitely the most exposed. Kubernetes is what, I briefly explain here, Kubernetes micro service is simply a group of images between the combination and the relationship between the tune, so how to compile the image will make the service performance is better, so that the image construction, push, pull faster, so that it occupies less network resources. More detailed can consult, here will not do more detailed explanation.

The article directories

To prepare

This article mainly discusses the combination of SpringBoot and Docker, so I don’t want to spend too much time on Docker installation and SpringBoot common application construction. However, these are the preparation work before we explain this article. So HERE I post the article I have written before, if you do not install Docker and do not know how to build SpringBoot common application friends, you can jump to the past to see, I think it is written in detail. Docker installation tutorial: This article is the Ubuntu, CentOS, Windows mainstream system installation method in detail explained a time, including how to configure image acceleration and other content. Docker novice treasure book (necessary) : because the content will be behind the content to build DockerFile, so if you do not know what DockerFile is, you can take a look at this article. After reading this article, you should be ready to build a simple Hello World application.

  • A simple springBoot2. x program, which simply creates a Controller Controller, can be accessed at http://localhost/index, as shown below

  • I use version 19 of Docker
  • My server runs CentOS7

Build manually using DockerFile

Dockfile is a script that is interpreted by a Docker program. Dockerfile consists of a sequence of instructions, each of which corresponds to a command under Linux. The Docker program translates these Dockerfile instructions into real Linux commands. Dockerfiles have their own written formats and supported commands, and Docker programs resolve dependencies between these commands, similar to makefiles. The Docker program will read the Dockerfile and generate a customized image according to the instructions. Compared to the black box of image, Dockerfile, an obvious script, is more acceptable to users because it clearly shows how image is generated. With Dockerfile, when we need to customize our additional requirements, we only need to add or modify instructions on Dockerfile to regenerate the image, saving the trouble of typing commands.

First, we package the above SpringBoot application using Maven instructions as follows

mvn package
Copy the code

Of course, if you are using Idea as dev, you can package it with Maven tool without input, as shown below



This will wrap the project with the JAR package, which I renamed toexample.jar“, and then we go to the target server (note that Docker is already installed on the server) and create the DockerFile file in any directory, because I’m using CentOS7/var/tmpCreate a Dockerfile file

touch Dockerfile
Copy the code

Note that the Dockerfile must be in the same path as the Jar, so be careful when you upload the Jar. The following post DockerFile content, just use the most basic build instructions, more complex can be familiar with, refer to the DockerFile command to build

FROM java:8
EXPOSE 8080
VOLUME /slm
ADD example.jar boot-docker.jar
RUN sh -c 'touch /boot-docker.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh"."-c"."java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /boot-docker.jar" ]
Copy the code

Here’s a little explanation of the basic syntax

  • FROM base mirrors necessary, which means your project will be built on
  • EXPOSE allows specified port forwarding
  • VOLUME creates a mount point that can be mounted from a local host or other containers. It is used to store databases and data that needs to be held.
  • ADD will copy the file from the path to the internal path of the container to support remote URL. If it is a remote URL, the permission will be 600
  • ENV can be used to set environment variables for docker containers
  • ENTRYPOINT specifies the command or file to execute when a Docker image is run as instance (i.e. Docker Container).
  • CMD and ENTRYPOINT can both be used to specify which program to start running, and both commands have two different syntax:
CMD ["ls".' '-l"]
CMD ls -l
Copy the code

Once the DockerFile is written, we start building the image with the following instructions

docker build -t boot-docker .
Copy the code

-t boot-docker stands for the name you want to build

Sending build context to Docker daemon  16.81MB
Step 1/7 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/7 : EXPOSE 8080
 ---> Using cache
 ---> b2445bf62da8
Step 3/7 : VOLUME /slm
 ---> Using cache
 ---> b73d0b73b868
Step 4/7 : ADD example.jar boot-docker.jar
 ---> Using cache
 ---> 2b4868aafca9
Step 5/7 : RUN sh -c 'touch /boot-docker.jar'
 ---> Using cache
 ---> 816b59f199af
Step 6/7 : ENV JAVA_OPTS=""
 ---> Using cache
 ---> 784f033b9dd6
Step 7/7 : ENTRYPOINT [ "sh"."-c"."java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /boot-docker.jar" ]
 ---> Using cache
 ---> 92a0da91ea19
Successfully built 92a0da91ea19
Successfully tagged bootdocker:latest
Copy the code

The sentence Successfully Built 92A0DA91ea19 specifies the ID of the image that was just built. Now we can operate according to this ID. Enter the run command to start.

docker run -itd -p 8080:8080 --name example 92a0da91ea19
Copy the code
  • -d indicates background running
  • -p Indicates the port mapping
  • – name Container name

The access interface has been successfully run. Note that the host’s IP plus port is accessed because it maps to the host’s port, such as http://ip:8080/index

Building with Maven

Create a Dockerfile in your project directory and copy the contents of the above Dockerfile. The structure is as follows



Add docker build dependencies to the POM

<plugin> <! Maven plugin --> <groupId>com.spotify</groupId> <artifactId>docker-maven plugin</artifactId> <version>0.413.</version> <! The execution node configures when the MVN is executedpackageDocker :build- > <executions> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <! --> <dockerHost> HTTP:/ / 192.168.66.131:2375 < / dockerHost ><! <imageName>${docker.image.prefix}/${project.artifactid}</imageName> <! ${project.basedir}</dockerDirectory> <! ImageTags --> <imageTags> <imageTag>${project.version}</imageTag> </imageTags> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>Copy the code

Note that dockerDirectory still needs to set the path of the Dockerfile file. Then if you do not configure dockerHost, copy the project to the Linux host and unzip it

unzip boot-docker.zip
cd boot-docker
Copy the code

Execute command:

mvn package docker:build
Copy the code

And if it’s configureddockerHostif

Both approaches start building builds

[INFO] Building image boot-docker/boot-docker
Step 1/7 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/7 : EXPOSE 8080
 ---> Using cache
 ---> b2445bf62da8
Step 3/7 : VOLUME /slm
 ---> Using cache
 ---> b73d0b73b868
Step 4/7 : ADD boot-docker-0.01.-SNAPSHOT.jar boot-docker.jar
 ---> Using cache
 ---> 2b4868aafca9
Step 5/7 : RUN sh -c 'touch /boot-docker.jar'
 ---> Using cache
 ---> 816b59f199af
Step 6/7 : ENV JAVA_OPTS=""
 ---> Using cache
 ---> 784f033b9dd6
Step 7/7 : ENTRYPOINT [ "sh"."-c"."java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /boot-docker.jar" ]
 ---> Using cache
 ---> 92a0da91ea19
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 92a0da91ea19
Successfully tagged boot-docker/boot-docker:latest
[INFO] Built boot-docker/boot-docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.908s
[INFO] Finished at: Wed Jul 10 16:00:21 CST 2019
[INFO] Final Memory: 35M/86M
[INFO] ------------------------------------------------------------------------
Copy the code

Then you can start the container

docker run -itd -p 8080:8080 --name example 92a0da91ea19
Copy the code

The access interface has been successfully run. Note that the host’s IP plus port is accessed because it maps to the host’s port, such as http://ip:8080/index

Deploying with Idea

Following the poM configuration in Maven builds, we now have an additional option in our IDEA, which is docker, as follows:



Click the green start button on the left to connect the Docker container. After successful connection, we can see all containers and images in the current Docker, including the Docker image we just created, as follows:



At this point, we select the image and right click to create a container based on the image, as shown below



Select Create container and fill in the required information about the container, Specify the container name, the image ID will be automatically filled in, Specify Specify port, and write the port mapping:



Once the configuration is complete, click the Run button below to start running.

Application containerization is a hot topic in recent years. And container technology emerges in endlessly, it is necessary to master application container technology. Today we built a Spring Boot container from scratch using Docker step by step. I hope it helps.