Spring Boot 简明教程
Spring Boot - Creating Docker Image
Docker 是一种容器管理服务,可简化构建和部署。如果您是 Docker 新手,可以从以下链接详细了解它: [role="bare" [role="bare"]https://www.tutorialspoint.com/docker/index.htm ]
Docker is a container management service that eases building and deployment. If you are a beginner to Docker, you can learn about is in detail at this link − [role="bare"https://www.tutorialspoint.com/docker/index.htm]
在本章中,我们将了解如何使用 Spring Boot 应用程序的 Maven 和 Gradle 依赖关系创建 Docker 镜像。
In this chapter, we are going to see How to create a Docker image by using Maven and Gradle dependencies for your Spring Boot application.
Create Dockerfile
首先,在目录 src/main/docker 下创建名称为 Dockerfile 的文件,内容如下所示。请注意,此文件对于创建 Docker 镜像非常重要。
First, create a file with the name Dockerfile under the directories src/main/docker with the contents shown below. Note that this file is important to create a Docker image.
FROM java:8
VOLUME /tmp
ADD dockerapp-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Maven
对于 Maven,将 Docker Maven 插件添加到构建配置文件 pom.xml 中
For Maven, add the Docker Maven plugin into your build configuration file pom.xml
<properties>
<docker.image.prefix>spring-boot-tutorialspoint</docker.image.prefix>
</properties>
<build>
<plugins>
<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>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
完整的 pom.xml 文件如下所示:
The complete pom.xml file is given below −
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
<artifactId>dockerapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dockerapp</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<docker.image.prefix>spring-boot-tutorialspoint</docker.image.prefix>
</properties>
<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>
<build>
<plugins>
<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>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
现在,您可以使用 Maven 命令运行您的应用程序 mvn package docker:build
Now, you can run your application by using the Maven command mvn package docker:build

Note - 在 tcp://localhost:2375 上启用公开守护进程,不使用 TLS。
Note − Enable the Expose daemon on tcp://localhost:2375 without TLS.
构建成功后,您可以在控制台看到输出,如下所示:
After build success, you can see the output on the console as shown below −

现在,通过使用 docker images 命令查看 Docker 镜像并在控制台上查看镜像信息。
Now, see the Docker images by the command using docker images and see the image info on the console.

Gradle
要使用 Gradle 构建配置构建 Docker 镜像,我们需要添加 docker 插件并编写任务 buildDocker 以创建 Docker 镜像。
To build a Docker image by using Gradle build configuration, we need to add the docker plugin and need to write a task buildDocker to create a Docker image.
Gradle Docker 配置的代码如下所示。
The code for Gradle Docker configuration is given below.
buildscript {
.....
dependencies {
.....
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
group = 'spring-boot-tutorialspoint'
.....
apply plugin: 'docker'
task buildDocker(type: Docker, dependsOn: build) {
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
完整的 build.gradle 文件如下所示。
The complete build.gradle file is given below.
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
group = 'spring-boot-tutorialspoint'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'
group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
task buildDocker(type: Docker, dependsOn: build) {
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
现在,使用下面显示的命令创建 Docker 镜像:
Now, create a Docker image by using the command shown below −
gradle build buildDocker

执行命令后,您可以在控制台窗口上看到 BUILD SUCCESSFUL 日志。
After executing the command, you can see the BUILD SUCCESSFUL log on the console window.

现在,通过使用 docker images 命令查看 Docker 镜像并在控制台上查看镜像的信息。
Now, see the Docker images by the command using docker images and see the image’s info on the console.
