这里有两种构建方式,一种是通过docker原生command来进行构建,另一种则是通过集成maven插件进行构建。
首先需要一个Dockerfile文件
FROM java:8
ADD demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
demo-0.0.1-SNAPSHOT.jar
添加到容器种,并修改名称为app.jar
注意:这里的jar包需在Dockerfile同目录或者子目录下,不然找不到
此时的目录结构为:
D:\docker\Dockerfile>dir
2019/07/31 10:12 <DIR> .
2019/07/31 10:12 <DIR> ..
2019/07/30 15:58 56,318,184 demo1-0.0.1-SNAPSHOT.jar
2019/07/29 16:16 49,392,759 demo-0.0.1-SNAPSHOT.jar
2019/08/01 10:34 198 Dockerfile
然后执行build命令
D:\docker\Dockerfile>docker build --tag=demo:1.0 .
Sending build context to Docker daemon 105.7MB
Step 1/4 : FROM java:8
---> d23bdf5b1b1b
Step 2/4 : VOLUME /tmp
---> Running in a557dc8afb78
Removing intermediate container a557dc8afb78
---> 6ac5921c3836
Step 3/4 : ADD demo-0.0.1-SNAPSHOT.jar app.jar
---> b19a4faddb87
Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Running in b6da1b5e5bf9
Removing intermediate container b6da1b5e5bf9
---> cfd2a5181c1f
Successfully built cfd2a5181c1f
Successfully tagged demo:1.0
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
build成功后可以查看当前所有镜像
D:\docker\Dockerfile>docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
demo 0.0.1-SNAPSHOT cfd2a5181c1f 2 minutes ago 693MB
demo1 0.0.1-SNAPSHOT 25eeb44db881 2 minutes ago 693MB
java 8 d23bdf5b1b1b 2 years ago 643MB
java latest d23bdf5b1b1b 2 years ago 643MB
在pom.xml
里加入插件:
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<!-- 详见:https://github.com/spotify/docker-maven-plugin -->
<imageName>${project.artifactId}:${project.version}</imageName>
<baseImage>java:8</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
这里这样配置之后就不需要Dockerfile了,如果想用Dockerfile来管理,可将
放开,即:
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<!-- 详见:https://github.com/spotify/docker-maven-plugin -->
<imageName>${project.artifactId}:${project.version}</imageName>
<dockerDirectory>src/main/resources</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
此时执行命令:
clean package -Dmaven.test.skip=true docker:build
即可构建成功。此时如果启动的话需要一个个执行启动命令
D:\docker\Dockerfile>docker run cfd2a5181c1f
D:\docker\Dockerfile>docker run 25eeb44db881
cfd2a5181c1f
为image ID,如果需要一次性多个容器运行,则应该使用Docker Compose
使用Compose需要三个步骤:
version: '3'
services:
demo1:
container_name: demo1
image: demo1:0.0.1-SNAPSHOT
ports:
- "8099:8099"
volumes:
- "D:/docker/log:/log"
networks:
- local-networks
env_file:
- demo.env
demo:
container_name: demo
image: demo:0.0.1-SNAPSHOT
ports:
- "9093:9093"
depends_on:
- demo1
networks:
- local-networks
env_file:
- demo.env
networks:
local-networks:
demo.env
#env file
demo1-uri=http://data-exchange:8099
这个配置完成之后,application.propertise
文件可使用${demo1-uri}
来进行解耦。
以上所有基础配置已完成,更多的的配置参考官方文档
执行命令启动compose,即可发现先启动demo,在启动demo1
D:\docker\Dockerfile>docker-compose up
如果想用idea远程调试docker容器里的代码,参考官方文档