以rocketmq-console-ng项目为例
在src/main
目录下创建docker
目录,在docker
目录中创建Dockerfile
文件,如下图所示:Dockerfile
文件内容为:
FROM openjdk:8-jre-alpine
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
VOLUME /tmp
ADD rocketmq-console-ng-*.jar rocketmq-console-ng.jar
RUN sh -c 'touch /rocketmq-console-ng.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /rocketmq-console-ng.jar" ]
修改pom.xml
:
<properties>
...
<docker.image.prefix>wujiawei0926</docker.image.prefix>
...
</properties>
注意这里的docker.image.prefix
应该改成自己的docker hub用户名
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<imageTags>
<imageTag>${project.version}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
</configuration>
</plugin>
docker-maven-plugin 已经不再维护,官方推荐
dockerfile-maven
来代替,此处不做介绍,使用方法大同小异
先mvn clean package
,再mvn docker:bulid
如果用的IDEA,按下图顺序依次等待执行:
控制台可以看到docker镜像打包时的日志,如下:
看到BUILD SUCCESS
字样后,通过docker images -a
命令查看镜像是否已经存在:
wujiawei@wujiaweideMacBook-Pro novel % docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
wujiawei0926/rocketmq-console-ng 2.0.0 f3c9bc0552cc 3 minutes ago 181MB
wujiawei0926/rocketmq-console-ng latest f3c9bc0552cc 3 minutes ago 181MB
<none> <none> 4cc06248c83a 3 minutes ago 181MB
<none> <none> 82effb3e3dd0 3 minutes ago 181MB
如果想要把该镜像推送到docker hub中,执行docker push wujiawei0926/rocketmq-console-ng
即可,wujiawei0926/rocketmq-console-ng
就是镜像的名称。
如果没有登录docker的话,需要先docker login
,输入用户名、密码后再进行推送。