问题描述
构建镜像时出现
#14 0.579 container_linux.go:367: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory
Dockerfile
# syntax=docker/dockerfile:experimental
# Build the manager binary
FROM golang:1.15.6-alpine3.13 as base
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update && apk add --no-cache git tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& GOPROXY="https://goproxy.cn,direct" go mod download
FROM base as builder
ENV VERSION_PKG=test/pkg/version
# Build
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache/go-build \
GIT_VERSION=$(git describe --tags --dirty --always) && \
GIT_COMMIT=$(git rev-parse HEAD) && \
BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on \
go build -ldflags="-s -w -X ${VERSION_PKG}.GitVersion=${GIT_VERSION} -X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} -X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" -mod=readonly -a -o /out/manager main.go
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM alpine:3.13 as bin
WORKDIR /
COPY --from=builder /out/manager .
RUN chmod +x manager
ENTRYPOINT ["/manager"]
排查过程
排查中发现如果镜像不存在会出现该问题
需要pull对应的镜像然后执行 docker builder prune 清理builer cache。
未找到实际原因,但是发现清理对应的容器镜像并修改Dockerfile后解决
解决方案
# syntax=docker/dockerfile:experimental
# Build the manager binary
FROM golang:1.15.6 as base
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN GOPROXY="https://goproxy.cn,direct" go mod download
FROM base as builder
ENV VERSION_PKG=test/pkg/version
# Build
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache/go-build \
GIT_VERSION=$(git describe --tags --dirty --always) && \
GIT_COMMIT=$(git rev-parse HEAD) && \
BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on \
go build -ldflags="-s -w -X ${VERSION_PKG}.GitVersion=${GIT_VERSION} -X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} -X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" -mod=readonly -a -o /out/manager main.go
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM alpine:3.13 as bin
WORKDIR /
COPY --from=builder /out/manager .
RUN chmod +x manager
ENTRYPOINT ["/manager"]
粗暴的方法
docker system prune -a
有风险谨慎执行
原因?
目前尚未发现,经常出现这次成功下次失败的问题,怀疑和buildx 的build cache有关系,可能出现丢失文件层的问题,需要之后深究
问题2
报错
failed to solve: rpc error: code = Unknown desc = failed to compute cache key: failed to walk /data0/docker-run/tmp/buildkit-mount329656163/out: lstat /data0/docker-run/tmp/buildkit-mount329656163/out: no such file or directory
解决方案
修改生成景象的tag可已解决