如何处理使失踪从高山 Docker 形象?

rvpgvaaj  于 11个月前  发布在  Docker
关注(0)|答案(3)|浏览(95)

我的应用程序上有Makefiles,我在Makefiles上的所有命令都放在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

dxpyg8gm

dxpyg8gm1#

如果您添加了RUN apk add --no-cache make,但仍然存在问题,请将以下内容也添加到您的DockerFile:

RUN apk add g++

字符串
Alpine图像是轻量级的,没有实用程序,添加g++解决了这个问题。
参考号:https://mroldan.medium.com/alpine-sh-make-not-found-1e87ab87c56

gv8xihay

gv8xihay2#

这里有两件事我会在Dockerfile中修复:

  • 添加apk add make
  • CMD指令被修改为使用shell表单来链接make命令的执行。如果这些命令是替代或选项,请考虑使用入口点脚本或其他机制来选择它们,而不是尝试顺序执行两者。
# Start from the latest golang base image
    FROM golang:alpine
    
    # Install system dependencies including 'make'
    RUN apk update && apk add --no-cache gcc libc-dev make
    
    # 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 if the go.mod and go.sum files are not changed
    RUN go mod download
    
    # Copy the source from the current directory to the WORKDIR inside the container
    COPY . .
    
    # Build the Go app
    RUN go build .
    
    # Expose port 3000 to the outside world
    EXPOSE 3000
    
    # Use a single CMD instruction to run your make commands
    # If you have multiple make commands consider using a shell script or chaining commands
    CMD ["sh", "-c", "make -C scripts test && make -C scripts prod"]

字符串

kjthegm6

kjthegm63#

如果您希望避免将make的所有依赖项添加到您的alpine映像中,并保持运输容器的大小较小:

  • 在你的容器外构建你的二进制文件,只复制可运送的二进制文件到你的alpine容器中。
  • 在一个普通的golang容器中构建你的二进制文件,然后将二进制文件复制到一个小的可运送的alpine容器中。
  • 你可以给予https://github.com/go-task/task一个尝试,不需要太多的依赖相比,安装使在乌尔阿尔卑斯山容器和取代乌尔使文件与任务文件.

相关问题