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:
CMD ["cmd", "$option"]
literally passes cmd
and $option
as arguments with no expansion at all
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…