rocketmq-console-ng项目为例

    src/main目录下创建docker目录,在docker目录中创建Dockerfile文件,如下图所示:
    image.png
    Dockerfile文件内容为:

    1. FROM openjdk:8-jre-alpine
    2. RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    3. RUN echo 'Asia/Shanghai' >/etc/timezone
    4. VOLUME /tmp
    5. ADD rocketmq-console-ng-*.jar rocketmq-console-ng.jar
    6. RUN sh -c 'touch /rocketmq-console-ng.jar'
    7. ENV JAVA_OPTS=""
    8. ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /rocketmq-console-ng.jar" ]

    修改pom.xml

    1. <properties>
    2. ...
    3. <docker.image.prefix>wujiawei0926</docker.image.prefix>
    4. ...
    5. </properties>

    注意这里的docker.image.prefix应该改成自己的docker hub用户名

    1. <plugin>
    2. <groupId>com.spotify</groupId>
    3. <artifactId>docker-maven-plugin</artifactId>
    4. <version>0.4.11</version>
    5. <dependencies>
    6. <dependency>
    7. <groupId>javax.activation</groupId>
    8. <artifactId>javax.activation-api</artifactId>
    9. <version>1.2.0</version>
    10. <scope>compile</scope>
    11. </dependency>
    12. </dependencies>
    13. <configuration>
    14. <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
    15. <dockerDirectory>src/main/docker</dockerDirectory>
    16. <resources>
    17. <resource>
    18. <targetPath>/</targetPath>
    19. <directory>${project.build.directory}</directory>
    20. <include>${project.build.finalName}.jar</include>
    21. </resource>
    22. </resources>
    23. <imageTags>
    24. <imageTag>${project.version}</imageTag>
    25. <imageTag>latest</imageTag>
    26. </imageTags>
    27. </configuration>
    28. </plugin>

    docker-maven-plugin 已经不再维护,官方推荐dockerfile-maven来代替,此处不做介绍,使用方法大同小异

    mvn clean package,再mvn docker:bulid
    如果用的IDEA,按下图顺序依次等待执行:
    image.png
    控制台可以看到docker镜像打包时的日志,如下:
    image.png
    看到BUILD SUCCESS字样后,通过docker images -a命令查看镜像是否已经存在:

    1. wujiawei@wujiaweideMacBook-Pro novel % docker images -a
    2. REPOSITORY TAG IMAGE ID CREATED SIZE
    3. wujiawei0926/rocketmq-console-ng 2.0.0 f3c9bc0552cc 3 minutes ago 181MB
    4. wujiawei0926/rocketmq-console-ng latest f3c9bc0552cc 3 minutes ago 181MB
    5. <none> <none> 4cc06248c83a 3 minutes ago 181MB
    6. <none> <none> 82effb3e3dd0 3 minutes ago 181MB

    如果想要把该镜像推送到docker hub中,执行docker push wujiawei0926/rocketmq-console-ng即可,wujiawei0926/rocketmq-console-ng就是镜像的名称。
    如果没有登录docker的话,需要先docker login,输入用户名、密码后再进行推送。