Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About
知乎专栏多维度架构

5.40. Spring Boot with Docker

5.40.1. 通过 Docker 命令构建镜像

5.40.1.1. 手工编译镜像

在项目根目录创建 Dockerfile 文件

		
% cat Dockerfile 
FROM openjdk
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]		
		
			

编译镜像

		
mvn package
docker build -t netkiller/docker .
 
% docker images | grep netkiller
netkiller/docker                           latest              ed359b6ffcad        16 seconds ago      105MB 

% docker run -ti --entrypoint /bin/sh netkiller/docker
sh-4.2# ls
app.jar  bin  boot  dev  etc  home  lib  lib64	media  mnt  opt  proc  root  run  sbin	srv  sys  tmp  usr  var
sh-4.2# 
		
			

启动镜像测试

		
docker run -p 8080:8080 netkiller/docker

neo@MacBook-Pro ~ % curl http://localhost:8080
Hello Docker World
		
			

5.40.1.2. Dockerfile 放在 src/main/docker/Dockerfile 下

		
% cat src/main/docker/Dockerfile 
FROM openjdk
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
		
			
		
mvn package
docker rmi netkiller/docker -f
docker build -t netkiller/docker -f src/main/docker/Dockerfile .
docker run -p 8080:8080 netkiller/docker
		
			
		
neo@MacBook-Pro ~ % curl http://localhost:8080
Hello Docker World
		
			

