# sbDocker
为啥使用 docker 公司微服务需要启动太多,有两个优点吧!
1.方便管理,2.减少服务占用内存量
# 上手
# 新建Dockerfile文件如下目录
src目录下新建Dockerfile文件
# Dockerfile文件内容
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
::: tip ps
- FROM: 第一条指令必须是FROM指令 (环境依赖 jdk 版本等)
- VOLUME: 作用是创建在本地主机或其他容器可以挂载的数据卷,用来存放数据。
- ARG: 定义一个变量
- JAR_FILE: 为pom文件中项目定义的路径地址
- COPY: 复制本地主机src目录或文件到容器的desc目录,desc不存在时会自动创建。
- ENTRYPOINT: 每个Dockerfile中只能有一个ENTRYPOINT,当有多个时最后一个生效。
:::
### 修改pom
```xml
<properties>
<docker.image.prefix>springio</docker.image.prefix>
</properties>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 需要在maven中添加配置
(不加入可能会导致 docker:build 不识别) 否则会报 No plugin found for prefix 'docker' in the current project and in the plugin groups...
即在maven的setting.xml中添加配置
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.-->
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
1
2
3
4
5
2
3
4
5
# docker 配置需要更改(勾选)
否则会报 localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
Expose daemon on...(此项打勾)
1
# 最后命令
a.mvn package dockerfile:build(项目路径下)
b.docker 查看镜像 docker images
c.运行项目 docker run -d -p 8099:8099 springio/xlw_demo
1
2
3
2
3