Commands
- Application commands in a pod definition file.
Dockercommands, for example runningdocker run ubuntuwill run an Ubuntu image and then exit immediately.docker ps -awill show all containers including those which have stopped.- Containers are meant to run a specific tasks or process.
- When the task is complete, the container exits. Similarly if the container is stopped.
- Within a container image, if you see the word
CMD, that is the program that will start within the container.- For example
CMD ["bash"] - If it cannot find a terminal, it exits.
- Therefore the container itself exits.
- For example
- To change this, you can append a command to the Docker override command.
- An example is the
docker run ubuntu sleep 5(sleep 5being the added option).- When the container starts, it runs the
sleepcommand for 5 seconds and then exits.- To make this permanent, edit the
dockerimage and addCMD sleep 5
- To make this permanent, edit the
- When the container starts, it runs the
- An example is the
- The
CMDcan be placed in ashellform or ajsonarray form as well.- For JSON, it should look like
CMD ["sleep","5"]
- For JSON, it should look like
- The image looks like this:
FROM Ubuntu CMD sleep 5 - Then we build the image with
docker build -t <image-name> .and run it withdocker run <image-name> - You can also change the image, by appending the new command to it, in this case
docker run <image-name> sleep 10- Ideally we want to pass in the seconds only and invoke the
sleepcommand automatically like so:docker run <image-name> 10
- Ideally we want to pass in the seconds only and invoke the
- Therefore to do the above, you need the
ENTRYPOINTinstruction, that beingENTRYPOINT ["sleep"]. The image would then look like:FROM Ubuntu ENTRYPOINT ["sleep"] - How do you configure a value automatically if one is not added in the command by the user. This can be done using:
FROM Ubuntu ENTRYPOINT ["sleep"] CMD ["5"] - Therefore, the above command will be
sleep 5, if a operand is not set by the user. - You can also override the command using the
--entrypointoption indockerlike so:docker run --entrypoint sleep2.0 <image-name> <value>