php小編魚仔在使用容器技術(shù)時,可能會遇到一個常見問題:文件復(fù)制到容器后找不到。這個問題通常出現(xiàn)在使用Docker等容器化平臺時。原因可能是文件路徑設(shè)置不正確,或者容器內(nèi)部的文件系統(tǒng)與宿主機不一致。解決這個問題的方法有多種,比如使用絕對路徑,或者通過共享文件夾的方式將文件復(fù)制到容器中。在解決這個問題之前,我們需要先了解容器的文件系統(tǒng)和文件路徑的映射關(guān)系,以及如何正確配置文件路徑。
問題內(nèi)容
我有一個如下所示的 dockerfile:
from golang:1.19 as builder workdir /app copy . . run cgo_enabled=0 go build -v -o "hello-bin" #from scratch from alpine copy --from=builder /app/hello-bin /app/hello-bin copy --from=builder /app/start.sh /app/start.sh workdir /app entrypoint [ "./start.sh" ]
登錄后復(fù)制
它只是從 hello-world go 文件構(gòu)建一個二進制文件。但是,當(dāng)我嘗試使用以下 docker-compose 設(shè)置運行此容器時,它顯示 exec ./start.sh: no such file or directory。
version: "3"
services:
hello:
restart: always
build:
context: .
dockerfile: dockerfile
登錄后復(fù)制
目錄結(jié)構(gòu)為
? tree . . ├── dockerfile ├── docker-compose.yaml ├── go.mod ├── hello ├── init.go └── start.sh
登錄后復(fù)制
因此 start.sh 應(yīng)通過 copy 加載到 行也是如此,它應(yīng)該通過 builder 容器中。 .copy --from=builder /app/start.sh /app/start.sh 傳遞到第二個容器。
上下文,start.sh的內(nèi)容如下:
#!/bin/bash
_main() {
echo "start"
}
_main "$@"
登錄后復(fù)制
我最困惑的部分是,如果我在 dockerfile 中將其更改為 cmd [ "ls", "-l" ] ,它實際上會打印出來
awesomeproject3-hello-1 | -rwxr-xr-x 1 root root 1819562 May 19 02:41 hello-bin awesomeproject3-hello-1 | -rwxrwxrwx 1 root root 51 May 19 02:39 start.sh
登錄后復(fù)制
如果我在 dockerfile 中將其更改為 entrypoint [ './hello-bin' ] ,二進制文件也會成功運行。我只是不明白為什么它說沒有 ./start.sh。
更新:受@larsks的啟發(fā),我注意到如果我將 ./start.sh 的標(biāo)頭從 #!/bin/bash 更改為 #!/bin/sh,它會神奇地工作。我仍然很困惑這里的根本問題是什么,如果我想保留 bash 標(biāo)頭,我應(yīng)該如何修復(fù) docker 文件?
解決方法
發(fā)生錯誤是因為您在 start.sh 中使用了 #!/bin/bash。
Alpine docker 鏡像默認(rèn)沒有安裝 bash。它使用 Busybox shell 代替。
您可以在容器中安裝 bash 。請參閱 Docker:如何將 bash 與 Alpine 一起使用基于docker鏡像?
或者您可以將 #!/bin/bash 更改為 #!/bin/sh,如您在問題評論中所述。






