spring-cloud-docker-learning

#开启Docker远程访问

测试环境是vbox fedora28 server

  • 打开/usr/lib/systemd/system/docker.service文件,修改ExecStart这行。

    1
    ExecStart=/usr/bin/dockerd  -H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock
  • 重启docker后生效

    1
    2
    systemctl daemon-reload    
    systemctl restart docker

#pom文件中加入docker plugin插件

  • 如下配置,${dockerHost}是docker地址,如:http://192.168.56.4:2375

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

    <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
    <dockerHost>${dockerHost}</dockerHost>
    <imageName>${project.artifactId}</imageName>
    <baseImage>registry.cn-hangzhou.aliyuncs.com/chainone/centos7-jdk8-maven3.3.9</baseImage>
    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
    <skipDockerBuild>false</skipDockerBuild>
    <resources>
    <resource>
    <targetPath>/</targetPath>
    <directory>${project.build.directory}</directory>
    <include>${project.build.finalName}.jar</include>
    </resource>
    </resources>
    </configuration>
    </plugin>
    </plugins>
    </build>

#打包上传

  • 用idea创建一个maven配置

TIM20180709123600.png

  • 使用docker:build打包成功logs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building diysoul-config-client 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ diysoul-config-client ---
[INFO] Deleting D:\Projects\github\diysoul-parent\config-client\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ diysoul-config-client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ diysoul-config-client ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Projects\github\diysoul-parent\config-client\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ diysoul-config-client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Projects\github\diysoul-parent\config-client\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ diysoul-config-client ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Projects\github\diysoul-parent\config-client\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ diysoul-config-client ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ diysoul-config-client ---
[INFO] Building jar: D:\Projects\github\diysoul-parent\config-client\target\diysoul-config-client-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ diysoul-config-client ---
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ diysoul-config-client ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying D:\Projects\github\diysoul-parent\config-client\target\diysoul-config-client-0.0.1-SNAPSHOT.jar -> D:\Projects\github\diysoul-parent\config-client\target\docker\diysoul-config-client-0.0.1-SNAPSHOT.jar
[INFO] Building image diysoul-config-client
Step 1/3 : FROM registry.cn-hangzhou.aliyuncs.com/chainone/centos7-jdk8-maven3.3.9

---> c9a2f484fae9
Step 2/3 : ADD /diysoul-config-client-0.0.1-SNAPSHOT.jar //

---> d1196268f6a5
Step 3/3 : ENTRYPOINT ["java", "-jar", "/diysoul-config-client-0.0.1-SNAPSHOT.jar"]

---> Running in 3cbde7981ffb
Removing intermediate container 3cbde7981ffb
---> ba5656dc808c
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built ba5656dc808c
Successfully tagged diysoul-config-client:latest
[INFO] Built diysoul-config-client
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.959 s
[INFO] Finished at: 2018-07-09T11:51:46+08:00
[INFO] Final Memory: 58M/663M
[INFO] ------------------------------------------------------------------------

#使用docker-compose编排镜像,并启动

  • 查看刚刚打包上传的images

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@dev home]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    diysoul-config-client latest ba5656dc808c About an hour ago 699MB
    diysoul-config-server latest 516736b23a74 About an hour ago 704MB
    diysoul-registry latest 2a93be158d61 About an hour ago 704MB
    sameersbn/gitlab 11.0.1 6c0958de4f90 13 days ago 2.45GB
    demo-scheduer latest e7a0f915b4f5 6 weeks ago 676MB
    bitnami/mysql latest e92b934ad575 6 weeks ago 302MB
    jetbrains/teamcity-agent latest 65915679655c 7 weeks ago 1.41GB
    jetbrains/teamcity-minimal-agent latest 3d277b5ad3f3 7 weeks ago 441MB
    jetbrains/teamcity-server latest 518af2437920 7 weeks ago 1.53GB
    sameersbn/redis latest 32eae00a0f53 5 months ago 133MB
    sameersbn/postgresql 9.6-2 7f79690e67e0 17 months ago 234MB
    registry.cn-hangzhou.aliyuncs.com/chainone/centos7-jdk8-maven3.3.9 latest c9a2f484fae9 2 years ago 659MB
    [root@dev home]#
  • docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
version: "3"

services:
diysoul-registry:
image: diysoul-registry
container_name: diysoul-registry
hostname: diysoul-registry
environment:
- active-profile=docker
ports:
- "8761:8761"
restart: always

diysoul-config-server:
image: diysoul-config-server
container_name: diysoul-config-server
hostname: diysoul-config-server
environment:
- active-profile=docker
- registry-host=diysoul-registry
depends_on:
- diysoul-registry
ports:
- "8762:8762"
restart: always

config-client:
image: diysoul-config-client
container_name: diysoul-config-client
hostname: diysoul-config-client
environment:
- active-profile=docker
- registry-host=diysoul-registry
- config-host=diysoul-config-server
depends_on:
- diysoul-registry
- diysoul-config-server
ports:
- "8763:8763"
restart: always
  • 启动

1
docker-compose up -d
  • 验证

1
2
3
4
5
6
7
[root@dev spring-cloud-docker]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------
diysoul-config-client java -jar /diysoul-config- ... Up 0.0.0.0:8763->8763/tcp
diysoul-config-server java -jar /diysoul-config- ... Up 0.0.0.0:8762->8762/tcp
diysoul-registry java -jar /diysoul-registr ... Up 0.0.0.0:8761->8761/tcp
[root@dev spring-cloud-docker]#

注册中心
TIM20180709130845.png

#source地址

1
https://github.com/fuzhouxxdong/diysoul-parent
Fork me on GitHub