如何处理 alpine docker 镜像中的 make 丢失问题?

问题描述 投票:0回答:2

我的应用程序上有 Makefile,所有命令都在 Makefile 上 我把它放在 Dockerfile 上:

# start from the latest golang base image
FROM golang:alpine


RUN apk update && apk add --no-cache gcc && apk add --no-cache libc-dev

# Set the current working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. they will be cached of the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the WORKDIR inisde the container
COPY . .

# Build the Go app
RUN go build .

# Exporse port 3000 or 8000 to the outisde world
EXPOSE 3000

# Command to run the executable
CMD ["make", "-C", "scripts", "test" ]
CMD ["make", "-C", "scripts", "prod" ]

并且得到了

docker: Error response from daemon: OCI runtime create failed: 
        container_linux.go:349: starting container process caused "exec: 
        \"make\": executable file not found in $PATH": unknown.

可以在 Docker 中运行

make -c scripts test
吗?如何正确地在 Docker 中使用此命令?

dockerfile
我跑步
golang:alpine

docker go makefile command-line dockerfile
2个回答
0
投票

如果您添加了

RUN apk add --no-cache make
但仍然存在问题,请将其也添加到您的 DockerFile 中:

RUN apk add g++

Alpine 图像是轻量级的,并且其中没有实用程序,添加 g++ 可以解决这个问题。您可以使用

which make
命令检查输出,对我来说它返回
/usr/bin/make

参考:https://mroldan.medium.com/alpine-sh-make-not-found-1e87ab87c56


-2
投票

如果您希望避免将 make 的所有依赖项添加到 alpine 映像并保持集装箱尺寸较小:

  • 在您的容器外部构建二进制文件,并仅将可交付的二进制文件复制到您的 alpine 容器
  • 在普通的 golang 容器中构建二进制文件,然后将二进制文件复制到小型可运输的 alpine 容器中
  • 您可以尝试一下https://github.com/go-task/task,与在您的 alpine 容器中安装 make 并用任务文件替换您的 make 文件相比,不需要太多依赖项。
© www.soinside.com 2019 - 2024. All rights reserved.