DEV Community

Cover image for Docker : From Zero to Hero πŸ›Έ ( part 2)
Prasenjeet Kumar
Prasenjeet Kumar

Posted on • Edited on

Docker : From Zero to Hero πŸ›Έ ( part 2)

Welcome to the second part of our Docker: From Zero to Hero series, where we will continue exploring essential Docker commands for container management. In the previous article, we covered Docker basics such as installation, images, and container creation. If you missed part one, you can find it here. In this article, we will focus on five critical Docker commands: docker ps, docker stop, docker rm, docker images, and docker rmi. With these commands, you will be able to manage running containers, stop or remove them, manage images, and clean up your Docker environment. Let's dive in and learn more about these essential commands.

docker ps

The docker ps command is used to list all running containers on the Docker host. The basic syntax of the docker psis as follows:

docker ps [OPTIONS]
Enter fullscreen mode Exit fullscreen mode
  • OPTIONS: Optional flags that can be used to modify the output of the command.

Some common options include:

  • a: Show all containers, including stopped ones.
  • q: Only show container IDs.
  • -format: Specify a format for the output.

Example usage:

docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
Enter fullscreen mode Exit fullscreen mode

This command will list all containers (including stopped ones), and format the output as a table with columns for the container ID, name, and status.

Here's an example output:

CONTAINER ID        NAMES               STATUS
a21a0498af56        nginx-container     Up 2 hours
be0fcf8f0c23        mysql-container     Up 5 days
e4389ac84062        redis-container     Exited (0) 3 weeks ago
Enter fullscreen mode Exit fullscreen mode

In this example output, we can see the container ID, name, and status of each container on the host. The nginx-containerand mysql-containerare currently running, while the redis-container
is stopped.

docker stop

The docker stop command is used to stop one or more running containers gracefully. It sends a SIGTERM signal to the container process, giving it a chance to shutdown gracefully, and then after a default timeout of 10 seconds, it sends a SIGKILL signal to forcefully terminate the container.

Here's the basic syntax of the docker stop command:

docker stop [OPTIONS] CONTAINER [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode

Here, OPTIONS are the various flags that you can pass to customize the behavior of the command, and CONTAINER is the ID or name of the container(s) you want to stop.

Here's an example of using the docker stop command:

docker stop my-container
Enter fullscreen mode Exit fullscreen mode

In this example, my-container is the name of the container that we want to stop.

You can also stop multiple containers at once by passing their names or IDs as space-separated arguments:

docker stop container1 container2 container3
Enter fullscreen mode Exit fullscreen mode

Some common options that can be used with docker stop are:

  • t, --time: Specify a custom timeout value (in seconds) for how long to wait before forcefully stopping the container with SIGKILL. By default, the timeout is set to 10 seconds.
  • -time=0: Stop the container immediately without waiting for the container process to shut down gracefully. This is equivalent to sending a SIGKILL signal directly to the container process.
  • f, --force: Stop the container forcefully by sending a SIGKILL signal directly to the container process, without waiting for the container process to shut down gracefully.

For example:

docker stop -t 30 my-container
Enter fullscreen mode Exit fullscreen mode

In this example, we're setting a custom timeout value of 30 seconds for stopping the my-container
container gracefully. If the container process does not shut down within 30 seconds, Docker will forcefully terminate it with a SIGKILL signal.

docker rm

The docker rm command is used to remove one or more stopped containers. It takes one or more container IDs or names as arguments and removes them. If a container is running, you need to stop it first using the docker stop command before removing it.

Here is the basic syntax:

docker rm [OPTIONS] CONTAINER [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode

Some of the commonly used options with the docker rm command are:

  • f, --force: This option forces the removal of a running container.
  • v, --volumes: This option removes the associated volumes as well.
  • l, --link: This option removes the specified link as well.

For example, to remove a container named my_container, you can use the following command:

docker rm my_container
Enter fullscreen mode Exit fullscreen mode

To remove multiple containers, you can specify their names or IDs as space-separated values:

docker rm container1 container2 container3
Enter fullscreen mode Exit fullscreen mode

To remove a running container, you can use the -foption:

docker rm -f my_container
Enter fullscreen mode Exit fullscreen mode

To remove the associated volumes as well, you can use the -v option:

docker rm -v my_container
Enter fullscreen mode Exit fullscreen mode

When a named volume is shared with multiple containers and one container is removed using the --volumesoption, the named volume will still exist and be accessible to other containers. However, if a container is using an anonymous volume and is removed with --volumes
, the anonymous volume will be removed along with the container and any data stored in the volume will be lost. To avoid accidental data loss, use the --volumesoption with caution.

docker images

The docker images command is used to list all the images that are currently stored on your system. This command shows the repository name, tag, image ID, creation date, and size of each image.

Here's an example:

$ docker images

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ubuntu                latest              7e0aa2d69a15        3 weeks ago         72.8MB
nginx                 latest              e37c78c07e2f        3 weeks ago         133MB
mysql                 latest              8a0d9ac31343        3 weeks ago         551MB
Enter fullscreen mode Exit fullscreen mode

In the output, you can see that there are three images available: Ubuntu, Nginx, and MySQL. The columns show the repository name, tag, image ID, creation date, and size of each image.

You can use this command to check which images are available on your system and to find the image ID that you need to use when running a container.

docker rmi

The docker rmi command is used to remove one or more Docker images from the host. The syntax of the command is as follows:

docker rmi [OPTIONS] IMAGE [IMAGE...]
Enter fullscreen mode Exit fullscreen mode

Here, OPTIONSare additional options that can be used with the command, and IMAGE is the name or ID of the image(s) to remove.

For example, to remove an image with the name my-image, you can use the following command:

docker rmi my-image
Enter fullscreen mode Exit fullscreen mode

If you want to remove multiple images, you can specify their names or IDs separated by a space, like this:

docker rmi my-image1 my-image2 my-image3
Enter fullscreen mode Exit fullscreen mode

Some common options used with the docker rmi command include:

  • f or -force: This option forces the removal of the image(s) even if there are running containers using them or if they have dependent child images.
  • -no-prune: This option prevents Docker from removing the parent images of the specified image(s) if they are not being used by any other images or containers.

Note that when you remove an image, any containers that were created from it will still exist on the host, but they will be unusable. To remove these containers, you can use the docker rm
command with the --forceoption.

In Docker, a parent image is the image that another image is built upon using a Dockerfile. Each image can have one parent image, with the exception of the base image (e.g. Ubuntu, Alpine, etc.) which has no parent.

To find the parent image(s) of a specified image, you can use the docker history command followed by the image name or ID. The output of the command will show the image's layers, with the parent image of each layer listed in the "IMAGE" column.

Top comments (0)