use mounted volume as entrypoint in docker-compose?

huangapple go评论82阅读模式
英文:

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) - use go build always
  • go build your go 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.

huangapple
  • 本文由 发表于 2023年3月22日 06:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75806864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定