使用docker-maven-plugin插件将项目编译为docker镜像到远程linux服务器

转自: https://blog.youkuaiyun.com/lvyuan1234/article/details/69255944

在win10中使用idea开发一个模块化的maven项目,然后想要把该项目直接编译到远程linux服务器的docker中,具体做法如下:


       第一:在各模块中的pom文件中加入以下

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
        <imageName>${project.name}:${project.version}</imageName>
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <skipDockerBuild>false</skipDockerBuild>
        <resources>
            <resource>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
       第二:在各模块的src/main下面新建docker包,在src/main/docker下面新建Dockerfile与runboot.sh文件(文件内容因情况不同就不贴了),结构如下

       第三:在linux服务器上装好docker,我的linux版本为centos7,这里不建议使用#yum install docker方式安装,因为这种方式安装的版本比较旧,而且在配置远程api时要好多错误,建议使用方式

#curl -fsSL https://get.docker.com/ | sh或#yum install docker-engine,这里以17.0.3-ce版本为例,使用

#docker version命令即可验证是否安装成功,接下来要想在windows中操作远程linux中的docker,那前提是必须

开启docker远程API,修改docker配置文件#vi /usr/lib/systemd/system/docker.service   ,进入编辑模式后,将ExecStart这一行后面加上 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock ,改完后如下所示

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock  ,这里就写4个0,你可别改成自己的ip哦,保存后退出,重新加载配置文件#systemctl daemon-reload   ,启动docker #systemctl start docker  ,

输入#netstat -anp|grep 2375 显示docker正在监听2375端口,输入#curl 127.0.0.1:2375/info  显示一大堆信息,证明远程api就弄好了

       第四:在windows系统环境变量中新建DOCKER_HOST,值为tcp://10.100.74.220:2375,(你改成你自己的docker服务器ip地址)


       第五步:打开dos窗口,即cmd命令行,进入到你要编译的项目文件夹下,

输入mvn clean package docker:build -DskipTests ,然后慢慢等待,直到最后build成功

       第六步:登陆linux,输入#docker images 发现自己的项目已经被编译成镜像了,可以启动容器运行镜像了,也相当于完成了项目的云部署
--------------------- 
作者:风雨诗轩 
来源:优快云 
原文:https://blog.youkuaiyun.com/lvyuan1234/article/details/69255944 
版权声明:本文为博主原创文章,转载请附上博文链接!

docker maven plugin 是个简单的可以管理Docker容器maven插件,这个插件将会根据你的配置,在构建时启动容器,构建结束时停止容器并删除,如果本地找不到镜像Docker会自动去中央仓库下载。 简单示例:     com.ofbizian     docker-maven-plugin     1.0.0                                            busybox                                                      start-docker             pre-integration-test                              start                            所有可能配置的完整示例:     com.ofbizian     docker-maven-plugin     1.0.0              http://localhost:4243                                       dockerfile/redis                                      <![CDATA[                     {"Hostname":"",                         "PortSpecs":null,                         "User":"",                         "Tty":false,                         "OpenStdin":false,                         "StdinOnce":false,                         "Memory":0,                         "MemorySwap":0,                         "CpuShares":0,                         "AttachStdin":false,                         "AttachStdout":false,                         "AttachStderr":false,                         "Env":null,                         "Cmd":null,                         "Dns":null,                         "Volumes":null,                         "VolumesFrom":"",                         "Entrypoint":[                         ],                         "NetworkDisabled":false,                         "Privileged":false,                         "WorkingDir":"",                         "Domainname":"",                         "ExposedPorts":null,                         "OnBuild":null}                     ]]>                                                       <![CDATA[                     {"ContainerIDFile": null, "LxcConf": null, "Links": null, "PortBindings": {                         "6379/tcp": [                             {                                 "HostIp": "0.0.0.0",                                 "HostPort": "6379"                             }                         ]                     }, "Privileged": false, "PublishAllPorts": false}                     ]]>                                                            busybox                                                      start-docker             pre-integration-test                              start                                            stop-docker             post-integration-test                              stop                            标签:Docker
`docker-maven-plugin` 是 Maven 插件之一,用于将 Java 应用程序,包括基于 Spring Boot 的项目,构建为 Docker 镜像。以下是使用这个插件将应用打包并推送到私有仓库的基本步骤: 1. **添加插件依赖**:首先,在 `pom.xml` 文件的 `<build>` 标签内添加 `docker-maven-plugin` 的依赖配置,例如: ```xml <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.15</version> <!-- 更新到最新版本 --> </plugin> </plugins> ``` 2. **配置镜像信息**:在 `<project>` 节点下设置 Dockerfile 相关的配置,如镜像名称、标签、基础镜像等。这里假设你有一个名为 `application.jar` 的主程序包文件: ```xml <build> <plugins> <plugin> <configuration> <imageName>${project.artifactId}</imageName> <imageTags> <tag>${project.version}</tag> <tag>latest</tag> <!-- 如果需要常量更新的标签 --> </imageTags> <baseImage>openjdk:8-jdk-alpine</baseImage> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> ``` 3. **私有仓库配置**:如果你有一个私有Docker注册表(如JFrog Artifactory),你需要配置 MavenDocker 接口,通常通过环境变量 `DOCKER_REGISTRY_URL` 和 `DOCKER_USERNAME`、`DOCKER_PASSWORD` 或者认证密钥: ```xml <profiles> <profile> <id>docker-push</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <dockerPushRepository>your-private-repo:5000</dockerPushRepository> <!-- 替换为你的私有仓库地址 --> </properties> <build> <plugins> <plugin> <executions> <execution> <goals> <goal>push</goal> </goals> <phase>deploy</phase> </execution> </executions> <configuration> <registryUrl>${dockerPushRepository}</registryUrl> <username>your-docker-registry-username</username> <password>your-docker-registry-password</password> <!-- 或者从安全环境下获取密钥 --> </configuration> </plugin> </plugins> </build> </profile> </profiles> ``` 4. **构建和推送镜像**:运行 `mvn clean package docker:build docker:push -Pdocker-push` 来构建并推送镜像
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值