打包方式有两种:一种是idea中引入docker相关插件并开放远程服务器上的docker端口进行远程打包,另外一种是直接通过服务器上的jdk进行docker-compose打包运行
配置服务器上的docker可被远程访问
vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
systemctl daemon-reload
systemctl restart docker
工程pom文件中加入插件,其中dockerHost的地址是linux中安装的docker地址
com.spotify
docker-maven-plugin
1.0.0
eureka:0.0.2
${project.basedir}
http://192.168.1.1:2375
/
${project.build.directory}
${project.build.finalName}.jar
然后在项目的根目录下新建Dockerfile的文件
FROM java:8
VOLUME /tmp
ADD eureka-0.0.2-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 7850
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
触发maven的build操作
成功之后远程到自己安装docker的服务器查看镜像
编写docker-compose.yml文件
version: '3'
services:
eureka:
restart: always
image: eureka:0.0.2
hostname: eureka
networks:
- finance-net
environment:
- TZ=Asia/Shanghai
ports:
- "8761:8761"
networks:
finance-net:
driver: bridge
执行命令docker-compose -f docker-compose.yml up -deureka即可
docker ps查看容器启动情况为up表示就没有问题
配置Dockerfile文件,需要事先下载一个jdk镜像
FROM carsharing/alpine-oraclejdk8-bash:latest
VOLUME /tmp
ADD ./eureka-1.0.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005","-jar","/app.jar"]
EXPOSE 8761
配置application.yml配置文件
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
instance:
prefer-ip-address: true #以IP地址注册到服务中心,相互注册使用IP地址
server:
enable-self-preservation: false # 关闭保护机制,默认true
eviction-interval-timer-in-ms: 2000 # 剔除失效服务间隔,默认60000
spring:
application:
name: eureka
moduleName: eureka
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
metrics:
tags:
application: ${spring.application.name}
export:
prometheus:
enabled: true
step: 1m
descriptions: true
配置docker-compose.yml文件
version: "3.7"
services:
eureka:
build:
context: ./eureka/
dockerfile: Dockerfile
image: eureka:0.0.1
container_name: eureka
network_mode: "host"
restart: always
environment:
SPRING_PROFILES_ACTIVE: test
TZ: Asia/Shanghai
REGISTER_URL: http://192.168.1.1:8761/eureka
SERVER_ID: eureka1
volumes:
- /app/eureka/application.yml:/config/application.yml
- /app/common/logback-spring.xml:/config/logback-spring.xml
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "6"
所有工作准备完毕
执行:docker-compose -f docker-compose.yml up -d --build eureka 即可