You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M1环境Docker Compose构建启动延迟问题求助

Docker Compose构建启动卡顿问题排查记录

问题描述

长期使用docker-compose up --build -d命令构建启动服务,此前从未出现延迟。今日通过make up_build触发该命令时,构建开始前出现约1分钟卡顿,停留在[+] Building 0.0s (0/0) docker:desktop-linux阶段,最终虽能完成构建启动,但卡顿为首次出现。

已执行的排查操作:

  • 重装Mac M1上的Docker Desktop
  • 执行docker system prune清理资源
  • 添加DOCKER_BUILDKIT=0后构建流程显示正常,但启动仍存在延迟
  • 临时解决方法:注释掉docker-compose.yml中的postgres:/var/lib/postgresql/data卷后,启动恢复正常

相关输出与配置文件

构建输出

Build authentication binary...
cd ../authentication-service && env GOOS=linux CGO_ENABLED=0 go build -o authApp ./cmd/api
Build api gateway binary...
cd ../api-gateway && env GOOS=linux CGO_ENABLED=0 go build -o apiGateway .
docker-compose down
[+] Running 4/3
 ✔ Container project-authentication-service-1  Removed                                                                                                                                                                                                          0.0s 
 ✔ Container project-api-gateway-1             Removed                                                                                                                                                                                                          0.1s 
 ✔ Container project-postgres-1                Removed                                                                                                                                                                                                          0.0s 
 ✔ Network project_default                     Removed                                                                                                                                                                                                          0.1s 
Building and starting docker images...
docker-compose up --build -d
[+] Building 0.0s (0/0)  docker:desktop-linux

完整构建完成输出

Build authentication binary...
cd ../authentication-service && env GOOS=linux CGO_ENABLED=0 go build -o authApp ./cmd/api
Build api gateway binary...
cd ../api-gateway && env GOOS=linux CGO_ENABLED=0 go build -o apiGateway .
docker-compose down
Building and starting docker images...
docker-compose up --build -d
[+] Building 63.5s (14/14) FINISHED                                                                                                                                                                                                             docker:desktop-linux
 => [api-gateway internal] load build definition from Dockerfile                                                                                                                                                                                               10.0s
 => => transferring dockerfile: 118B                                                                                                                                                                                                                           10.0s
 => [api-gateway internal] load .dockerignore                                                                                                                                                                                                                  10.0s
 => => transferring context: 70B                                                                                                                                                                                                                               10.0s
 => [authentication-service internal] load .dockerignore                                                                                                                                                                                                       10.0s
 => => transferring context: 2B                                                                                                                                                                                                                                10.0s
 => [authentication-service internal] load build definition from Dockerfile                                                                                                                                                                                    10.0s
 => => transferring dockerfile: 112B                                                                                                                                                                                                                           10.0s
 => [api-gateway internal] load metadata for docker.io/library/alpine:latest                                                                                                                                                                                   23.3s
 => [api-gateway auth] library/alpine:pull token for registry-1.docker.io                                                                                                                                                                                       0.0s
 => [authentication-service 1/3] FROM docker.io/library/alpine:latest@sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48                                                                                                                   0.0s
 => [api-gateway internal] load build context                                                                                                                                                                                                                  10.2s
 => => transferring context: 11.65MB                                                                                                                                                                                                                           10.2s
 => [authentication-service internal] load build context                                                                                                                                                                                                       10.2s
 => => transferring context: 15.02MB                                                                                                                                                                                                                           10.2s
 => CACHED [authentication-service 2/3] RUN mkdir /app                                                                                                                                                                                                          0.0s
 => CACHED [api-gateway 3/3] COPY apiGateway /app                                                                                                                                                                                                               0.0s
 => [api-gateway] exporting to image                                                                                                                                                                                                                            0.0s
 => => exporting layers                                                                                                                                                                                                                                         0.0s
 => => writing image sha256:171f01a47b5af04235e6dfbee9ac3f00ce1c50c7e6ac89699745aa1ddc78413b                                                                                                                                                                    0.0s
 => => naming to docker.io/library/project-api-gateway                                                                                                                                                                                                          0.0s
 => CACHED [authentication-service 3/3] COPY authApp /app                                                                                                                                                                                                       0.0s
 => [authentication-service] exporting to image                                                                                                                                                                                                                 0.0s
 => => exporting layers                                                                                                                                                                                                                                         0.0s
 => => writing image sha256:766dfb2e64100c9d4f1c5acaefef52f721bc7e1b4e57021f34be12907f0e3f4b                                                                                                                                                                    0.0s
 => => naming to docker.io/library/project-authentication-service                                                                                                                                                                                               0.0s
