Golang docker 容器无法在 Windows Home 上启动

我正在使用在 Docker 中启用了 WSL 的 Windows Home OS。


打印下面的构建消息后


docker build .

SECURITY WARNING: You are building a Docker image from Windows against a non-    Windows Docker host. All files and directories added to build context will have '-rwxr-    xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

当我尝试运行下面的图像消息时打印


docker run 49fee6f5a6bb -d

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

Dockerfile 内容如下


FROM golang:alpine

# Set necessary environmet variables needed for our image

ENV GO111MODULE=on \

    CGO_ENABLED=0 \

    GOOS=linux \

    GOARCH=amd64


# Move to working directory /build

WORKDIR /build


# Copy and download dependency using go mod

COPY go.mod .

COPY go.sum .

RUN go mod download


# Copy the code into the container

COPY . .


# Build the application

RUN go build -o main .


# Move to /dist directory as the place for resulting binary folder

WORKDIR /dist


# Copy binary from build to main folder

RUN cp /build/main .


# Export necessary port

EXPOSE 3000


# Command to run when starting the container

CMD ["/dist/main"]


慕运维8079593
浏览 86回答 1
1回答

LEATH

问题是命令的顺序(很可能):docker run -d yourcontainer应该管用一些不相关的建议:这是对您的 docker 文件的重写(未经测试)。如您所见,它更短,并且使用构建和运行阶段。FROM alpine:latest构建阶段使用一个更大的容器,里面有golang 、你的代码等。运行容器(例如,仅使用您的代码来强化 linux)。FROM golang:alpine AS buildWORKDIR /buildADD . /build# Depending on the golang version GO111MODULE can be removed as env variableRUN GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o main FROM alpine:latest# Export necessary portEXPOSE 3000# Add  applicationWORKDIR /distCOPY --from=build /build/main /dist/main
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go