By
HHZH
Updated:
本地不需要安装docker,也可以构建docker镜像并推送到服务器,只需要用到docker的maven插件spotify下的docker-maven-plugin,更多配置访问docker-maven-plugin的github官网 。
1. 不使用Dockerfile
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
| <project> ... <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.build.finalName}</imageName> <baseImage>java</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <imageTags> <imageTag>latest</imageTag> </imageTags> <dockerHost>http://10.100.1.108:5000</dockerHost> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> ... </project>
|
2. 使用Dockerfile
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
| <project> ... <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.build.finalName}</imageName> <dockerDirectory>docker</dockerDirectory> <imageTags> <imageTag>latest</imageTag> </imageTags> <dockerHost>http://10.100.1.108:5000</dockerHost> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> ... </project>
|
其中${…}都是调用的maven的属性,${project.build.directory}默认为target/,${project.build.finalName}默认为${project.artifactId}-${project.version}。
3. 构建并推送
如果只想测试构建docker镜像,可以用如下命令:
mvn clean package docker:build
如果推送到私有仓库,可以用如下命令:
mvn clean package docker:build -DpushImage
如果想推送到服务器,还需要配置一下仓库所在服务器的用户名和密码,修改/apache-maven-3.5.0/conf/settings.xml文件,在标签下增加:
1 2 3 4 5 6 7 8
| <server> <id>docker-hub</id> <username>你的服务器用户名</username> <password>你的服务器密码</password> <configuration> <email>你的邮箱</email> </configuration> </server>
|
如果这样直接推送,会报错:
1 2
| [ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.13:build (default-cli) on project scmd-cascu: Exception caught: Request error: GET http://10.100.1.106:5000/version: 404: HTTP 404 Not Found -> [Help 1]
|
还需要暴露5000端口,编辑配置文件:
vim /etc/systemd/system/docker.service
增加这样一条启动命令 ExecStart=/usr/bin/dockerd -H tcp://10.100.1.106:5000 -H unix:///var/run/docker.sock