Skip to main content

Docker Container Management

List all the Running Containers

docker ps

List all the Containers (irrespective of the state)

docker ps -a

Please note: -a is the short form for --all and they both can be used interchangeably.

docker ps -all

List all the Running Containers with the File Size

docker ps --size
docker container ls -s

List the IDs of the Running Containers

docker ps -q
docker container ls -q

List the IDs of all the Containers (irrespective of the state)

docker ps -aq
docker container ls -aq

Filter container list

List all the containers that are in the exited state.

docker ps --filter "status=exited"

List all the containers that are in the running state.

docker ps -a -f status=running

Creating a new Container using Docker Image

The docker create command is used to create a new container using a Docker image. It does not run the container but just adds a writable layer on top of the Docker image. We'll have to run the docker start command to run the created container.

As docker create command interacts with the containerobject, we can also use the below command:

docker container create <image>

Creating a new Container using Docker Image with some fixed name

docker create --name <container-name> <image>
docker container create --name <container_name> <image_name>

Start a Docker Container

docker start <container-id>
docker container start <container-id>

Stop a running Docker Container

docker stop <container-id>
docker container stop <container-id>

Restart a Docker container

docker restart <container_id or container_name>
docker container restart <container_id or container_name>

Pause a running Container

docker pause <container_id or container_name>
docker container pause <container_id or container_name>

Unpause a paused Container

To again run the paused container, we can use the below command:

docker unpause <container_id or container_name>
docker container unpause <container_id or container_name>

Docker Run command

The docker run command is used to create a new container from a Docker image. It is a combination of docker create and docker start commands.

docker run <image>
docker container run <image>

Docker Run command in Foreground and Detached Modes

docker run -it <image>
docker container run -it <image>

Delete the container on the exited state

docker rm <container-id>
docker container rm <container-id>

Run the container in daemon mode

docker run -d <image>
docker container run -d <image>

Run Docker Container with a name using the run command

docker run --name <container-name> <image>
docker container run --name <container-name> <image>

Listing Processes running in a Docker Container

docker top <container-id>
docker container top <container-id>

Map ports of a Docker Container

docker run -p <host-port>:<container-port> <image>
docker container run -p <host-port>:<container-port> <image>

Rename a Docker Container

Run the Docker Container in an Interactive Mode

Get Inside the Running Container (Literally!)

Start a Docker Container and keep it running

Copy a File from a Container to a Host

Copy a File from the Host to the Docker Container

Remove a specific Docker Container

Remove a Docker Container after it exits

Delete all the Stopped Containers

Delete all the Docker Containers

Create a Docker image from a Docker Container

Run command inside the Docker Container

Set Environment Variable in a Docker Container

Set Environment Variable in a Docker Container using a File