文章目录
- 环境准备
- 构建Docker镜像
- docker command构建
- maven插件构建
- Docker Compose构建
- docker-compose 文件
- run docker-compose
- 扩展
环境准备
- 首先需要安装Docker环境,如果是Mac或者Windows系统可从官网直接下载Docker Desktop,Linux用户可参考官网的安装示例。
- 准备两个SpringBoot项目demo和demo1,其中demo的接口会调用demo1的接口。
构建Docker镜像
这里有两种构建方式,一种是通过docker原生command来进行构建,另一种则是通过集成maven插件进行构建。
docker command构建
首先需要一个Dockerfile文件
FROM java:8
ADD demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- FROM: 当前镜像基于java8来构建
- ADD : 将
demo-0.0.1-SNAPSHOT.jar
添加到容器种,并修改名称为app.jar
- ENTRYPOINT:程序的入口点,这里即为执行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
maven插件构建
在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来管理,可将<dockerDirectory>src/main/resources</dockerDirectory>
放开,即:
<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
Docker Compose构建
使用Compose需要三个步骤:
- build docker image。这一步在上面已经完成
- 编写docker-compose文件
- run docker-compose。
docker-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:
- container_name: 容器名称
- image:镜像名称:版本号,先从本地寻找,如本地没有,会从DockerHub拉取
- ports: Host:Contanier 将主机端口映射到容器端口
- volumes:host绝对路径:容器绝对路径。将主机目录挂载到容器目录上。
- networks:网络配置,如上面2个容器使用了一个网络,代表他们可以进行通信
- env_file:环境变量文件,,格式为键值对
- depends_on:依赖关系,此处则为在启动demo时,会先启动demo1,但不会等待demo1启动完成。
demo.env
#env file
demo1-uri=http://data-exchange:8099
这个配置完成之后,application.propertise
文件可使用${demo1-uri}
来进行解耦。
以上所有基础配置已完成,更多的的配置参考官方文档
run docker-compose
执行命令启动compose,即可发现先启动demo,在启动demo1
D:\docker\Dockerfile>docker-compose up