“Docker compose –build” taking way longer than “docker build” and then failing to build

Working with Docker Desktop v4.30 for Windows in Windows 10. Docker is using WSL integration. I’m trying to get docker compose to build the image of a project instead of having to manually build them every time. The image builds correctly from PowerShell with docker build -t image_name ./source_folder in less than 20 seconds, the output is:

[+] Building 16.3s (14/14) FINISHED                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 548B                                                                               0.0s
 => [internal] load metadata for docker.io/library/node:18-alpine                                                  0.6s
 => [internal] load .dockerignore                                                                                  0.1s
 => => transferring context: 78B                                                                                   0.0s
 => [internal] load build context                                                                                  0.1s
 => => transferring context: 2.38kB                                                                                0.0s
 => [stage-1 1/4] FROM docker.io/library/node:18-alpine@sha256:4837c2ac8998cf172f5892fb45f229c328e4824c43c8506f8b  0.0s
 => CACHED [stage-1 2/4] WORKDIR /backend                                                                          0.0s
 => [stage-1 3/4] RUN rm -rf ./*                                                                                   0.5s
 => [builder 3/7] COPY ./package.json .                                                                            0.1s
 => [builder 4/7] COPY ./package-lock.json .                                                                       0.1s
 => [builder 5/7] RUN npm set strict-ssl false                                                                     1.2s
 => [builder 6/7] RUN npm install                                                                                 12.5s
 => [builder 7/7] COPY . .                                                                                         0.1s
 => [stage-1 4/4] COPY --from=builder /backend .                                                                   0.6s
 => exporting to image                                                                                             0.5s
 => => exporting layers                                                                                            0.5s
 => => writing image sha256:f3168093f381b74a0b39decf193564105863f826590eabfbcd18166545baf699                       0.0s
 => => naming to docker.io/image_name:1.0

However, when trying to do it via docker compose --build the build process stays a while in “load build context”, transferring about 3GB of data (compare with the previous command which transfers less than 3kB) and then fails because of something related to tar and/or unix file permissions (–verbose output):

time="2024-05-15T12:37:47+02:00" level=debug msg="using default config store "C:\\Users\\username\\.docker\\buildx""
[+] Building 86.3s (5/13)                                                                                docker:default
 => [backend internal] load build definition from Dockerfile                                                       0.0s
 => => transferring dockerfile: 548B                                                                               0.0s
 => [backend internal] load metadata for docker.io/library/node:18-alpine                                          0.9s
 => [backend internal] load .dockerignore                                                                          0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [backend builder 1/7] FROM docker.io/library/node:18-alpine@sha256:4837c2ac8998cf172f5892fb45f229c328e4824c43  0.0s
 => ERROR [backend internal] load build context                                                                   81.6s
 => => transferring context: 2.80GB                                                                               81.6s
------
 > [backend internal] load build context:
------
time="2024-05-15T12:37:47+02:00" level=debug msg="using default config store "C:\\Users\\username\\.docker\\buildx""
time="2024-05-15T12:37:47+02:00" level=debug msg="serving grpc connection"
time="2024-05-15T12:37:47+02:00" level=debug msg="stopping session" span="load buildkit capabilities"
time="2024-05-15T12:37:48+02:00" level=debug msg="stopping session"
time="2024-05-15T12:37:48+02:00" level=debug msg="serving grpc connection"
time="2024-05-15T12:37:48+02:00" level=debug msg="serving grpc connection"
time="2024-05-15T12:39:14+02:00" level=debug msg="stopping session"
failed to solve: archive/tar: unknown file mode ?rwxr-xr-x

Following some comments on github and here on SO of people having similar problems I tried doing it running the docker compose --build command from WSL itself, which managed to finish transferring the context (after almost 900 seconds!) but then failed because it couldn’t find the package-lock.json and/or package.json files:

DEBU[0000] Enabled Docker Desktop integration (experimental) @ unix:///var/run/docker-cli.sock
WARN[0000] /home/username/project_folder/compose.yaml: `version` is obsolete
DEBU[0000] using default config store "/home/username/.docker/buildx"
[+] Building 870.7s (8/13)                                                                               docker:default
 => [backend internal] load build definition from Dockerfile                                                       0.1s
 => => transferring dockerfile: 548B                                                                               0.0s
 => [backend internal] load metadata for docker.io/library/node:18-alpine                                          0.5s
 => [backend internal] load .dockerignore                                                                          0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [backend builder 1/7] FROM docker.io/library/node:18-alpine@sha256:4837c2ac8998cf172f5892fb45f229c328e4824c43  0.0s
 => [backend internal] load build context                                                                        869.5s
 => => transferring context: 2.81GB                                                                              869.1s
 => CACHED [backend builder 2/7] WORKDIR /backend                                                                  0.0s
 => ERROR [backend builder 3/7] COPY ./package.json .                                                              0.0s
 => ERROR [backend builder 4/7] COPY ./package-lock.json .                                                         0.0s
------
 > [backend builder 3/7] COPY ./package.json .:
------
------
 > [backend builder 4/7] COPY ./package-lock.json .:
------
DEBU[0000] using default config store "/home/username/.docker/buildx"
DEBU[0000] stopping session                              span="load buildkit capabilities"
DEBU[0000] serving grpc connection
DEBU[0000] stopping session
DEBU[0000] serving grpc connection
DEBU[0000] serving grpc connection
DEBU[0871] stopping session
failed to solve: failed to compute cache key: failed to calculate checksum of ref 1915df6a-49bc-4b75-9a0d-2b9df49060bd::v8ypouqpyva81dnwnbkrqnysp: "/package-lock.json": not found

My source_folder is 6’5MB, there isn’t a node_modules folder (and even if there was, it’s listed in my .dockerignore file), so I don’t know where the 3GB are coming from.

My Dockerfile is as follows:

FROM node:18-alpine as builder
WORKDIR /backend
COPY ./package.json .
COPY ./package-lock.json .
RUN npm set strict-ssl false
RUN npm install
COPY . .

FROM node:18-alpine
WORKDIR /backend
RUN rm -rf ./*
COPY --from=builder /backend .

EXPOSE 3977

CMD ["npm", "start"]

My compose.yaml is:

version: "2.23"
services:
  backend:
    build:
      context: ./
      dockerfile: ./source_folder/Dockerfile
    container_name: container_name
    image: image_name
    ports:
      - "127.0.0.1:3978:3978"
    networks:
      - mynetwork
    restart: always
    environment:
      - port=3978
    pull_policy: build

So what is going on here? What is docker compose --build doing differently from docker build that it requires 3 whole GB of build context? Why does it not find the package.json and package-lock.json files? Why does it fail earlier on PowerShell?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật