环境
- 系统:mac OS 10.12
- docker:
docker-machine env default
- jdk 1.8
- maven 3.0
步骤
1.添加maven的docker构建插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<dockerHost>https://192.168.99.100:2376</dockerHost>
<dockerCertPath>/Users/lovedrose/.docker/machine/machines/default</dockerCertPath>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
简单说一下configuration里面的标签:
- imageName:镜像名称。注意:${docker.image.prefix} 为你在 docker官方仓库的用户名,如果你不需要上传镜像,随便填。
- dockerDirectory:Dockerfile位置
- dockerHost:docker容器的主机(这个在mac机器上很总要)
- dockerCertPath:客户端证书(这个在mac机器上很总要)
2.Dockerfile
在src/main/下创建docker目录,然后创建Dockerfile,即/src/main/docker/Dockerfile
FROM livingobjects/jre8
VOLUME /tmp
ADD [你的应用].jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
3.打包
mvn -e clean package docker:build
4.查看镜像
docker images
5.启动镜像
docker run -p [PORT]:[TO_PORT] -t [REPOSITORY]
6.停止镜像
先查看容器中的镜像:docker ps
然后
docker stop [CONTAINER ID]
7.删除镜像
docker rm [IMAGE ID]