我正在基于以下目录结构: https://github.com/golang-standards/project-layout
我创建了一个非常简单的应用程序,基本上我想将其容器化。 基本上我在那里有两个文件。 server.go是http端点的定义,而主文件则在cmd / webserver下启动名为main.go的服务器。
该项目的目录如下:
./
├── cmd
│ └── webserver
│ └── main.go
├── Dockerfile
├── go.mod
└── server.go
go.mod
module github.com/geborskimateusz/auth
go 1.15
一个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 .
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"]:
构建成功,但是问题是我运行时 docker run -p 3000:3000 geborskimateusz / auth 我得到了:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/dist/main\": permission denied": unknown.
ERRO[0000] error waiting for container: context canceled
我想念什么?我假设也许我需要将Dockerfile中的CD光盘放入放置main.go(可执行文件)的cmd / webserver中。
答案 0 :(得分:0)
我实际上是通过将DockerFile修改为
来解决此问题的FROM golang:alpine
WORKDIR /app
COPY go.mod .
RUN go mod download
COPY . .
RUN cd ./cmd/webserver/ && go build -o main . && cp main ../../ && cd ../../
CMD ["./main"]