docker-compose容器编排

GA666666 2022-11-22 PM 3630℃ 1条

一、背景

  • 背景:在小型公司中通常测试环境部署在开发者个人电脑中,一些固定服务占用了大量电脑内存,影响开发效率
  • 技术定位:初级
  • 目标群体:熟悉docker,了解Dockerfile和docker-compose
  • 技术应用场景:Docker 部署 Springboot
  • 整体思路:将一些不经常修改的服务和中间件整合部署在docker上
    二、操作步骤
    2.1 开发前的准备工作
    准备工作一
  • 安装docker : Docker安装教程
  • 安装docker-compose
    准备工作二
  • 将SpringBoot项目打包
    mvn clean package
  • 查看项目中target目录下是否生成jar包
    2.2 进入开发阶段
  • 创建Dockerfile:在target目录外src同级目录下创建Dockerfile,注意没有后缀名,MacOS & Linux建议使用 touch Dockerfile 命令来创建Dockerfile
  • 创建docker-compose.yml : 在项目文件夹下创建docker-compose.yml文件
    目录结构:
    docker-fatboy
    ├── docker-compose.xml
    ├── fatboy
    │   ├── Dockerfile
    │   └── target
    │   └── fatboy-1.0.0.jar
    ├── gateway
    │   ├── Dockerfile
    │   └── target
    │   └── gateway-1.0.0.jar
    └── system
    ├── Dockerfile
    └── target

      └── system-1.0.0.jar
    

2.3 编写配置文件

  1. DockerFile

    // 依赖环境
    FROM moxm/java:1.8-full

// 创建工作目录
RUN mkdir -p /system-service

WORKDIR /system-service

// Jar包路径
ARG JAR_FILE=target/system-service-1.0.0.jar

//把Jar包复制过来然后改个名
COPY ${JAR_FILE} app.jar

// 暴露端口,多个用空格隔开
EXPOSE 7102

// JDK 参数,这样写占用内存比较小
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"

// 运行部署命令,多个容器同时部署,可以配置CMD sleep 60;java -jar ...
CMD java -jar app.jar $JAVA_OPTS

docker-compose.yml

version: '3'

services:
gapro-nacos:

image: jerry6290/nacos-server
ports:
  - 8848:8848
restart: always
container_name: gapro-nacos
environment:
  - MODE=standalone
hostname: gapro-nacos

gapro-redis:

image: redis:7.0.0
ports:
  - 6379:6379
restart: always
container_name: gapro-redis
hostname: gapro-redis

gapro-gateway:

build:
  context: ./gateway
restart: always
ports:
  - 7101:7101
  - 7100:7100
container_name: gapro-gateway
image: gapro-gateway

gapro-system:

build:
  context: ./service/system-service
restart: always
ports:
  - 7102:7102
container_name: gapro-system
image: gapro-system

这个就是固定的模板了,这些参数就不过多赘述,这里用的redis是docker镜像,如果application配置文件中有宿主机的端口例如:127.0.0.1:6379,这个是访问不到的,可以将127.0.0.1修改为:docker.for.mac.host.internal,这里是mac的版本,linux和windows可以自行百度。
三、总结
将配置文件写好后,项目目录下运行docke-compose up -d 即可运行项目
231669104728_.pic.jpg
可以看到4个SpringCloud项目才占用260MB内存,1%的CPU,不用每次打开电脑启动一堆中间件了

标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~



唉呀 ~ 仅有一条评论


  1. GA666666
    GA666666 博主

    {!{data:image/webp;base64,UklGRtgVAABXRUJQVlA4WAoAAAAwAAAADwQAKwEASUNDUBgCAAAAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAA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