使用Jib快速简便地创建Docker镜像
- - DockOne.io在本文中,我们将看看Jib,它是Google提供的一个工具,可以轻松快速地创建Docker镜像. 无需创建Dockerfile文件,也无需安装Docker守护进程,Jib可直接使用. 到目前为止,我们一直使用来自Spotify的 dockerfile-maven-plugin来构建和推送Docker镜像.
dockerfile-maven-plugin
来构建和推送Docker镜像。这要求我们根据 最佳实践来编写Dockerfile,安装Docker守护进程并将插件添加到构建过程中。Jib将为我们提供一种更简便的方式来创建Docker镜像。我们只需要添加并配置Maven插件即可。当然,只有当我们自己亲自尝试了,我们才会相信它更轻便,这正是接下来要做的。
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
StringBuilder message = new StringBuilder("Hello Jib Maven Plugin!");
try {
InetAddress ip = InetAddress.getLocalHost();
message.append(" From host: " + ip);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return message.toString();
}
}
$ mvn spring-boot:run
Hello Jib Maven Plugin! From host: gunter-Latitude-5590/127.0.1.1
com.google.cloud.tools
jib-maven-plugin
1.8.0
openjdk:11.0.5-jre
docker.io/${docker.image.prefix}/${project.artifactId}
pass
from
标签包含我们的基础镜像,就像Dockerfile中的 FROM
语句。 to
标签包含我们想要推送的镜像。 ${docker.image.prefix}
设置为 mydeveloperplanet
(我们的Docker Hub账号),你需要对应修改为你自己的账号。 ${project.artifactId}
包含 1.0-SNAPSHOT
版本。为了使用Credential Helper,我们将标签 credHelper
设置为 pass
。 $ gpg --gen-key
.profile
文件中,注意替换 Your_GPG_Key
内容。 export GPGKEY=Your_GPG_Key
$ source .profile
$ gpg --send-keys --keyserver keyserver.ubuntu.com $GPGKEY
$ sudo apt install pass
$ pass init Your_GPG_Key
mkdir: created directory '/home/gunter/.password-store/'
Password store initialized for My Password Storage Key
$ wget https://github.com/docker/docker-credential-helpers/releases/download/v0.6.3/docker-credential-pass-v0.6.3-amd64.tar.gz
$ tar xvzf docker-credential-pass-v0.6.3-amd64.tar.gz
$ mv docker-credential-pass /usr/bin
$ chmod +x docker-credential-pass
config.json
文件。在文档中说明要添加内容 { "credStore": "pass" }
,但是使用此配置,Jib将无法连接到Docker Hub注册表。我们发现该Issue中提及到 credStore
已不再支持Google Cloud Registry。 "credHelpers": {
"https://index.docker.io/v1": "pass"
}
$ pass insert docker-credential-helpers/docker-pass-initialized-check
mkdir: created directory '/home/gunter/.password-store/docker-credential-helpers'
Enter password for docker-credential-helpers/docker-pass-initialized-check:
Retype password for docker-credential-helpers/docker-pass-initialized-check:
$ pass show docker-credential-helpers/docker-pass-initialized-check
pass is initialized
config.json
文件中。不清楚为何会出现该提示,因为凭证已经是以加密形式保存在 config.json
中。 $ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: your_user_name
Password:
WARNING! Your password will be stored unencrypted in /home/gunter/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
docker login
无需输入凭证即可执行。 $ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/gunter/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
docker logout
再次注销 $ docker logout
Removing login credentials for https://index.docker.io/v1/
$ mvn compile jib:build
Base image 'openjdk:11.0.5-jre' does not use a specific image digest - build may not be reproducible
openjdk:11.0.5-jre
为 openjdk@sha256:b3e19d27caa8249aad6f90c6e987943d03e915bbf3a66bc1b7f994a4fed668f6
(sha256格式)值来轻松解决。 The credential helper (docker-credential-pass) has nothing for server URL: https://index.docker.io/v1
docker.io/${docker.image.prefix}/${project.artifactId}
pass
${project.version}
com.mydeveloperplanet.myjibplanet.MyJibPlanetApplication
format
和OCI值添加到容器配置中。Docker Hub尚未完全支持OCI,将会显示 An error occurred while loading the tags. Try reloading the page
错误信息。 $ docker pull mydeveloperplanet/myjibplanet
Using default tag: latest
latest: Pulling from mydeveloperplanet/myjibplanet
844c33c7e6ea: Pull complete
ada5d61ae65d: Pull complete
f8427fdf4292: Pull complete
a5217f27a28f: Pull complete
176e83ebae4f: Pull complete
800204250483: Pull complete
492e142ab90b: Pull complete
7c8e6198cd4b: Pull complete
c49bb7f02774: Pull complete
Digest: sha256:b7144bfdf6ee47d6b38914a84789ef9f7e2117320080b28ce39c385ee399a0c8
Status: Downloaded newer image for mydeveloperplanet/myjibplanet:latest
docker.io/mydeveloperplanet/myjibplanet:latest
$ docker run -p 127.0.0.1:8080:8080/tcp mydeveloperplanet/myjibplanet
...
2019-12-25 09:57:13.196 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-12-25 09:57:13.205 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c05e431b0bd1 mydeveloperplanet/myjibplanet "java -cp /app/resou…" 13 seconds ago Up 12 seconds 127.0.0.1:8080->8080/tcp recursing_meninsky
$ docker inspect c05e431b0bd1
...
"NetworkSettings": {
...
"IPAddress": "172.17.0.2",
...
}
...
Hello Jib Maven Plugin! From host: c05e431b0bd1/172.17.0.2
$ docker exec -it -u root c05e431b0bd1 cat /etc/passwd
...
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
...
nobody
用户,我们可以用它来运行应用程序。添加 user
标签到pom文件中:
com.mydeveloperplanet.myjibplanet.MyJibPlanetApplication
nobody
docker inspect
检验是否是使用 nobody
作为用户。 ...
"Config": {
"Hostname": "76b3afaca3af",
"Domainname": "",
"User": "nobody",
...
}
...