英文:
docker shell form entrypoint not replacing variable if not provided by `docker run`
问题
我在查看这个答案https://stackoverflow.com/a/37904830/169252时,试图解决我的问题。
然而,对于我的情况似乎不起作用
ENV API="list,create,delete"
ENV ID="1-1"
ENV VERBO=5
ENTRYPOINT /myapp --id=$ID --api=$API --origin="*" --trace --metrics --verbosity=$VERBO --allow-insecure
然而,如果不通过docker run
命令提供,我总是得到-verbosity
标志的invalid value "":
docker run -p 80:8080 $DOCKER_IMAGE
。
然而,如果我这样运行
docker run -p 80:8080 -e VERBO=4 $DOCKER_IMAGE
,它似乎可以工作(在此之后有另一个问题,所以不能发布实际结果)。
我使用得对吗?想法是VERBO
是一个可选参数,并且不需要由docker run
设置-只有在需要时才这样做。
这是完整的Dockerfile:
英文:
I was looking at this answer https://stackoverflow.com/a/37904830/169252 to try to solve my problem.
However, it doesn't seem to work for my case
ENV API="list,create,delete"
ENV ID="1-1"
ENV VERBO=5
ENTRYPOINT /myapp --id=$ID --api=$API --origin="*" --trace --metrics --verbosity=$VERBO --allow-insecure
However, I always get invalid value "" for flag -verbosity:
if not provided by the docker run
command, i.e.
docker run -p 80:8080 $DOCKER_IMAGE
.
However, if I run it as
docker run -p 80:8080 -e VERBO=4 $DOCKER_IMAGE
, it seems to work (have another issue right now after this so can't post actual result).
Am I using this correctly? The idea is that VERBO
is an optional argument and doesn't need to be set by docker run
-only if so desired.
This is the complete Dockerfile:
FROM golang:1.19-alpine as builder
RUN apk add --no-cache make gcc musl-dev linux-headers git
WORKDIR /go/myapp
COPY . .
ARG GOPROXY
RUN go mod download
RUN make myapp
ENV API="list,create,delete"
ENV ID="1-1"
ENV VERBO=5
FROM alpine:latest
RUN apk add --no-cache ca-certificates
COPY --from=builder /go/myapp/build/myapp /
ENTRYPOINT /myapp --id=$ID --api=$API --origin="*" --trace --metrics --verbosity=$VERBO --allow-insecure
答案1
得分: 1
ENV VERBO=5
的声明是正确的,并将默认值设置为5。
要排除问题,运行容器并检查它:
docker run -p 80:8080 $DOCKER_IMAGE
docker ps -a
docker container inspect <container-id>
在Config
字典中,检查Env
列表的值,其中应该包含VERBO=5
。
"Env": [
"PATH=...",
"VERBO=5"
]
如果存在该键值对,表示您已正确设置默认值。
检查您的Dockerfile,您的问题与基础镜像的多次声明(FROM
命令)有关,这会在容器中创建多个上下文。
因此,VERBO值不是全局的,您需要在第二部分中也设置默认值:
...
FROM alpine:latest
ENV VERBO=5
...
英文:
The declaration of ENV VERBO=5
is correct and sets the default to 5.
To troubleshoot the problem, run the container and inspect it:
docker run -p 80:8080 $DOCKER_IMAGE
docker ps -a
docker container inspect <container-id>
Within the Config
dictionary, check the value of Env
list that should contan also VERBO=5
.
"Env": [
"PATH=...",
"VERBO=5"
]
If there is that key-value means you've set the default value correctly.
Checking your Dockerfile, your issue is related to the multiple declaration of the base image (FROM
command) that creates multiple contexts in your container.
So, the VERBO value is not global and you need to set the default value also in the second part:
...
FROM alpine:latest
ENV VERBO=5
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论