• 单个Dockerfile合并多个镜像以及docker-compose批量部署


    合并多个镜像到单个Dockerfile

    FROM 后面跟上AS 生成阶段指定名称

    FROM openjdk:8 AS practice-service
    ARG logs=/logs
    ENV TZ Asia/Shanghai
    ENV address=${address} \
        port=${port} \
        namespace=${namespace} \
        logs=${logs}
    COPY ./practice.jar  /opt/practice.jar
    CMD ["java", "-Dfile.encoding=utf-8", "-Xms512M","-Xmx1024M","-jar", "/opt/practice.jar","--logging.file.name=${logs}/practice.log"]
    
    
    FROM openjdk:8 AS case-service
    ARG logs=/logs
    ENV TZ Asia/Shanghai
    ENV logs=${logs}
    COPY ./case.jar  /opt/case.jar
    CMD ["java", "-Dfile.encoding=utf-8", "-Xms512M","-Xmx1024M","-jar", "/opt/case.jar","--logging.file.name=${logs}/case.log"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    构建镜像

    执行下面的命令就能同时构建两个镜像了,其中 -t 是生成的镜像名称, --targetAS 后面生成阶段, 例如:

    docker build -t practice-service:1.0.0 --target practice-service .
    docker build -t case-service:1.0.0 --target case-service .
    
    • 1
    • 2

    关于AS

    默认情况,每个构建阶段是没有命名的,通过整型编号进行引用(类似数组下标索引,0-第一个FROM1-第二个FROM,依次类推, 以下面的Dockefile为例, 第二个COPY: 把前构建阶段–from=0/go/src/github.com/helloworld/app文件复制到 ./root/目录

    FROM golang:1.13.5 
    WORKDIR /go/src/github.com/helloworld/
    # RUN go get -d -v golang.org/x/net/html  
    RUN go get -d -v github.com/go-sql-driver/mysql
    COPY app.go    .
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
    
    FROM alpine:latest  
    RUN apk --no-cache add ca-certificates
    WORKDIR /root/
    COPY --from=0 /go/src/github.com/helloworld/app .
    CMD ["./app"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如果存在AS阶段名的话, Dockfile里面还可以通过 --from=<阶段名> 调用


    docker-compose批量构建镜像并启动

    version: "3.6"
    services:
      practice-service:
        build:  #build对应build镜像的操作
          context: .  #路径
          dockerfile: Dockerfile 
          target: practice-service  #对应镜像阶段
        image: practice-service:1.0.0 
        container_name: practice-service
        restart: always
        deploy:   #容器的资源限制 
          resources:
             limits:
                cpus: "2.00"
                memory: 5G
             reservations:  #初始化资源,只支持内存参数,CPU参数不支持
                memory: 200M
        volumes:
          - /home/sdp/bin/log/practice-service:/logs
          - /etc/localtime:/etc/localtime
        ports:
          - "8888:8888"
        environment:
          - TZ=Asia/Shanghai
          - logs=/logs
        logging:  # 日志大小限制
          driver: "json-file"
          options:
            max-size: "1000k"
            max-file: "20"
      case-service:
        build:
          context: .
          dockerfile: Dockerfile
          target: case-service
        image: case-service:1.0.0
        container_name: case-service
        restart: always
        volumes:
          - /home/sdp/bin/log/ase-service:/logs
          - /etc/localtime:/etc/localtime
        ports:
          - "8890:8890"
        environment:
          - TZ=Asia/Shanghai
          - logs=/logs
        logging:
          driver: "json-file"
          options:
            max-size: "1000k"
            max-file: "20"    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    docker-compose的target选项是在version: "3.4"之后才支持的, 不支持该参数的话需要升级更新, 具体介绍见 https://github.com/docker/compose ,安装步骤如下

    wget https://github.com/docker/compose/releases/download/v2.10.2/docker-compose-linux-x86_64
    mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
    chmod 755 /usr/local/bin/docker-compose
    source /etc/profile
    
    • 1
    • 2
    • 3
    • 4

    执行 docker-compose up --build -d 会在在后台启动容器并在启动前构建镜像
    docker-compose start <服务名> 可以启动单个服务
    docker-compose stop <服务名> 可以停止单个服务
    docker-compose down --rmi all会删除所有镜像和容器

    如果遇到下面的报错,可以先执行export DOCKER_BUILDKIT=0设置环境变量后再执行docker-cmpose的命令

    [root@node product]# docker-compose up
    [+] Running 0/2
     ⠿ practice-service Error                                                                                                                                                 2.8s
     ⠿ exam-service Error                                                                                                                                                     2.9s
    [+] Building 0.0s (0/0)                                                                                                                                                        
    listing workers for Build: failed to list workers: Unavailable: connection error: desc = "transport: Error while dialing unable to upgrade to h2c, received 404"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参考

    1. docker-compose限制容器cpu和内存
    2. Docker多阶段构建镜像
    3. https://github.com/containers/podman/issues/13889

  • 相关阅读:
    ASEMI整流桥UD4KB100,UD4KB100体积,UD4KB100大小
    里P7告诉你,接口测试真的很简单,有手就行
    mybatis 源码本地编译
    jQuery、vue、小程序、uni-app中的本地存储数据和接受数据是什么?
    YB203H系列是一组CMOS技术实现的三端低功耗高电压稳压器
    layui实现(学生)数据的增删改查
    Could not create the Java virtual machine解决
    【赠书第20期】AI绘画与修图实战:Photoshop+Firefly从入门到精通
    Linux 企业级夜莺监控分析工具远程访问
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题4题
  • 原文地址:https://blog.csdn.net/qq_26545503/article/details/126707380