Eclipse中开发SpringBoot持续集成docker
Docker环境
操作系统
[sho-gxj@DockerHost0 ~]$ cat /etc/issue
Ubuntu 16.04 LTS \n \l
docker安装
参考docker官网
docker配置
[sho-gxj@DockerHost0 ~]$ docker info
Containers: 5
Running: 1
Paused: 0
Stopped: 4
Images: 12
Server Version: 18.06.1-ce
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 45
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.4.0-21-generic
Operating System: Ubuntu 16.04 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.954GiB
Name: DockerHost0
ID: 3HOO:AGZQ:IH2K:6BCT:GP4K:C22W:RHAW:OQEK:T6VJ:M2JN:GY3T:QGAZ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
provider=generic
Experimental: false
Cluster Store: consul://192.168.137.234:8500
Cluster Advertise: 192.168.137.234:2376
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: No swap limit support
### docker需要开启允许远程连接,这里没有启用TLS(若需要启用远程连接TLS,可参考网上其他博文)
# cat /etc/systemd/system/docker.service.d/10-machine.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver aufs --label provider=generic --cluster-store=consul://192.168.137.234:8500 --cluster-advertise=enp0s3:2376
#ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver aufs --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=generic --cluster-store=consul://192.168.137.234:8500 --cluster-advertise=enp0s3:2376
Environment=
Environment=
测试远程连接docker
Client
sho-gxj@DockerHost1:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:2b:80:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.137.235/24 brd 192.168.137.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe2b:8031/64 scope link
valid_lft forever preferred_lft forever
sho-gxj@DockerHost1:~$ docker -H 192.168.137.234:2376 images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.6-alpine 449d3495be0e 2 months ago 75.5MB
redis alpine 08bef618c30a 3 months ago 28.7MB
busybox latest e1ddd7948a1c 3 months ago 1.16MB
progrium/consul latest 09ea64205e55 3 years ago 69.4MB
Eclipse配置SpringBoot环境
参考官方文档,本文使用maven工具管理项目。
本文demo代码
git clone git@gitee.com:podboq/springboot4docker.git
pom.xml
增加 docker maven 插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
eclipse运行配置
Main: package docker:build -DskipTests
Environment: DOCKER_HOST tcp://192.168.137.234:2376
如下图:
其中,Environment也可以放在pom.xml中指定:
或者配置本地环境变量(不做演示)。
另,注意 Dockerfile
需要放到<dockerDirectory>目录中:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY springboot4docker-0.0.1-SNAPSHOT.jar /tmp/app.jar
ENTRYPOINT ["java","-jar","/tmp/app.jar"]
运行遇到的问题
1) jdk以及maven版本问题,按照官方要求即可。
2) run报错如下
[INFO] Building image springboot/springboot4docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.189 s
[INFO] Finished at: 2018-11-12T15:45:30+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on project springboot4docker: Exception caught: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException: The server failed to respond with a valid HTTP response -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
原因:Docker服务器未设置允许运行远程连接或开启了TLS。
运行
build成功后,可以在docker服务器看到最新的image:
[sho-gxj@DockerHost0 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot/springboot4docker latest e4577e3fa73b 16 hours ago 119MB
openjdk 8-jdk-alpine 97bc1352afde 2 weeks ago 103MB
python 3.6-alpine 449d3495be0e 2 months ago 75.5MB
redis alpine 08bef618c30a 3 months ago 28.7MB
busybox latest e1ddd7948a1c 3 months ago 1.16MB
progrium/consul latest 09ea64205e55 3 years ago 69.4MB
run一个容器测试: