Docker执行失败,显示“在CMD/ENTRYPOINT中找不到命令”。

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

Docker execution fails with "command in CMD/ENTRYPOINT not found"

问题

我正在尝试使用Docker容器构建我的Golang API的生产版本。在开发环境中(使用Air)可以工作,但在生产环境中却告诉我找不到可执行文件。
我已经测试过它,它已经正确编译并且存在,并且是可执行的。

我在Dockerfile中添加了文本以确保:

  1. # 首先构建应用程序二进制文件
  2. FROM golang:1.20-alpine
  3. WORKDIR /app
  4. COPY goapp/ .
  5. RUN go mod download
  6. RUN go build -o /app/executable
  7. # 检查二进制文件是否存在并且可执行
  8. RUN [ -f /app/executable -a -x /app/executable ]
  9. CMD /app/executable

我使用docker-compose运行它:

  1. version: '3.3'
  2. services:
  3. # db...
  4. goapp:
  5. build:
  6. context: ./
  7. dockerfile: ./docker/go/Dockerfile_prod
  8. environment:
  9. - SECRET=${SECRET}
  10. - PORT=3000
  11. - GO_ENV=production
  12. - GIN_MODE=release
  13. - MYSQL_HOST=${MYSQL_HOST}
  14. - MYSQL_PORT=${MYSQL_PORT}
  15. - MYSQL_USER=${MYSQL_USER}
  16. - MYSQL_PASSWORD=${MYSQL_PASSWORD}
  17. - MYSQL_DATABASE=${MYSQL_DATABASE}
  18. - SMTP_HOST=${SMTP_HOST}
  19. - SMTP_USER=${SMTP_USER}
  20. - SMTP_PASSWORD=${SMTP_PASSWORD}
  21. - SENTRY_DSN=${SENTRY_DSN}
  22. ports:
  23. - 3000:3000
  24. volumes:
  25. - ./goapp:/app
  26. - ./data/goapp:/app/data
  27. depends_on:
  28. db:
  29. condition: service_healthy

我一直在使用docker-compose -f ... build命令进行构建,并带有--no-cache标志。

尝试将CMD更改为ENTRYPOINT,并尝试使用括号或不使用括号。还尝试过以下方式:

  1. ENTRYPOINT ["/bin/sh", "-c", "/app/executable"]

但我一直得到以下错误:

  1. goapp_1 | /bin/sh: /app/executable: not found
  2. project_goapp_1 exited with code 127

为什么它无法运行,我该如何使其工作?谢谢。

英文:

I'm trying to make a production build of my Golang API with Docker container. It worked in Development (with Air), but keeps telling me, the executable was not found in production one.
I've tested it and it's correctly compiled and it exists and is executable.

I added the text to Dockerfile to be sure;

  1. # first build the app binary
  2. FROM golang:1.20-alpine
  3. WORKDIR /app
  4. COPY goapp/ .
  5. RUN go mod download
  6. RUN go build -o /app/executable
  7. # check if the binary exists and is executable
  8. RUN [ -f /app/executable -a -x /app/executable ]
  9. CMD /app/executable

I run it using docker-compose:

  1. version: '3.3'
  2. services:
  3. # db...
  4. goapp:
  5. build:
  6. context: ./
  7. dockerfile: ./docker/go/Dockerfile_prod
  8. environment:
  9. - SECRET=${SECRET}
  10. - PORT=3000
  11. - GO_ENV=production
  12. - GIN_MODE=release
  13. - MYSQL_HOST=${MYSQL_HOST}
  14. - MYSQL_PORT=${MYSQL_PORT}
  15. - MYSQL_USER=${MYSQL_USER}
  16. - MYSQL_PASSWORD=${MYSQL_PASSWORD}
  17. - MYSQL_DATABASE=${MYSQL_DATABASE}
  18. - SMTP_HOST=${SMTP_HOST}
  19. - SMTP_USER=${SMTP_USER}
  20. - SMTP_PASSWORD=${SMTP_PASSWORD}
  21. - SENTRY_DSN=${SENTRY_DSN}
  22. ports:
  23. - 3000:3000
  24. volumes:
  25. - ./goapp:/app
  26. - ./data/goapp:/app/data
  27. depends_on:
  28. db:
  29. condition: service_healthy

I've been building it with --no-cache flag on docker-compose -f ... build

Tried to change CMD for ENTRYPOINT and with brackets or without. Also tried

  1. ENTRYPOINT ["/bin/sh", "-c", "/app/executable"]

But I keep geting this:

  1. goapp_1 | /bin/sh: /app/executable: not found
  2. project_goapp_1 exited with code 127

Why is it not running and how can I make it work? Thanks.

答案1

得分: 2

  1. :
  2. - ./goapp:/app

在主机上,./goapp 被挂载到容器中的一个非空目录 (/app)。/app 目录中已有的内容会被挂载覆盖。显然,这不是你想要的结果。

请参考 在容器中挂载到一个非空目录

你应该选择要挂载到容器中的文件和目录,并将它们分别挂载。但是,如果你的应用程序依赖于这些文件和目录,更好的方法是修改 Dockerfile 将它们复制到镜像中。

英文:
  1. volumes:
  2. - ./goapp:/app

./goapp on the host machine is mounted into a non-empty directory (/app) on the container. The /app directory’s existing contents are obscured by the bind mount. Obviously, this is not what you want.

See Mount into a non-empty directory on the container.

You should pick the files and directories that you want to mount into the container and mount them separately. But if your app depends on those files and directories, a better approach is to modify the Dockerfile to copy them into the image.

huangapple
  • 本文由 发表于 2023年7月13日 15:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677083.html
匿名

发表评论

匿名网友

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

确定