Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

docker - How to pass argument to ENTRYPOINT with variable expansion?

I'm building a docker image based on some other image that is out of my control. The base image sets an ENTRYPOINT I want to use by simply passing arguments via the CMD command. I also need to use runtime variable expansion in the arguments. Here's my Dockerfile

FROM base # I do not control base, but I need to call its ENTRYPOINT.

# The "as default parameters to ENTRYPOINT" form. Does not expand variables.
CMD [ "--port", "$PORT" ]

# The "exec" from. I can't use it, because I do not know path to the executable as the base image is outside my control.
CMD [ "executable", "--port", "$PORT" ]

# The "shell" form. Won't even build because "--port" is interpreted as flag of "CMD".
CMD --port $PORT

As explained in the code, none of the three forms offered by the docs works in my use case.

There are already similar question here on SO, but neither answers this particular use case. This one doesn't comprise variable expansion. And this one doesn't account for the inability to change the ENTRYPOINT.

question from:https://stackoverflow.com/questions/66055259/how-to-pass-argument-to-entrypoint-with-variable-expansion

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't use CMD with variable expansion in this case.

Docker on its own doesn't know how to expand variables in the container command. As you note, there are two syntaxes:

  1. CMD ["cmd", "$option"] literally passes cmd and $option as arguments with no expansion at all
  2. CMD cmd $option just wraps that string in a shell invocation, equivalent to CMD ["sh", "-c", "cmd $option"], and depends on that shell to do the expansion.

In a comment you mention a specific image. If you look at its generated history this uses the "container as command" pattern, where the ENTRYPOINT is a command to launch and the CMD is arguments to that command. Unless the command specifically expects to receive sh and -c as options, this won't work; but the sh -c wrapper is the only way Docker has to cause variable expansion at startup time.


You more specifically are trying to pass a --port option. That affects the port the process inside the container listens on. Since the container runs in an isolated network namespace, there's not usually a reason to need to change this: even if you have a dozen containers all listening on the standard HTTP port 80, they won't conflict.

If you want to connect from outside Docker to the container, you only need to change the docker run -p port mapping. Again, the image documentation suggests launching the image with docker run -p 8080:80 to cause it to be accessible from host port 8080; if you want a different host port, change that number, and leave the second port number the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...