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

基于scratch基础镜像使用RUN指令构建镜像失败求助

Why Your scratch Image Build Fails with RUN

Hey there, let's break down exactly what's going wrong here and how to fix it. The error you're hitting comes down to a core detail about the scratch image: it's completely empty. There are no operating system files, no utilities, and most importantly, no shell (like /bin/sh or /bin/bash) included.

When you run a RUN command without specifying the exec format, Docker automatically tries to wrap your command in a shell—so it attempts to execute something like /bin/sh -c "mkdir hello". But since /bin/sh doesn't exist in the blank scratch image, the runtime throws that "stat /bin/sh: no such file or directory" error you saw.


Fixes to Try

1. Use the Exec Format for RUN (If You Must Stick to scratch)

If you need to stay with the scratch base image, you can use the exec-style RUN command, which skips the shell entirely. But there's a catch: you'll need to have the mkdir binary available in the image first (since scratch doesn't include any tools by default).

Here's how to do it (note: you'll need to copy the mkdir binary from a working Linux system to your build directory first):

FROM scratch
# Copy the mkdir binary from your host machine (e.g., from /bin/mkdir on Ubuntu)
COPY mkdir /bin/mkdir
# Exec format: pass the command and arguments as a list
RUN ["/bin/mkdir", "hello"]

2. Switch to a Minimal Linux Base Image (Easier Option)

For most scenarios where you need to run shell commands like mkdir, scratch is overkill. Instead, use a lightweight Linux image like alpine (it's only ~5MB and includes a shell and basic utilities out of the box):

FROM alpine:latest
RUN mkdir hello

This will build successfully without any extra steps.


Quick Note on scratch

Keep in mind: scratch is designed for building ultra-minimal images that only contain a single static binary (like a compiled Go program). If you need to run standard shell commands or rely on system tools, it's not the right choice for your use case.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 22:08:14