5.40.1.3. 通过参数指定 Springboot 文件

		
% cat src/main/docker/Dockerfile 
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
		
			

		
mvn package
docker rmi netkiller/docker -f
docker build --build-arg JAR_FILE=target/*.jar -t netkiller/docker -f src/main/docker/Dockerfile .
docker run -p 8080:8080 netkiller/docker
		
			

5.40.1.4. SPRING_PROFILES_ACTIVE 指定配置文件

		
% docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 netkiller/docker
		
			

5.40.1.5. 推送镜像到仓库

		
neo@MacBook-Pro ~ % docker push netkiller/docker
The push refers to repository [docker.io/netkiller/docker]
100ff47f36fe: Pushed 
a7aafc769de1: Mounted from library/openjdk 
2666aafcfdd9: Mounted from library/openjdk 
c4a7cf6a6169: Mounted from library/openjdk 

latest: digest: sha256:3078fea95c633f007be33b829efae0ff8e9d78ad463925af7d07752c95eb43a3 size: 1165
		
			

5.40.2. 通过 Maven 构建 Docker 镜像

5.40.2.1. Maven + Dockerfile 方案一

项目地址 https://github.com/spotify/dockerfile-maven

		
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>cn.netkiller.docker.Application</mainClass>
				</configuration>
			</plugin>
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>dockerfile-maven-plugin</artifactId>
				<version>1.4.10</version>
				<executions>
					<execution>
						<id>default</id>
						<goals>
							<goal>build</goal>
							<goal>push</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<dockerfile>${project.basedir}/src/main/docker/Dockerfile</dockerfile>
					<repository>${docker.image.prefix}/${project.artifactId}</repository>
					<tag>${project.version}</tag>
					<buildArgs>
						<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
					</buildArgs>
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>

				</configuration>

			</plugin>
		</plugins>
	</build>
	
		
			
		
neo@MacBook-Pro ~/git/springcloud/docker % mvn dockerfile:build
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< cn.netkiller:docker >-------------------------
[INFO] Building docker 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dockerfile-maven-plugin:1.4.10:build (default-cli) @ docker ---
[INFO] dockerfile: /Users/neo/git/springcloud/docker/src/main/docker/Dockerfile
[INFO] contextDirectory: /Users/neo/git/springcloud/docker
[INFO] Building Docker context /Users/neo/git/springcloud/docker
[INFO] Path(dockerfile): /Users/neo/git/springcloud/docker/src/main/docker/Dockerfile
[INFO] Path(contextDirectory): /Users/neo/git/springcloud/docker
[INFO] 
[INFO] Image will be built as netkiller/docker:0.0.1-SNAPSHOT
[INFO] 
[INFO] Step 1/7 : FROM openjdk
[INFO] 
[INFO] Pulling from library/openjdk
[INFO] Digest: sha256:38ec2c78a60ec4d5773c93534e433237be154ff5afa476965a68837b43ef2f19
[INFO] Status: Image is up to date for openjdk:latest
[INFO]  ---> b697a97ee8e1
[INFO] Step 2/7 : MAINTAINER Netkiller <netkiller@msn.com>
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> e6fd68ec1ce8
[INFO] Step 3/7 : VOLUME /tmp
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 78b146e1a8a0
[INFO] Step 4/7 : ARG JAR_FILE
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 2c60b65d49dc
[INFO] Step 5/7 : COPY ${JAR_FILE} app.jar
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 3186f0425f1d
[INFO] Step 6/7 : CMD ["java", "-version"]
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> d14b8d6360fe
[INFO] Step 7/7 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 68e424cf5eab
[INFO] Successfully built 68e424cf5eab
[INFO] Successfully tagged netkiller/docker:0.0.1-SNAPSHOT
[INFO] 
[INFO] Detected build of image with id 68e424cf5eab
[INFO] Building jar: /Users/neo/git/springcloud/docker/target/docker-0.0.1-SNAPSHOT-docker-info.jar
[INFO] Successfully built netkiller/docker:0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.413 s
[INFO] Finished at: 2019-04-13T05:39:07+08:00
[INFO] ------------------------------------------------------------------------

		
			

5.40.2.2. Maven + Dockerfile 方案二

			
<build>
  <plugins>
    ...
    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>VERSION GOES HERE</version>
      <configuration>
        <imageName>example</imageName>
        <dockerDirectory>docker</dockerDirectory>
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>			
			
			

5.40.2.3. Maven 不使用 Dockerfile 文件

项目地址 https://github.com/spotify/docker-maven-plugin

		
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>1.2.0</version>
				<configuration>
					<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
					<baseImage>openjdk</baseImage>
					<tag>${project.version}</tag>
					<maintainer>${docker.maintainer}</maintainer>
					<volumes>/tmp</volumes>
					<workdir>/</workdir>
					<cmd>["java", "-version"]</cmd>
					<entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
					<!-- copy the service's jar file from target into the root directory of the image -->
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
				</configuration>
			</plugin>
		
			

构建镜像 mvn clean package docker:build

		
neo@MacBook-Pro ~/git/springcloud/webflux % mvn docker:build         
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< cn.netkiller:webflux >------------------------
[INFO] Building webflux 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- docker-maven-plugin:1.2.0:build (default-cli) @ webflux ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /Users/neo/git/springcloud/webflux/target/webflux-0.0.1-SNAPSHOT.jar -> /Users/neo/git/springcloud/webflux/target/docker/webflux-0.0.1-SNAPSHOT.jar
[INFO] Building image netkiller/webflux
Step 1/7 : FROM openjdk

 ---> b697a97ee8e1
Step 2/7 : MAINTAINER netkiller

 ---> Using cache
 ---> c275f5dc2815
Step 3/7 : WORKDIR /

 ---> Using cache
 ---> 27815e0b4455
Step 4/7 : ADD /webflux-0.0.1-SNAPSHOT.jar //

 ---> Using cache
 ---> 78b0fe2a827d
Step 5/7 : ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/webflux-0.0.1-SNAPSHOT.jar"]

 ---> Using cache
 ---> 66d5499c8ba3
Step 6/7 : CMD ["java", "-version"]

 ---> Using cache
 ---> 080a1468d88b
Step 7/7 : VOLUME /tmp

 ---> Using cache
 ---> 60debfac7b7c
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 60debfac7b7c
Successfully tagged netkiller/webflux:latest
[INFO] Built netkiller/webflux
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.485 s
[INFO] Finished at: 2019-04-13T05:41:41+08:00
[INFO] ------------------------------------------------------------------------
		
			

5.40.2.4. 推送镜像

			
neo@MacBook-Pro ~ % vim /usr/local/Cellar/maven/3.6.0/libexec/conf/settings.xml
			
  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    <server>
      <id>docker-hub</id>
      <username>netkiller</username>
      <password>******</password>
      <configuration>
        <email>netkiller@msn.com</email>
      </configuration>
    </server>
  </servers>			
			
			

****** 修改为你的密码

查看 Docker Registry 地址

			
neo@MacBook-Pro ~ % docker info | grep Registry
Registry: https://index.docker.io/v1/			
			
			

maven docker 插件配置

			
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>1.2.0</version>
				<configuration>
					<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
					<baseImage>openjdk</baseImage>
					<tag>${project.version}</tag>
					<maintainer>${docker.maintainer}</maintainer>
					<volumes>/tmp</volumes>
					<workdir>/srv</workdir>
					<cmd>["java", "-version"]</cmd>
					<entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/srv/${project.build.finalName}.jar"]</entryPoint>
					<!-- copy the service's jar file from target into the root directory of the image -->
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
					<image>${docker.image.prefix}/${project.artifactId}</image>
					<newName>${docker.image.prefix}/${project.artifactId}:${project.version}</newName>
					<serverId>docker-hub</serverId>
					<registryUrl>https://index.docker.io/v1/</registryUrl>
				</configuration>
			</plugin>			
			
			
			
docker:build -DpushImage or docker:push			
			
			

使用加密的密码

			
neo@MacBook-Pro ~ % mvn --encrypt-master-password 
Master password: 
{r7kkN/XCOXYHqwRqE30k6Bz+pNGsB7/UogGTqqo+G2A=}
			
			
			
vim /usr/local/Cellar/maven/3.6.0/libexec/conf/settings.xml
			
<servers>
  <server>
    <id>docker-hub</id>
    <username>netkiller</username>
    <password>{r7kkN/XCOXYHqwRqE30k6Bz+pNGsB7/UogGTqqo+G2A=}</password>
  </server>
</servers>			
			
			

			
vim ~/.m2/settings-security.xml

<settingsSecurity>
  <master>{r7kkN/XCOXYHqwRqE30k6Bz+pNGsB7/UogGTqqo+G2A=}</master>
</settingsSecurity>			
			
			

5.40.3. [ERROR] No plugin found for prefix 'dockerfile' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/neo/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]

		
在maven的conf/setting.xml中要加入:

neo@MacBook-Pro ~ % mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/Cellar/maven/3.6.0/libexec
Java version: 12, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
Default locale: en_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"


vim /usr/local/Cellar/maven/3.6.0/libexec/conf/settings.xml

<pluginGroups>  
    <pluginGroup>com.spotify</pluginGroup>  
</pluginGroups>