This article uses Docker to deploy the Spring Boot project. Docker and Maven need to be installed in the environment before deployment (for packaging), so this article will install Docker and Maven first. Then build a Spring Boot project to support Docker deployment; Finally, deploy and test.
Environment set up
Install the Docker
Check the system kernel version
Docker runs on CentOS 7 with a 64-bit operating system and a kernel version of 3.10 or later. Ensure that the Linux kernel is installed on the host. Run the uname -r command to check the kernel version.
[root@localhost ~]# uname -r3.10.0-957. El7 x86_64Copy the code
Install Docker in CentOS 7
Run the yum install -y docker command to install the docker. -y indicates that the docker is installed using the default configuration.
Start the Docker service and set it to start automatically after startup
Use the following command:
systemctl start docker.service
systemctl enable docker.service
Copy the code
Viewing version Information
Enter docker version. If the version information is displayed, the Docker is successfully installed.
[root@localhost ~]# docker versionClient: Version: 1.13.1 API Version: 1.26 Package Version: docker - 1.13.1-96. Gitb2f74b2. El7. Centos. X86_64 Go Version: Go1.10.3 Git commit: b2f74B2/1.13.1 Built: Wed May 1 14:55:20 2019 OS/Arch: Linux/AMD64 Server: Version: 1.13.1 API Version: 1.26 (Minimum version 1.12) Package Version: Docker - 1.13.1-96. Gitb2f74b2. El7. Centos. X86_64 Go version: go1.10.3 Git commit: b2f74b2/1.13.1 Built: Wed May 1 14:55:20 2019 OS/Arch: linux/amd64 Experimental:false
Copy the code
Install Maven and configure Ali Cloud mirroring
Download Maven
Wget HTTP: / / https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
Unzip and Move
Tar VXF apache-maven-3.5.4-bin.tar.gz mv apache-maven-3.5.4 /usr/local/maven3
Copy the code
Configuring environment Variables
Open the environment variable vim /etc/profile and add the following:
MAVEN_HOME=/usr/local/maven3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin
Copy the code
Save the configuration and exit: : wq
Refresh the configuration of environment variables for them to take effect: source /etc/profile
Viewing version Information
Run the MVN -v command
[root@localhost ~]# mvn -vApache Maven 3.5.4 (1 edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00) Maven home: /usr/local/maven3
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /usr/local/ Java /jdk1.8.0_211/ JRE Default locale: zh_CN, platform Encoding: UTF-8 OS name:"linux", version: "3.10.0-957. El7. X86_64", arch: "amd64", family: "unix"
Copy the code
The MVN package command was used to package, but an error was reported indicating that the connection was rejected.
Go to the Maven installation directory, open settings. XML, and add the following options to the
TAB:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
Copy the code
Spring Boot project adds Docker support
Create a Spring Boot project
Create a new Spring Boot project and add a simple DockerController for easy testing.
@RestController
@SpringBootApplication
@RequestMapping("/docker")
public class DockerController {
@RequestMapping("/hello")
@ResponseBody
public String hello(a) {
return "Hello Docker"; }}Copy the code
Add Docker support
Add the Docker image name to pom.xml
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
Copy the code
Add Docker plugin:
<! -- Docker plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<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>
Copy the code
Write a Dockerfile file
Create a Dockerfile file under the directory SRC /main/docker. The Dockerfile file tells you how to build the image.
FROM Java :8 VOLUME/TMP ADD docker-0.0.1- snapshot.jar app.jar ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
Copy the code
The FROM directive specifies an existing image, telling Docker that subsequent directives are based on it. For example, FROM Java :8 indicates that an image is built using the Jdk1.8 environment as the base image.
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.
Deploy the Spring Boot project
Copy the project code into the docker server and enter the project path for packaging test.
Run java-jar target/spring-boot-docker-1.0.jar
If the project is successfully started, the environment configuration is ok. Then you can use Dockerfile to build the image.
MVN Package Docker :build It can be seen that the docker image is successfully built. If you look carefully at the log, you will find that the four steps correspond to the instructions in the Dockerfile one by one.
Use the docker images command to view the built images:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot/docker latest 39aabeebb971 5 days ago 660 MB
Copy the code
Run docker run -p 8080:8080 -t springboot/docker
If you access the system from the local browser, the deployment is successful.
Full code: GitHub address
Spring Boot 2.0(4) : Deploy Spring Boot with Docker