docker纪录篇4-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 \

[root@localhost /]# vi /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target rhel-push-plugin.socket registries.service
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=all
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current \
          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          --default-runtime=docker-runc \
          --exec-opt native.cgroupdriver=systemd \
          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          --init-path=/usr/libexec/docker/docker-init-current \
          --seccomp-profile=/etc/docker/seccomp.json \
          -H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
          $REGISTRIES

重新加载服务

[root@localhost /]# systemctl daemon-reload 

重启docker服务

[root@localhost /]# service docker restart

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

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

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

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

FROM maven

RUN mkdir -p /app/regcenter

ADD ./target/REGCENTER-0.0.1-SNAPSHOT.jar /app/regcenter

WORKDIR /app/regcenter

EXPOSE 8761

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方法类

<build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <repository>regcenter</repository>
                    <tag>jiaozi</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
            <plugin>  
	          <groupId>org.springframework.boot</groupId>  
	          <artifactId>spring-boot-maven-plugin</artifactId>  
	                <configuration>  
	                    <mainClass>cn.ps.RegServerMain</mainClass>  
	                </configuration>  
	            </plugin>  
        </plugins>
</build>

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

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

mvn package

执行生成镜像 
 

mvn clean package dockerfile:build -DskipTests

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

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

 启动镜像 道出8761端口

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

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

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

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

<build>
        <plugins>
            <plugin>
		      <groupId>com.spotify</groupId>
		      <artifactId>docker-maven-plugin</artifactId>
		      <version>1.1.1</version>
		      <configuration>
		        <imageName>regcenter1</imageName>
		        <dockerHost>http://192.168.1.238:2375</dockerHost>
		        <baseImage>maven</baseImage>
		        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
		        <!-- copy the service's jar file from target into the root directory of the image --> 
		        <resources>
		           <resource>
		             <targetPath>/</targetPath>
		             <directory>${project.build.directory}</directory>
		             <include>${project.build.finalName}.jar</include>
		           </resource>
		        </resources>
		      </configuration>
		    </plugin>
            <plugin>  
	          <groupId>org.springframework.boot</groupId>  
	          <artifactId>spring-boot-maven-plugin</artifactId>  
	                <configuration>  
	                    <mainClass>cn.ps.RegServerMain</mainClass>  
	                </configuration>  
	            </plugin>  
        </plugins>
</build>

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

mvn clean package docker:build -DskipTests

查看多了一个regserver1的镜像

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

猜你喜欢

转载自blog.csdn.net/liaomin416100569/article/details/81338604