docker纪录篇2——docker使用maven远程构建

一 。docker远程服务

 docker默认情况下 本机 使用 docker命令操作镜像和容器  docker提供了 -H可以连接远程的docker服务器镜像远程镜像
和容器管理,项目中使用maven可以通过DockerFile远程连接并构建镜像 ,达到快速测试的目的

默认docker服务启动 文件(/usr/lib/systemd/system/docker.service)编辑文件 
在ExecStart后面 添加一行(表示启动2375端口 用于监听远程操作)
-H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock \


 
 
  1. [root@localhost /] # vi /usr/lib/systemd/system/docker.service
  2. [Unit]
  3. Description=Docker Application Container Engine
  4. Documentation=http: //docs.docker.com
  5. After=network.target rhel-push-plugin.socket registries.service
  6. Wants=docker-storage-setup.service
  7. Requires=docker-cleanup.timer
  8. [Service]
  9. Type=notify
  10. NotifyAccess=all
  11. EnvironmentFile=-/run/containers/registries.conf
  12. EnvironmentFile=-/etc/sysconfig/docker
  13. EnvironmentFile=-/etc/sysconfig/docker-storage
  14. EnvironmentFile=-/etc/sysconfig/docker-network
  15. Environment=GOTRACEBACK=crash
  16. Environment=DOCKER_HTTP_HOST_COMPAT= 1
  17. Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
  18. ExecStart=/usr/bin/dockerd-current \
  19. --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
  20. -- default-runtime=docker-runc \
  21. --exec-opt native.cgroupdriver=systemd \
  22. --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
  23. --init-path=/usr/libexec/docker/docker-init-current \
  24. --seccomp-profile=/etc/docker/seccomp.json \
  25. -H tcp: //0.0.0.0:2375 -H unix:///var/run/docker.sock \
  26. $OPTIONS \
  27. $DOCKER_STORAGE_OPTIONS \
  28. $DOCKER_NETWORK_OPTIONS \
  29. $ADD_REGISTRY \
  30. $BLOCK_REGISTRY \
  31. $INSECURE_REGISTRY \
  32. $REGISTRIES

重新加载服务

[root@localhost /]# systemctl daemon-reload 
 
 

重启docker服务

[root@localhost /]# service docker restart
 
 

使用任意一台有docker的机器上运行 (以下 images是显示的 192.168.1.3上的所有镜像  我的刚刚服务修改就是 1.3上)


 
 
  1. [root@localhost /] # docker -H 192.168.1.3:2375 images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. localhost: 5000/mycentos jiaozi 9eea42e7d707 About an hour ago 200 MB
  4. docker.io/registry latest b2b03e9146e1 3 weeks ago 33.3 MB

 

二。使用dockerfile-maven-plugin插件远程构建docker

在eclipse中添加 maven项目 并且设置好maven环境  项目根目录下添加 Dockerfile文件


 
 
  1. FROM maven
  2. RUN mkdir -p /app/regcenter
  3. ADD ./target/REGCENTER- 0. 0. 1-SNAPSHOT.jar /app/regcenter
  4. WORKDIR /app/regcenter
  5. EXPOSE 8761
  6. CMD java -jar /app/regcenter/REGCENTER- 0. 0. 1-SNAPSHOT.jar

pom.xml中分别添加添加dockerfile-maven-plugin【官网 https://github.com/spotify/dockerfile-maven】用于远程生成镜像  spring-boot-maven-plugins用于制定打包后的main方法类


 
 
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.spotify </groupId>
  5. <artifactId>dockerfile-maven-plugin </artifactId>
  6. <version>1.4.0 </version>
  7. <configuration>
  8. <repository>regcenter </repository>
  9. <tag>jiaozi </tag>
  10. <buildArgs>
  11. <JAR_FILE>target/${project.build.finalName}.jar </JAR_FILE>
  12. </buildArgs>
  13. </configuration>
  14. </plugin>
  15. <plugin>
  16. <groupId>org.springframework.boot </groupId>
  17. <artifactId>spring-boot-maven-plugin </artifactId>
  18. <configuration>
  19. <mainClass>cn.ps.RegServerMain </mainClass>
  20. </configuration>
  21. </plugin>
  22. </plugins>
  23. </build>

系统环境变量中添加一个环境变量 用于制定 docker的主机和端口

项目打包 (会在项目下 生成 REGSERVER-SNASHOT-0.0.1.jar )

mvn package

 
 

执行生成镜像 
 

mvn clean package dockerfile:build -DskipTests
 
 

生成成功后 可以看到生成的镜像 
 


 
 
  1. [root@localhost ~] # docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. regcenter jiaozi 3072cfb28b91 5 minutes ago 675 MB

 启动镜像 道出8761端口


 
 
  1. [root@localhost ~]# docker run -d -p 8761 :8761 regcenter :jiaozi
  2. 1 f61a01c2d59f719ca80caebcbb7da17f2f8d859f0a3746e18915b3babb2b175

浏览器访问 (部署的是注册中心的代码)

三。使用docker-maven-plugin插件远程构建docker

使用这个插件不需要dockerfile 直接在maven中定义dockerfile的逻辑  官网建议使用 dockerfile-maven-plugin插件  这个插件可以不配置环境变量 在maven中配置
pom.xml修改如下【配置的属性和dockerfile基本一致】:
 


 
 
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.spotify </groupId>
  5. <artifactId>docker-maven-plugin </artifactId>
  6. <version>1.1.1 </version>
  7. <configuration>
  8. <imageName>regcenter1 </imageName>
  9. <dockerHost>http://192.168.1.238:2375 </dockerHost>
  10. <baseImage>maven </baseImage>
  11. <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"] </entryPoint>
  12. <!-- copy the service's jar file from target into the root directory of the image -->
  13. <resources>
  14. <resource>
  15. <targetPath>/ </targetPath>
  16. <directory>${project.build.directory} </directory>
  17. <include>${project.build.finalName}.jar </include>
  18. </resource>
  19. </resources>
  20. </configuration>
  21. </plugin>
  22. <plugin>
  23. <groupId>org.springframework.boot </groupId>
  24. <artifactId>spring-boot-maven-plugin </artifactId>
  25. <configuration>
  26. <mainClass>cn.ps.RegServerMain </mainClass>
  27. </configuration>
  28. </plugin>
  29. </plugins>
  30. </build>

这里一点不同 这个地方的dockerhost必须是http 不能使tcp
运行

mvn clean package docker:build -DskipTests
 
 

查看多了一个regserver1的镜像


 
 
  1. [root@localhost ~] # docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. regcenter1 latest 2dc1894563af 10 seconds ago 675 MB

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值