简体   繁体   中英

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.

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}"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM