首先 idea 得安装插件maven。使用maven的package功能生成.jar文件。如图:
在项目中添加三个文件:application.yml、docker-compose.yml、Dockerfile,文件内容分别如下:
spring:
application:
name: eureka-service
---
server:
port: 8081
spring:
profiles: eureka-svc1
eureka:
instance:
hostname: eureka-svc1
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://eureka-svc2:8082/eureka/,http://eureka-svc3:8083/eureka/
register-with-eureka: true
---
server:
port: 8082
spring:
profiles: eureka-svc2
eureka:
instance:
hostname: eureka-svc2
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://eureka-svc1:8081/eureka/,http://eureka-svc3:8083/eureka/
register-with-eureka: true
---
server:
port: 8083
spring:
profiles: eureka-svc3
eureka:
instance:
hostname: eureka-svc3
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://eureka-svc2:8082/eureka/,http://eureka-svc1:8081/eureka/
register-with-eureka: true
version: '3'
services:
eureka-svc1:
build: .
container_name: eureka-svc1
networks:
- eureka-net
ports:
- 7001:7001
environment:
- spring.profiles.active=eureka-svc1
eureka-svc2:
build: .
container_name: eureka-svc2
networks:
- eureka-net
ports:
- 7002:7002
environment:
- spring.profiles.active=eureka-svc2
eureka-svc3:
build: .
container_name: eureka-svc3
networks:
- eureka-net
ports:
- 7003:7003
environment:
- spring.profiles.active=eureka-svc3
networks:
eureka-net:
driver: bridge
FROM openjdk:8-alpine
ENV TARGET_JAR="工程jar包名字.jar"
ADD ./${TARGET_JAR} /${TARGET_JAR}
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone
CMD java -Xrs -Xmx1024m -jar ${TARGET_JAR} --eureka.hostUri=${EUREKAHOSTURI}
本人使用工具Xftp上传,新建了个目录存放在这些文件,如下图:
服务器上必须得先安装Docker,然后再安装 Docker-compose,这样就具备了创建镜像与容器的条件。
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Docker-compose up -d --build
docker images
docker ps -a