Cloud and Container Basics
FROM alpine
ENTRYPOINT ["ping", "-c", "3"]
CMD ["localhost"]An image named pinger, built from the Dockerfile above, is started with docker run pinger example.com. Which of the following is the command executed in the container?
- Aping -c 3 localhost
- Bping -c 3 example.com
- Cexample.com (an attempt to run it as a command fails with an error)
Show answer and explanation
Answer: B. ping -c 3 example.com
An ENTRYPOINT written in exec form (a JSON array) is the command the container always runs, and CMD supplies its default arguments. Arguments written after the image name in docker run replace CMD and are appended after the ENTRYPOINT, so ping -c 3 example.com is executed. Started without arguments as docker run pinger, the localhost in CMD would be used, giving ping -c 3 localhost. To change the ENTRYPOINT itself, use the --entrypoint option of docker run.
- A. This is what happens when the container is started without arguments. The arguments of docker run replace CMD.
- B. Correct. The argument example.com replaces localhost in CMD and is appended after the ENTRYPOINT.
- C. This would happen with an image that has only CMD and no ENTRYPOINT, because the argument would replace the whole command. Here there is an ENTRYPOINT, so it is passed as an argument.