CMD is used to provide default command to a running container.
CMD command can be written in below three forms.
a. CMD ["executable","param1","param2"] (exec form, this is the preferred form)
b. CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
c. CMD command param1 param2 (shell form)
Let’s try with an example.
Step 1: Create a directory ‘sleeper’.
Step 2: Navigate to the directory ‘sleeper’. Create Dockerfile with below content.
FROM ubuntu CMD sleep 5
Step 3: Go to the directory where Dockerfile is located, execute below command to generate the image.
docker build -t ubuntu_sleeper .
$docker build -t ubuntu_sleeper .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM ubuntu
---> 94e814e2efa8
Step 2/2 : CMD sleep 5
---> Running in 7f5bd2287317
Removing intermediate container 7f5bd2287317
---> 2678ecea665f
Successfully built 2678ecea665f
Successfully tagged ubuntu_sleeper:latest
Step 4: Run the container ubuntu_sleeper by executing below command.
docker run ubuntu_sleeper
You can observe that the container will sleep for 5 seconds and exit.
How to override the default command?
You can pass the command to run while running the container itself.
Syntax
docker run {image} {commandToExecute}
$docker run ubuntu_sleeper echo "I am overriding sleep command."
I am overriding sleep command.
No comments:
Post a Comment