Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Uncategorized

How to List, Start, and, Stop Docker Containers?

Introduction

Docker containers have evolved the manner applications are deployed as well as handled. They offer a lightweight and portable solution to package, transmit, and operate applications in any environment. In this blog, we will explore the concepts of Docker containers and guide you through the process of how to list, start, and stop Docker containers. Whether you are a system administrator, developer, or someone curious about containerization technology, this guide will help you employ the power of Docker containers efficiently.

What is a Docker Container?

Before diving into the specifics of listing, starting, and stopping Docker containers, let’s understand what Docker containers are and how they work. Docker containers are isolated environments that package an application and its dependencies with each other. They utilize the host operating system’s kernel, making them more lightweight and faster than traditional virtual machines.

To create a Docker container, you need a Docker image—a read-only template with all the necessary instructions for setting up the application environment. Docker images are built using Dockerfiles, which contain a series of commands to install software, copy files, and configure the container. As soon as the image is prepared, you can operate it as a Docker container.

Also Read: How to Install Docker on CentOS 7?

List Docker Containers

To list docker containers is a fundamental operation in managing your containerized applications. To view the existing containers on your system, open a terminal or command prompt and use the following command:

docker ps

This command will display a list of running containers along with essential information like their container IDs, names, status, and ports. However, if you want to see all containers, including the ones that are not currently running, use the following command:

docker ps -a

Additional Docker ps command options:

-aq flag: If you want to see only the container IDs, you can use the -q or –quiet flag along with the docker ps command. This option displays only the numeric IDs of the containers, making it useful for scripting or when you need to extract specific container information quickly.

docker ps -aq

-s flag: The -s or –size flag can be used to display the total file sizes of the containers. It shows the amount of data that each container is using on the disk. This can be helpful when you want to recognize which containers are using up the maximum space of the disk.

docker ps -as

-l flag: The -l or –latest flag displays the last created container, regardless of its current state (running or stopped). This choice is specifically helpful in case you wish to quickly access the details of the most recently created container.

docker ps -l

The docker ps command provides several columns of information about the running containers. 

The default columns include:

  • Container ID: The unique alphanumeric identifier set to the purpose of the container.
  • Image: The Docker image used to create the container.
  • Command: The command that was implemented when beginning the container.
  • Created: The date and time when the container was constructed.
  • Status: The current state of the container (e.g., “Up,” “Exited,” “Paused,” etc.).
  • Ports: The port mappings between the container & host.
  • Names: The name(s) assigned to the container.

By default, docker ps displays these columns. However, you can use the –format flag to customize the output and display specific columns of information as per your requirements.

For example, to show only the container IDs and their corresponding names, you can use the following command:

docker ps --format "ID: {{.ID}} Name: {{.Names}}"

You can modify the format string to display the information you need, and you can find more details about formatting options in Docker’s official documentation.

Start Docker Container

Now that you know how to list docker containers let’s move on to how to start docker containers. To start a Docker container, you can utilize the docker run command, which allows you to execute an instance of a Docker image. The command has various options that provide flexibility and control over container configurations. Let’s explore some of the key options and additional functionalities of the docker run command:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
  • OPTIONS: Specify additional options, such as port bindings, environment variables, or resource constraints. Some commonly used options include:
    • -d, –detach: Run the container in detached mode, which implies it will operate in the background.
    • -p, –publish: Map ports between the container & host. The format is hostPort:containerPort.
    • –name: Assign a name to the container for easier identification.
    • –env, -e: Make environment variables inside the container.
    • –volume, -v: Mount host directories or volumes to the container.
    • –restart: Mention the policy of restarting the container after it exits.
  • IMAGE: The Docker image name you wish to operate. You can find images on Docker Hub or in your local Docker image repository.
  • TAG: (Optional) The specific tag of the image. If not provided, Docker uses the “latest” tag by default. Tags represent different versions or configurations of the same image.
  • COMMAND: (Optional) The command you want to run inside the container, overrides the default command specified in the image. For instance, you can run a particular shell or script.
  • ARG: (Optional) Additional arguments to pass to the command. These arguments can be employed as variables inside the Dockerfile during image building.

For example, to start an instance of the “nginx” web server, expose it on port 80 of the host, and name the container “web_server”, you can use the following command:

docker run -d -p 80:80 --name web_server nginx

The above command runs the “nginx” container in detached mode (-d), maps port 80 of the host to port 80 of the container (-p 80:80), and assigns the name “web_server” to the container (–name web_server).

You can also link to a running container to interact with its console. To do this, you can utilize the docker attach command:

docker attach [CONTAINER]
  • CONTAINER: The container ID or name of the container you want to attach to.

For example, to attach to the “web_server” container we created earlier, you can use:

docker attach web_server

Using the docker attach command will allow you to interact with the container’s console. To unlink from the container without restructuring it, press Ctrl + P, Ctrl + Q.

Stop Docker Container

To stop Docker containers, you can utilize the docker stop command. This command allows you to gracefully stop multiple operating containers. Here’s how you can do it:

docker stop [OPTIONS] CONTAINER [CONTAINER...]
  • OPTIONS: You can specify additional options to control the stop process.
    • –time, -t: Set a timeout value before forcefully stopping the container. If the container does not stop gracefully within the mentioned time, Docker will forcefully terminate it. The time can be specified in seconds (s) or as a duration (e.g., 10s, 2m).
    • –time=10 will attempt to stop the container gracefully for 10 seconds before forcefully stopping it.
    • –time=120s will attempt to stop the container gracefully for 2 minutes before forcefully stopping it.
  • CONTAINER: The container ID or name of the container you want to stop. You can find the container ID or name by employing the docker ps command.

For example, to stop a container with the ID abc123, you can execute the following command:

docker stop abc123

If you have multiple containers to stop, you can pass multiple container IDs or names to the docker stop command. Docker will attempt to stop each container one by one.

docker stop container1 container2 container3

If you encounter issues with the containers not stopping gracefully, you can use the –time option along with the docker stop command to set a timeout. If the container is not able to stop in the mentioned time, Docker will forcefully terminate it.

docker stop --time=30 container1

Additionally, if a container is unresponsive to the docker stop command and you need to force it to stop immediately, you can use the docker kill command.

docker kill container1

It’s important to note that forcefully stopping a container using docker kill may result in data loss or potential issues, so it is recommended to first try gracefully stopping the container using docker stop. Use docker kill as a last resort when the container is unresponsive or not stopping as expected.

By utilizing the various options provided by the docker stop and docker kill commands, you can effectively manage the lifecycle of your Docker containers, ensuring smooth application deployment and maintenance.

Conclusion

Docker containers provide an efficient and reliable way to manage applications, making the deployment process smoother and more consistent across different environments. With this guide, we have learned the concept of Docker containers and how they function. We also learned how to list the existing containers on our system and how to start and stop docker containers using simple commands.

Docker’s versatility and ease of use have made it a game-changer in modern software development and system administration. By following the steps outlined in this guide, you can effectively utilize Docker containers to streamline your application management and deployment processes.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.