Stopping and cleaning up

Stop, restart and remove containers and images.

A Docker practice simulator runs inside your browser tab. It behaves like the real commands for what the lessons teach, and it is labelled as a simulation.

Stopping and starting again

docker stop stops a running container. The container still exists, with its settings and files, and docker start runs it again.

Use docker ps -a to see stopped containers.

Example
docker run -d --name web nginx
docker stop web
docker ps -a

Removing a container

docker rm deletes a container. Docker refuses to remove one that is running: stop it first, or add -f to stop and remove it in one step.

Removing a container does not remove its image. You can start a new container from the image at any time.

Example
docker run -d --name web nginx
docker stop web
docker rm web
docker ps -a

Your turn: clear a container

A container named old is already running here. Run docker ps to see it.

Practice task

Stop the container named old and remove it. Leave the nginx image in place.

Your answer is run and checked in the lesson. Create a free account to try it.

Removing an image

docker rmi deletes an image from your computer and frees the disk space.

Docker refuses to remove an image while any container made from it still exists, running or stopped. Remove those containers first.

Example
docker pull busybox
docker rmi busybox
docker images

Your turn: container, then image

A container named cache is already running here, made from the redis image.

Practice task

Remove the container named cache, then remove the redis image.

Your answer is run and checked in the lesson. Create a free account to try it.