英文:
use mounted volume as entrypoint in docker-compose?
问题
我有一个Go项目(在cmd目录中),我通过docker-compose启动它:
version: "3"
services:
my-image:
build:
context: .
image: my_image
volumes:
- cmd:/cmd
depends_on:
- deps
ports:
- 8080:8080
entrypoint: ["go", "run", "./cmd/main.go"]
然而,我一直收到这个错误信息:
stat ./cmd/main.go: 没有那个文件或目录
有没有办法将cmd目录挂载为卷,并使用其中的文件作为入口点?
英文:
I have a go project (in the directory cmd) that I'm spinning up through docker-compose:
version: "3"
services:
my-image:
build:
context: .
image: my_image
volumes:
- cmd:/cmd
depends_on:
- deps
ports:
- 8080:8080
entrypoint: ["go", "run", "./cmd/main.go"]
However, I keep getting this message:
stat ./cmd/main.go: no such file or directory
is there a way I can mount the cmd directory as a volume and use a file in that as the entrypoint?
答案1
得分: 1
首先,有几件事情:
- 不要使用
go run
(它只适用于最简单的用例,并且以后会给你带来麻烦)-始终使用go build
- 将你的
go
二进制文件构建成一个Docker镜像-有很多资源可以教你如何做到这一点 - 更新你的
docker-compose.yaml
文件,引用这个预编译的镜像
回到你的挂载问题:如果你想让你的容器-特别是在docker compose
中-挂载本地目录,使用完全限定的路径:
volumes:
- /Users/me/some/path/cmd:/cmd
这将确保compose
可以在任何地方运行-并且不会对本地挂载的位置产生混淆。
英文:
Firstly, a number of things:
- don't use
go run
(it's meant for the most trivial of use cases and will burn you later) - usego build
always go build
yourgo
binary into a docker image - there's plenty of resources on how to do this- update your
docker-compose.yaml
to reference this precompiled image
Back to your mount issue: if you want your containers - especially in docker compose
- to mount local mounts, use fully qualified paths:
volumes:
- /Users/me/some/path/cmd:/cmd
this will ensure the compose
can be run from anywhere - and there's no confusion as to where the local mount is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论