ENTRYPOINT and CMD with build arguments
ENTRYPOINT and CMD with build arguments
This doesn't work:
FROM alpine:3.7
# build argument with default value
ARG PING_HOST=localhost
# environment variable with same value
ENV PING_HOST=${PING_HOST}
# act as executable
ENTRYPOINT ["/bin/ping"]
# default command
CMD ["${PING_HOST}"]
It should be possible to build an image with build-arg and to start a container with an environment variable to override cmd as well.
docker build -t ping-image .
docker run -it --rm ping-image
Error: ping: bad address '${PING_HOST}'
UPDATE:
FROM alpine:3.7
# build argument with default value
ARG PING_HOST=localhost
# environment variable with same value
ENV PING_HOST ${PING_HOST}
COPY ./entrypoint.sh /usr/local/bin/
# act as executable
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
# default command
CMD $PING_HOST
entrypoint.sh
#!/bin/sh
/bin/ping $PING_HOST
This works because the entrypoint.sh enables variable substitution as expected.
It's true that
CMD
executes the given command literally (no variable or other shell metacharacter expansion). This is true regardless of wheter or not you are using build arguments. What is your question?– larsks
2 days ago
CMD
1 Answer
1
For CMD to expand variables, you need to arrange for a shell because shell is responsible for expanding environment variables, not Docker. You can do that like this:
ENTRYPOINT ["/bin/sh"]
CMD ["-c" , "ping ${PING_HOST}"]
OR
CMD ["sh", "-c", "ping ${PING_HOST}"]
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is the error message that you are getting?
– Brian
2 days ago