[+] Running 4/4
 ✔ Network project_default                     Created                                                                                                                                                                                                          0.1s 
 ✔ Container project-postgres-1                Started                                                                                                                                                                                                          0.3s 
 ✔ Container project-api-gateway-1             Started                                                                                                                                                                                                          0.3s 
 ✔ Container project-authentication-service-1  Created       

docker-compose.yml 文件

version: '3'

services:
  api-gateway:
    build:
      context: ./../api-gateway
      dockerfile: ./../api-gateway/Dockerfile
    restart: always
    ports:
      - '80:80'

  authentication-service:
    build:
      context: ./../authentication-service
      dockerfile: ./../authentication-service/Dockerfile
    depends_on:
      postgres:
        condition: service_healthy
    restart: always
    ports:
      - '8001:8001'
    deploy:
      mode: replicated
      replicas: 1
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_HOST: postgres
      POSTGRES_PORT: '5432'
      POSTGRES_DB: imsdb

  postgres:
    image: 'postgres:14.2'
    ports:
      - '5432:5432'
    restart: always
    deploy:
      mode: replicated
      replicas: 1
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: imsdb
    volumes:
      # using volumes instead of bind mounts
      - postgres:/var/lib/postgresql/data
      - ./schema.sql:/docker-entrypoint-initdb.d/schema.sql
    healthcheck:
      test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
      interval: 10s
      timeout: 2s

volumes:
  postgres:

Makefile

AUTHENTICATION_BINARY=authApp
APIGATEWAY_BINARY=apiGateway

up_build: build_authentication build_api_gateway
    docker-compose down
    @echo "Building and starting docker images..."
    docker-compose up --build -d
    @echo "Docker images built and started!"

build_authentication:
    @echo "Build authentication binary..."
    cd ../authentication-service && env GOOS=linux CGO_ENABLED=0 go build -o ${AUTHENTICATION_BINARY} ./cmd/api

build_api_gateway:
    @echo "Build api gateway binary..."
    cd ../api-gateway && env GOOS=linux CGO_ENABLED=0 go build -o ${APIGATEWAY_BINARY} .

down:
    docker-compose down

添加DOCKER_BUILDKIT=0后的输出

docker-compose up --build -d
Sending build context to Docker daemon  6.308MB
Step 1/5 : FROM alpine:latest
 ---> 1dc785547989
Step 2/5 : RUN mkdir /app
 ---> Using cache
 ---> 3b744c7dca2a
Step 3/5 : COPY apiGateway /app
 ---> Using cache
 ---> 3d3bcba5ef89
Step 4/5 : CMD ["/app/apiGateway"]
 ---> Using cache
 ---> b6fcdff5430f
Step 5/5 : LABEL com.docker.compose.image.builder=classic
 ---> Using cache
 ---> 8f67e117f601
Successfully built 8f67e117f601
Successfully tagged project-api-gateway:latest
Sending build context to Docker daemon  6.972MB
Step 1/5 : FROM alpine:latest
 ---> 1dc785547989
Step 2/5 : RUN mkdir /app
 ---> Using cache
 ---> 3b744c7dca2a
Step 3/5 : COPY authApp /app
 ---> Using cache
 ---> bbed0acb97b4
Step 4/5 : CMD ["/app/authApp"]
 ---> Using cache
 ---> 75a7a12ab1ce
Step 5/5 : LABEL com.docker.compose.image.builder=classic
 ---> Using cache
 ---> 6469ad6fb3a0
Successfully built 6469ad6fb3a0
Successfully tagged project-authentication-service:latest
[+] Running 4/4
 ✔ Network project_default                     Created                                                                                                                                                                                                          0.0s 
 ✔ Container project-api-gateway-1             Started                                                                                                                                                                                                          0.0s 
 ✔ Container project-postgres-1                Started                                                                                                                                                                                                          0.0s 
 ✔ Container project-authentication-service-1  Created 

内容的提问来源于stack exchange,提问作者Jessica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:54:53