英文:
Docker execution fails with "command in CMD/ENTRYPOINT not found"
问题
我正在尝试使用Docker容器构建我的Golang API的生产版本。在开发环境中(使用Air)可以工作,但在生产环境中却告诉我找不到可执行文件。
我已经测试过它,它已经正确编译并且存在,并且是可执行的。
我在Dockerfile中添加了文本以确保:
# 首先构建应用程序二进制文件
FROM golang:1.20-alpine
WORKDIR /app
COPY goapp/ .
RUN go mod download
RUN go build -o /app/executable
# 检查二进制文件是否存在并且可执行
RUN [ -f /app/executable -a -x /app/executable ]
CMD /app/executable
我使用docker-compose运行它:
version: '3.3'
services:
# db...
goapp:
build:
context: ./
dockerfile: ./docker/go/Dockerfile_prod
environment:
- SECRET=${SECRET}
- PORT=3000
- GO_ENV=production
- GIN_MODE=release
- MYSQL_HOST=${MYSQL_HOST}
- MYSQL_PORT=${MYSQL_PORT}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- SMTP_HOST=${SMTP_HOST}
- SMTP_USER=${SMTP_USER}
- SMTP_PASSWORD=${SMTP_PASSWORD}
- SENTRY_DSN=${SENTRY_DSN}
ports:
- 3000:3000
volumes:
- ./goapp:/app
- ./data/goapp:/app/data
depends_on:
db:
condition: service_healthy
我一直在使用docker-compose -f ... build
命令进行构建,并带有--no-cache
标志。
尝试将CMD
更改为ENTRYPOINT
,并尝试使用括号或不使用括号。还尝试过以下方式:
ENTRYPOINT ["/bin/sh", "-c", "/app/executable"]
但我一直得到以下错误:
goapp_1 | /bin/sh: /app/executable: not found
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;
# first build the app binary
FROM golang:1.20-alpine
WORKDIR /app
COPY goapp/ .
RUN go mod download
RUN go build -o /app/executable
# check if the binary exists and is executable
RUN [ -f /app/executable -a -x /app/executable ]
CMD /app/executable
I run it using docker-compose:
version: '3.3'
services:
# db...
goapp:
build:
context: ./
dockerfile: ./docker/go/Dockerfile_prod
environment:
- SECRET=${SECRET}
- PORT=3000
- GO_ENV=production
- GIN_MODE=release
- MYSQL_HOST=${MYSQL_HOST}
- MYSQL_PORT=${MYSQL_PORT}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- SMTP_HOST=${SMTP_HOST}
- SMTP_USER=${SMTP_USER}
- SMTP_PASSWORD=${SMTP_PASSWORD}
- SENTRY_DSN=${SENTRY_DSN}
ports:
- 3000:3000
volumes:
- ./goapp:/app
- ./data/goapp:/app/data
depends_on:
db:
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
ENTRYPOINT ["/bin/sh", "-c", "/app/executable"]
But I keep geting this:
goapp_1 | /bin/sh: /app/executable: not found
project_goapp_1 exited with code 127
Why is it not running and how can I make it work? Thanks.
答案1
得分: 2
卷:
- ./goapp:/app
在主机上,./goapp
被挂载到容器中的一个非空目录 (/app
)。/app
目录中已有的内容会被挂载覆盖。显然,这不是你想要的结果。
请参考 在容器中挂载到一个非空目录。
你应该选择要挂载到容器中的文件和目录,并将它们分别挂载。但是,如果你的应用程序依赖于这些文件和目录,更好的方法是修改 Dockerfile 将它们复制到镜像中。
英文:
volumes:
- ./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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论