docker 中部署spring cloud项目是比较流行的搭配,以下是配置过程记录
1. 先把spring boot项目打包
我这以我前面建立的microservice-eureka-server-8101项目为例
cd /home/linbin/workspace/microservice-eureka-server-8101
mvn install
注意打包的项目pom.xml有如下的内容才能自动生成打包清单
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包完成后会在目录下多了个target目录,里面有了打包后jar文件 microservice-eureka-server-8101-0.0.1-SNAPSHOT.jar
2. 在该目录下新建Dockerfile文件
[root@centos7 microservice-eureka-server-8101]# cat Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD microservice-eureka-server-8101-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8101
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
3. 创建影像 docker build -t springclouddemo/eureka:eureka8101 . #注意最后的. 点,表示当前目录
-
# docker build -t springclouddemo/eureka:eureka8101 .
-
Sending build context
to Docker daemon
46.36 MB
-
Step
1/
5 :
FROM openjdk:
8-jdk-alpine
-
Trying
to pull repository docker.io/library/openjdk ...
-
8-jdk-alpine: Pulling
from docker.io/library/openjdk
-
cd784148e348: Pull complete
-
35920a071f91: Pull complete
-
88b05767cad1: Downloading [================================> ]
88b05767cad1: Downloading
47.73 MB/
70.65 MB88b05767cad1: Pull complete
-
Digest: sha256:
4745e7b0cc551f68592f75a524f4479e878cb726ffc851dfad3e2c9280828b01
-
Status: Downloaded newer image
for docker.io/openjdk:
8-jdk-alpine
-
--->
2cfb1dc1f0c8
-
Step
2/
5 : VOLUME /tmp
-
---> Running
in
423589dc8991
-
---> caaa1fbb0bef
-
Removing intermediate container
423589dc8991
-
Step
3/
5 : ADD microservice-eureka-server
-8101
-0.0
.1-SNAPSHOT.jar app.jar
-
---> abe41148d340
-
Removing intermediate container
43fb43e3c94e
-
Step
4/
5 : EXPOSE
8101
-
---> Running
in
7c66ccf5021c
-
--->
6d5e9baf8e38
-
Removing intermediate container
7c66ccf5021c
-
Step
5/
5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
-
---> Running
in
83a072492725
-
--->
285be3969707
-
Removing intermediate container
83a072492725
-
Successfully built
285be3969707
因为 openjdk:8-jdk-alpine影像本地没有,会先自动下载。
根据下载的影像再加入指定的jar,生成新的影像
4. 运行
docker run -d -p 8101:8101 springclouddemo/eureka:eureka8101
把宿主机的8101端口映射到docker实例的8101端口
5.测试正常服务
6.查看镜像和实例
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springclouddemo/eureka eureka8101 285be3969707 30 minutes ago 149 MB
docker.io/openjdk 8-jdk-alpine 2cfb1dc1f0c8 8 days ago 103 MB
ubuntu hadoop 8dbec7cabd07 3 weeks ago 1.38 GB
ubuntu-java latest 3f7331f968a4 4 weeks ago 539 MB
docker.io/ubuntu 16.04 a51debf7e1eb 2 months ago 116 MB
docker.io/hello-world latest 4ab4c602aa5e 4 months ago 1.84 kB
[root@centos7 ~]# docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96f0222698f4 springclouddemo/eureka:eureka8101 "java -Djava.secur..." 28 minutes ago Up 20 minutes 0.0.0.0:8101->8101/tcp happy_darwin