[sho-gxj@DockerHost0 ~]$ cat /etc/issue Ubuntu 16.04 LTS \n \l
參考docker官網java
[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=
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
參考官方文檔,本文使用maven工具管理項目。python
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>
Main: package docker:build -DskipTests Environment: DOCKER_HOST tcp://192.168.137.234:2376
以下圖:
linux
其中,Environment也能夠放在pom.xml中指定: git
或者配置本地環境變量(不作演示)。redis
另,注意 Dockerfile
須要放到<dockerDirectory>目錄中:spring
FROM openjdk:8-jdk-alpine VOLUME /tmp COPY springboot4docker-0.0.1-SNAPSHOT.jar /tmp/app.jar ENTRYPOINT ["java","-jar","/tmp/app.jar"]
[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。docker
build成功後,能夠在docker服務器看到最新的image:apache
[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一個容器測試:
json