Saturday 9 May 2020

Docker: Execute the command while running a container

You can pass the command to be executed while running a container in the command line.

For example, the below statement prints "Hello World" message to console and exit the container.
docker run ubuntu echo "Hello World"

Below statement executes the sleep command (sleep for 5 seconds) and exit the container.
docker run ubuntu sleep 5

Container will exist until the underlying task is running, once the underlying task is finished or aborted, then the container destroyed automatically.
$docker run ubuntu echo "Hello World"
Hello World
$
$docker run ubuntu sleep 5
$
$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
e73d81bec53a        ubuntu              "sleep 5"                15 seconds ago      Exited (0) 9 seconds ago                        nervous_bhaskara
12bf7981e2c5        ubuntu              "echo 'Hello World'"     31 seconds ago      Exited (0) 29 seconds ago                       competent_pascal



Previous                                                    Next                                                    Home

No comments:

Post a Comment