Dockerfile 构建jar 镜像docker Dockerfile jar 包docker-compose
docker 的好处⾃不必多说,启动迅速,占⽤资源少,⽅便管理。把jar 包build 构建成docker 镜像,再结合docker-compose 管理docker 容器的启动,简直是开发Spring cloud 项⽬的最佳助⼿!
使⽤CentOS7
1.安装稳定版docker compose:
sudo curl -L "github/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2.⽂件授权
sudo chmod +x /usr/local/bin/docker-compose
3.查看是否安装成功
$ docker-compose --version
docker-compose version 1.25.0, build 1110ad01
⾃⾏准备⼀个可运⾏的jar 包即可,测试使⽤的是⼀个eureka 注册中⼼的项⽬jar 包。步骤:
1.创建springboot 项⽬
2.添加eureka 依赖
3.添加
application 配置
⼀、安装
docker 和compose
⼆、准备jar 包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
测试jar 包
Dockerfile: 构建docker 镜像配置
1. Dockerfile
FROM: 基础镜像,通过jdk8镜像开始
MAINTAINER: 维护者
ADD: 复制jar 包到镜像内,名字为app.jar
EXPOSE: 声明端⼝
ENTRYPOINT: docker 启动时,运⾏的命令.这⾥就是容器运⾏就启动jar 服务
进⼊Dockerfile 的同级⽬录,指定镜像构建:
docker build -t lin/eureka .
最后的点不要漏掉,lin/eureka 是给镜像起的名字
查看镜像: docker image ls
2. l
serviceUrl:
defaultZone: ${eureka.instance.hostname}:${server.port}/eureka/
三、编写配置⽂件⽬录结构
springframework jar包下载FROM java:8
MAINTAINER lin
ADD eurake-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8761
ENTRYPOINT ["java","-jar","app.jar"]REPOSITORY TAG IMAGE ID CREATED SIZE
lin/eureka latest f45c58780812 4 hours ago 691MB
redis latest dcf9ec9265e0 3 weeks ago 98.2MB java 8 d23bdf5b1b1b 2 years ago 643MB
version: '3'
services:
eureka:
image: lin/eureka
container_name: lin-eureka
services: 指定容器服务,下⾯可配多个服务
eureka: 服务名字,⾃⼰指定.这⾥就代表我的eureka 服务
image: 指定启动的镜像,就是上边通过Dockerfile 构建的镜像
container_name: 给容器起⼀个名字
restart: 可重启,使⽤always 就好
ports: 声明映射端⼝检查配置是否正确
docker-compose config 启动容器服务
docker-compose up 停⽌并删除容器服务
docker-compose down 强制启动重新建⽴容器
docker-compose up -d --force-recreate
3. 查看服务
查看进程: docker ps
查看⽇志: docker logs lin-eureka
docker-compose 配置,除了使⽤image 指定镜像之外,还可以使⽤build 把构建镜像和启动容器放在⼀起执⾏.
restart: always
ports:
- 8761:8761CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f21fdb5bb57c lin/eureka "java -jar app.jar" 4 hours ago Up 4 hours 0.0.0.0:8761->8761/tcp lin-eureka
2019-12-16 17:18:17.104 INFO 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.Confi gurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2019-12-16 17:18:17.482 INFO 1 --- [ main] com.lyf.eurake.EurakeApplication : No active profile set, falling back to default profiles: default 2019-12-16 17:18:19.690 WARN 1 --- [ main] o.s.dpoint.EndpointId : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-12-16 17:18:20.315 INFO 1 --- [ main] o.t.scope.GenericScope : BeanFactory id=6ff43722-b2c2-392e-b72c-6151fa54c3d d
2019-12-16 17:18:21.170 INFO 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.Confi
gurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is
四、补充docker-compose 配置
version: '3'
services:
eureka2:
build: ./eureka
image: lin-eureka:2
container_name: lin-eureka-2
restart: always
ports:
- 8762:8761
build: 指定构建⽬录,可以使⽤相对路径
image: 跟在build后⾯,指定构建镜像的名字
构建镜像并启动: docker-compose up -d
Creating network "work_default" with the default driver
Building eureka2
Step 1/5 : FROM java:8
-
--> d23bdf5b1b1b
Step 2/5 : MAINTAINER lin
---> Using cache
---> 79ef5083226e
Step 3/5 : ADD eurake-0.0.1-SNAPSHOT.jar app.jar
---> 7efd60a9f862
Step 4/5 : EXPOSE 8761
---> Running in 6968db6d10ad
Removing intermediate container 6968db6d10ad
---> a01061f73fe1
Step 5/5 : ENTRYPOINT ["java","-jar","app.jar"]
-
--> Running in 00c5dc7031e0
Removing intermediate container 00c5dc7031e0
---> 2ed328e41b1c
Successfully built 2ed328e41b1c
Successfully tagged lin-eureka:2
重新构建:
docker-compose build或者docker-compose up --build
查看镜像: docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE lin-eureka 2 2ed328e41b1c 49 seconds ago 691MB
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论