英文:
bash: No such file or directory when running a dockerfile
问题
我正在尝试从一个名为dockerfile
和一个名为docker-compose
的文件中运行一个容器,它们的内容如下所示 -
docker-compose.yml
build:
context: .
dockerfile: docker/Dockerfile
target: go-container
entrypoint: bash -c "./main"
Dockerfile
的内容如下 -
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ENV GOPATH /gopath
ENV PATH $GOPATH/bin:$PATH
WORKDIR /gopath/src/project
ADD go.mod go.sum ./
RUN go mod download -x
RUN go build -o main cmd/main.go
FROM scratch as another_container
COPY --from=go-container /gopath/src/project/main .
ENTRYPOINT ["./main"]
运行docker-compose
时,我遇到了以下错误 -
bash: line 1: ./main: No such file or directory
ERROR: 127
这里发生了什么?我不知道如何调试它,所以任何帮助都将不胜感激!
英文:
I'm trying to run a container from a dockerfile
and a docker-compose
file which look like this -
docker-compose.yml
build:
context: .
dockerfile: docker/Dockerfile
target: go-container
entrypoint: bash -c "./main"
the Dockerfile
looks like this -
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ENV GOPATH /gopath
ENV PATH $GOPATH/bin:$PATH
WORKDIR /gopath/src/project
ADD go.mod go.sum ./
RUN go mod download -x
RUN go build -o main cmd/main.go
FROM scratch as another_container
COPY --from=go-container /gopath/src/project/main .
ENTRYPOINT ["./main"]
On running the docker-compose
, I get an error like this -
bash: line 1: ./main: No such file or directory
ERROR: 127
What is happening here? I don't know how to debug it so any help is appreciated!
答案1
得分: 3
从零开始作为另一个容器
你正在使用一个FROM语句,它会用一个基于镜像的新容器替换当前容器。在这种情况下,你使用的是FROM scratch
,它是一个空容器。
由于你没有使用COPY --from
从前一个阶段复制编译好的文件,所以./main
文件丢失,因此你的应用程序无法正常工作。
请参考这里了解多阶段构建Go应用程序的示例。
英文:
FROM scratch as another_container
You're using a FROM statement, which replaces the current container with a new container based on an image. In this case, you're doing it FROM scratch
, which is a container with nothing in it.
Since you didn't use COPY --from
to copy the compiled file from the previous stage, the file ./main
is missing, so your application won't work.
See here for a multistage Go build example.
答案2
得分: 1
Docker的scratch
镜像不包含诸如bash
之类的二进制文件,基本上是一个空的上下文。然后,FROM scratch as another_container
指令会删除之前的层;在这种情况下,./main
文件被排除在外。
你可以使用另一个最小化的镜像,比如FROM busybox as another_container
,来执行./main
二进制文件。
例如:
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ADD go.mod go.sum ./
RUN go mod download -x
RUN go build -o main cmd/main.go
FROM busybox as another_container
COPY --from=go-container ./main /main
ENTRYPOINT ["./main"]
完整示例:
docker-compose.yaml
version: "3.9"
services:
web:
build:
context: .
dockerfile: Dockerfile
target: go-container
entrypoint: bash -c "./main"
Dockerfile
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ENV GOPATH /gopath
ENV PATH $GOPATH/bin:$PATH
WORKDIR /gopath/src/project
ADD go.mod go.sum main.go ./
RUN go mod download -x
RUN go build -o main main.go
FROM busybox as another_container
COPY --from=go-container /gopath/src/project/main .
ENTRYPOINT ["./main"]
main.go
package main
import "fmt"
func main() {
fmt.Println("Created with vim-go")
}
使用以下命令执行:docker compose up
docker compose up
[+] Running 2/2
⠿ Network delete_default Created 0.0s
⠿ Container delete-web-1 Created 0.0s
Attaching to delete-web-1
delete-web-1 | vim-go
delete-web-1 exited with code 0
英文:
Docker scratch
doesn't have binaries such as bash
, basically it's an empty context. The FROM scratch as another_container
instruction then removes previous layers; in this case ./main
file is excluded.
You could use another minimal image, ie FROM busybox as another_container
, to execute the ./main
binary.
ie:
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ADD go.mod go.sum ./
RUN go mod download -x
RUN go build -o main cmd/main.go
FROM busybox as another_container
COPY --from=go-container ./main /main
ENTRYPOINT ["./main"]
Full example:
docker-compose.yaml
version: "3.9"
services:
web:
build:
context: .
dockerfile: Dockerfile
target: go-container
entrypoint: bash -c "./main"
Dockerfile
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ENV GOPATH /gopath
ENV PATH $GOPATH/bin:$PATH
WORKDIR /gopath/src/project
ADD go.mod go.sum main.go ./
RUN go mod download -x
RUN go build -o main main.go
FROM busybox as another_container
COPY --from=go-container /gopath/src/project/main .
ENTRYPOINT ["./main"]
main.go
package main
import "fmt"
func main() {
fmt.Println("Created with vim-go")
}
Execute with: docker compose up
docker compose up
[+] Running 2/2
⠿ Network delete_default Created 0.0s
⠿ Container delete-web-1 Created 0.0s
Attaching to delete-web-1
delete-web-1 | vim-go
delete-web-1 exited with code 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论