docker shell form entrypoint如果没有在`docker run`中提供变量,将不会替换变量。

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

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 &lt;container-id&gt;

Within the Config dictionary, check the value of Env list that should contan also VERBO=5.

 &quot;Env&quot;: [
     &quot;PATH=...&quot;,
     &quot;VERBO=5&quot;
 ]

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
...

huangapple
  • 本文由 发表于 2023年6月15日 01:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476211.html
匿名

发表评论

匿名网友

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

确定