使用jenkins镜像时,因为镜像内docker的运行环境异常,而无法构建docker应用

例如 没有映射/var/run/docker.sock而导致的问题

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.2.0:build (build-image) 
on project software-service: Exception caught: java.util.concurrent.ExecutionException:
com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.io.IOException: No such file or directory -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException:
Failed to execute goal com.spotify:docker
-maven-plugin:1.2.0:build (build-image) on project software-service: Exception caught at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) .....

例如 无docker命令导致的问题

RROR] Failed to execute goal com.spotify:docker-maven-plugin:1.2.0:build (build-image) 
on project software-service: Exception caught:
Get https://registry-1.docker.io/v2/library/java/blobs/sha256:49e2d6393f32abb1de7c9395c04c822ceb2287383d5a90998f7bd8dbfd43d48c:
Get
https://auth.docker.io/token?scope=repository%3Alibrary%2Fjava%3Apull&service=registry.docker.io: net/http: TLS handshake timeout -> [Help 1]

解决方案
1) 创建容器时使用 -v /var/run/docker.sock:/var/run/docker.sock
2) 在容器内安装docker

创建容器

链接: Jenkins部署

Step 1

 使用root用户登录容器,jenkins为容器的名字

# docker exec -it -u 0 jenkins bash

Step 2

# curl -sSL https://get.docker.com/ | sh

运行结果:

root@2ea04af2a474:/# curl -sSL https://get.docker.com/ | sh
# Executing docker install script, commit: 1b02882d63b9cfc484ad6b0180171c679cfe0f3a
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
debconf: delaying package configuration, since apt-utils is not installed
+ sh -c curl -fsSL "https://download.docker.com/linux/debian/gpg" | apt-key add -qq - >/dev/null
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ sh -c echo "deb [arch=amd64] https://download.docker.com/linux/debian stretch stable" > /etc/apt/sources.list.d/docker.list
+ sh -c apt-get update -qq >/dev/null
+ [ -n  ]
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
debconf: delaying package configuration, since apt-utils is not installed
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
  sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

Step 3

# usermod -aG docker jenkins

查看结果

root@2ea04af2a474:/# which docker
/usr/bin/docker
root@2ea04af2a474:/# docker -v
Docker version 19.03.8, build afacb8b7f0

Step 4

退出当前登录,使用默认用户登录jenkins容器

root@2ea04af2a474:/# exit
exit
[root@iZ2ze1eulubbo5hyz2ftv8Z ~]# docker exec -it jenkins bash
jenkins@2ea04af2a474:/$ docker -v
Docker version 19.03.8, build afacb8b7f0