Container Management Commands
Check Container List #
The docker ps command displays information about currently running containers.
$ docker ps
Check All Container Lists #
The docker ps command does not display stopped containers. Including the -a option displays information about all created containers.
$ docker ps -a
Restart and Connect to a Container #
You can start a previously stopped container using the docker start command. Afterwards, connect to that container using the docker attach command.
$ docker start my_ubuntu
Attach to the inside of a started container.
$ docker attach my_ubuntu
Stop a Container #
The docker stop command stops a currently running container.
docker stop m_ubuntu
Delete a Container #
The docker rm command deletes a stopped container from the container list. Containers that are only stopped but not deleted still occupy space somewhere in storage.
$ docker rm my_ubuntu
Stop and Delete at Once #
The docker rm -f option allows you to stop and delete a running container at once.
$ docker rm -f my_ubuntu
Delete All Containers in the Container List #
You can delete all containers in the container list at once. Always double-check and use caution when using commands that include terms like "all" or "force."
$ docker container prune
Automatic Deletion Option on Container Exit #
The docker run --rm option configures a container to be automatically deleted when it exits.
$ docker run -i -t --rm ubuntu:16.04
The command above runs an Ubuntu 16.04 container, and after completing tasks inside and exiting the container with the exit command, it automatically deletes that container.
Display Detailed Container Status Information #
The docker inspect command provides detailed information and status about a container. It is primarily used when you want to find the cause of an abnormal container termination or view detailed container information.
# docker inspect [container]
$ docker inspect my_ubuntu