maven-docker 插件 微服务镜像

使用Dockerfile进行构建

1. 先制作jdk8的镜像,作为基础镜像

  我用的commit的方式,或者参考我的另一个blog用dockerfile

2. 在您的工程 src/main/docker 目录建Dockerfile

FROM openjdk8:1.0.0
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN mkdir -p /root/fly
ADD cwebapp-service-1.0-SNAPSHOT.jar /root/fly/cwebapp-service-1.0-SNAPSHOT.jar
EXPOSE 8202
WORKDIR /root/fly
ENTRYPOINT ["java","-jar","cwebapp-service-1.0-SNAPSHOT.jar","--server.port=8202","--spring.profiles.active=test"]

3. 修改pom.xml

<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!-- imageName 要小写或者数字,大写会报错 -->
                    <imageName>镜像名称:label</imageName>
                    <!-- 指定Dockerfile所在的路径 -->
                    <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>

4. 结合jenkins。git 下载代码,执行: clean package docker:build -Dmaven.test.skip=true  打镜像

5.docker images 就能看到

mvn clean package docker:build 只执行 build 操作

mvn clean package docker:build -DpushImage 执行 build 完成后 push 镜像

mvn clean package docker:build -DpushImageTag 执行 build 并 push 指定 tag 的镜像 

将Docker镜像push制品库

1. 首先修改Maven的全局配置文件settings.xml,添加以下段落
<servers>
  <server>
    <id>docker-hub</id>
    <username>你的DockerHub用户名</username>
    <password>你的DockerHub密码</password>
  </server>
</servers>

2. pom中插件增加:

<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <imageName>镜像名称:tag</imageName>
                    <!-- 指定Dockerfile所在的路径 -->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                    <serverId>serverid</serverId>
                    <registryUrl>url地址</registryUrl>
                </configuration>
            </plugin>

3. jenkins中,命令:mvn clean package docker:build -DpushImage

构建好就推送成功了

启动镜像:

docker run -d -p8500:8202 33007ae32155

查看日志服务
docker logs -f --tail=10 d973230b7888

拉取

docker pull 镜像名称:tag

猜你喜欢

转载自blog.csdn.net/zhuchunyan_aijia/article/details/83146666
今日推荐