Jan 18, 2017 4) Removing containers. Once docker containers are stopped you may need to delete docker images. One thing most system administrators may find very annoying is all unused containers and images taking up precious space. It would have been easier if there was a docker. In this guide, I will show you simple tips to stop and remove containers, docker images, and values. List all Docker Images Removing Docker Image or images Removing All Docker Image Remove a container(s) Remove a container and its volume List all containers (only IDs) Stop all running containers Remove all containers Remove all exited.
What is a Docker Container
A running instance of an image is called a container. Docker launches them using the Docker images as read-only templates. If you start an image, you have a running container of this image. Naturally, you can have many running containers of the same image. We use the command “docker run” to run a container.
Listing Docker Containers
To list the containers, run the following command:
To list both running and stopped containers, use the -a option as follows:
here,
CONTAINER ID – Unique ID given to all the containers.
IMAGE – Base image from which the container has been started.
COMMAND – Command which was used when the container was started (default is /bin/bash, if you do not specify any command with “docker run”).
CREATED – Time at which the container was created.
STATUS – The current status of the container (Up or Exited).
PORTS – Port numbers if any, forwarded to the docker host for communicating with the external world.
NAMES – Dockers daemon names the cluster in some funny way. You can also specify your own name while spawning a container though.
To list only the container ID, use the -aq option.
To list the last container created (running or stopped) :
Starting a Docker Container
Use the below command to start a Docker container:
For example :
here,
-i : Starts the container in interactive mode.
-t : allocates a pseudo-tty and attaches it to the standard input
–name : User friendly name for the container. If the name is not specified, random string will be assigned as the container name.
When you run a docker command with -t, you would get attached to the container immediately and would see the command prompt changed. You can use the command “exit” to exit out from the container.
In all above examples, when you start the container, you are automatically logged into it. And when you exit the container, the container is stopped. You can also keep the container running even when you logout by using the -d option. For example :
Here,
-d – Runs container in background and print container ID.
Stopping a Docker Container
You can stop one or more (all) containers at once. The syntax of the command to stop a docker container is :
Here,
–time/-t is grace period to wait before stopping the container.
For Example, first check which container is running.
Now to stop the above container use the below command.
To stop all the containers, run the below command:
Deleting a Docker container
To delete docker container use the syntax below:
For Example:
To delete a container we need to first stop it. For example, if you try deleting a running container you would get an error as:
So to delete the container, first stop it and then delete it.
To forcefully delete a container without stopping it, use the -f option.
To delete all the containers at once, first stop them all and then delete them.
How to install docker on CentOS / RHEL / Fedora
Description
Remove one or more containers
Usage
Options
Name, shorthand | Default | Description |
--force , -f | Force the removal of a running container (uses SIGKILL) | |
--link , -l | Remove the specified link | |
--volumes , -v | Remove the volumes associated with the container |
Parent command
Command | Description |
---|---|
docker | The base command for the Docker CLI. |
Examples
Remove a container
This will remove the container referenced under the link/redis
.
Remove a link specified with --link
on the default bridge network
This will remove the underlying link between /webapp
and the /redis
containers on the default bridge network, removing all network communicationbetween the two containers. This does not apply when --link
is used withuser-specified networks.
Force-remove a running container
This command will force-remove a running container.
The main process inside the container referenced under the link redis
will receiveSIGKILL
, then the container will be removed.
Remove all stopped containers
This command will delete all stopped containers. The commanddocker ps -a -q
will return all existing container IDs and pass them tothe rm
command which will delete them. Any running containers will not bedeleted.
Remove a container and its volumes
This command will remove the container and any volumes associated with it.Note that if a volume was specified with a name, it will not be removed.
Remove a container and selectively remove volumes
In this example, the volume for /foo
will remain intact, but the volume for/bar
will be removed. The same behavior holds for volumes inherited with--volumes-from
.