The development of Docker technology provides a more convenient environment for micro-service landing. It is actually very simple to deploy Spring Boot with Docker. In this article, we will simply learn.
Start by building a simple Spring Boot project, then add Docker support to the project, and finally deploy the project.
A simple Spring Boot project
In Pom.xml, Spring Boot 2.0-related dependencies are used
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
. < version > 2.0.0 RELEASE < / version >
</parent>
Copy the code
Add Web and test dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Copy the code
Create a DockerController with an index() method that returns Hello Docker!
@RestController
public class DockerController {
@RequestMapping("/")
public String index() {
return "Hello Docker!" ;
}
}
Copy the code
Start the class
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
Copy the code
After the addition, start the project. After the successful startup, the browser will ask: http://localhost:8080/, and the page will return: Hello Docker! “Indicates that the Spring Boot project is correctly configured.
Spring Boot project adds Docker support
Add the Docker image name to pom.xml-properties
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
Copy the code
Add Docker build plugins to plugins:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<! -- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
The < version > 1.0.0 < / version >
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<! -- Docker maven plugin -->
</plugins>
</build>
Copy the code
Create a Dockerfile file under the directory SRC /main/docker. The Dockerfile file tells you how to build the image.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring - the boot - docker - 1.0. Jar app. The jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Copy the code
Build the Jdk base environment and add the Spring Boot Jar to the image.
-
FROM: indicates that the Jdk8 environment is used as the base image. If the image is not local, it will be downloaded FROM DockerHub
-
VOLUME, VOLUME points to a directory named/TMP. Since Spring Boot uses the built-in Tomcat container, Tomcat uses/TMP as its working directory by default. Create a temporary file in the /var/lib/docker directory on the host and link it to the/TMP directory in the container
-
ADD, copy and rename the file
-
Egd system attribute pointing to /dev/urandom as ENTRYPOINT to shorten Tomcat startup time
This completes the addition of the Docker dependency to the Spring Boot project.
Build a packaging environment
We need a Docker environment to package the Spring Boot project. It is very troublesome to build a Docker environment on Windows, so I take Centos 7 as an example.
Install the Docker environment
The installation
yum install docker
Copy the code
Once the installation is complete, use the following command to start the Docker service and set it to boot:
ervice docker start
chkconfig docker on
CentOS 7 supports the new systemd syntax, which is as follows:
systemctl start docker.service
systemctl enable docker.service
Copy the code
Use the Docker China accelerator
vi /etc/docker/daemon.json
# after adding:
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"live-restore": true
}
Copy the code
Restart Docker
systemctl restart docker
Copy the code
If docker version is displayed, the installation is normal.
Install the JDK
Yum -y install Java -- 1.8.0 comes with its *
Copy the code
Configure environment variables open vim /etc/profile add some content
-
Export JAVA_HOME = / usr/lib/JVM/Java -- 1.8.0 comes with its 1.8.0.161-0. B14. El7_4. X86_64
-
export PATH=$PATH:$JAVA_HOME/bin
Copy the code
After the modification is complete, make the modification take effect
source /etc/profile
Copy the code
Run the Java -version command to return the version information.
Install MAVEN
Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
-
# # decompression
-
The tar VXF apache maven -- 3.5.2 - bin. Tar. Gz
-
# # mobile
-
The mv apache maven -- 3.5.2 / usr/local/maven3
Copy the code
Modify the environment variable by adding the following lines to /etc/profile
-
MAVEN_HOME=/usr/local/maven3
-
export MAVEN_HOME
-
export PATH=${PATH}:${MAVEN_HOME}/bin
Copy the code
Remember to execute source /etc/profile for the environment variables to take effect.
Run the MVN -version command to return the version information.
The entire build environment is now configured.
Deploy the Spring Boot project using Docker
Copy the project spring-boot-Docker server into the project path for packaging test.
-
# packaged
-
mvn package
-
# start
-
Java - jar target/spring - the boot - docker - 1.0. The jar
Copy the code
After seeing the Boot log of Spring Boot, the environment configuration is ok. Next, we use DockerFile to build the image.
mvn package docker:build
Copy the code
The first build may be a bit slow, but it is successful when you see the following:
.
Step 1 : FROM openjdk:8-jdk-alpine
---> 224765a6bdbe
Step 2 : VOLUME /tmp
---> Using cache
---> b4e86cc8654e
ADD spring-boot-docker-1.0.jar app.jar
---> a20fe75963ab
Removing intermediate container 593ee5e1ea51
Step 4 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
---> Running in 85d558a10cd4
---> 7102f08b5e95
Removing intermediate container 85d558a10cd4
Successfully built 7102f08b5e95
[INFO] Built springboot/spring-boot-docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.346s
[INFO] Finished at: 2018-03-13T16:20:15+08:00
[INFO] Final Memory: 42M/182M
[INFO] ------------------------------------------------------------------------
Copy the code
Use the docker images command to view the built images:
-
docker images
-
REPOSITORY TAG IMAGE ID CREATED SIZE
-
Springboot /spring-boot-docker latest 99ce9468DA74 6 seconds ago 117.5 MB
Copy the code
Springboot /spring-boot-docker is the image we built and the next step is to run it
-
docker run -p 8080:8080 -t springboot/spring-boot-docker
Copy the code
After the startup is complete, we use Docker PS to view the running image:
-
docker ps
-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-
049570DA86a9 Springboot /spring-boot-docker "java-djava. security" 30 seconds ago Up 27 seconds 0.0.0.0:8080->8080/ TCP determined_mahavira
Copy the code
Can see that we build the container is running, access to a browser: http://192.168.0.x:8080/, to return
-
Hello Docker!
Copy the code
Successful deployment of the Spring Boot project using Docker!
The sample code: https://github.com/ityouknow/spring-cloud-examples
END