Docker has revolutionized the way developers deploy applications by packaging applications into consistent, portable, and isolated environments called containers. Knowing how to manage these containers is a fundamental application deployment skill that every developer and system administrator should have in their skillset.
This comprehensive tutorial will go through how to list, start, and stop Docker containers. Once you finish this tutorial, you will be an expert in Docker container management.
Table Of Contents
Let’s start with the prerequisites for the process.
Before going through the commands discussed below, ensure you have the following:
- Docker installed on your machine.
- Basic familiarity with Linux command-line interface (CLI) operations.
- Sufficient permissions to execute Docker commands on your system.
List Docker Containers
A Docker container is a lightweight, stand-alone package that contains everything (the code, a runtime, libraries, environment variables, and config files) needed to run software in the target environment.
We assume that you already have an existing container. If not, you can create a container from a Docker image and proceed with this tutorial.
Let’s start with viewing all active Docker containers. For this, open your terminal and type:
$ docker ps
Next, if you want to see all containers (active and inactive), use the -a option:
$ docker ps -a
This output provides significant information such as:
- CONTAINER ID: A unique identifier allocated to each container.
- IMAGE: The base image used to create the container.
- COMMAND: The command that was used to start the container.
- CREATED: The timestamp when the container was created.
- STATUS: The current status of the container (Up or Exited).
- PORTS: The exposed and bound ports of the container.
- NAMES: The name assigned to the container.
Since container ID is unique to each container, you can list the containers with their container IDs using the -aq option:
$ docker ps –aq
Listing the last container created on the system is often a common query. For this, use the -l flag:
$ docker ps –l
Start Docker Container
A Docker container shuts down after its process is completed. System admins can manually halt process execution and move the container to a stop state.
You can easily start a container to bring it to an active state where it can start running its application.
To start a specific container, you need its CONTAINER ID or NAME. Then, run the following command:
$ docker start [options] [CONTAINER ID or NAME]
The simplest command to start a container is:
# docker start <containerID>
In most cases, you’ll only see the container ID as the command’s output. As such, you need to verify that your container has started successfully by running this command:
$ docker ps
Look for the container’s STATUS column in the output. It should show “Up” followed by the uptime duration.
If you’re creating a container from an image, spawn it in the running state with the following commands:
$ docker pull image_name
$ docker run [options] image_name
By default, the container is assigned a random string as the container’s name. However, it is a good idea to assign a name that you can use to recognize the container’s purpose.
For this, use the following command:
$ docker run --name=MyContainerName image_name
For instance, this command creates a container named production1 using the image centos and starts it up.
# docker run --name=production1 centos
Sometimes, a container might be running, but you can’t talk to it or control it. If you want to access the container, start the container in an interactive mode by using the following command:
$ docker run -it --name=MyContainerName image_name
Here’s a command that creates a container named prod2 based on a CentOS image. The -it flags indicate that the container is in interactive mode, and you can run commands directly inside it in real-time.
# docker run -it --name=prod2 centos
If you don’t want to use the -i or -t flags, there’s another way to connect to a running container with the attach command. The syntax of this command is:
$ docker attach container_id
This lets you connect and run commands in an already up-and-running container.
Stop Docker Container
You may wish to stop a container to conserve system resources, perform maintenance, or simply because the application it runs is no longer needed.
Stopping a container is as straightforward as starting one. Again, you will need the CONTAINER ID or NAME to use with the docker stop command:
$ docker stop [option] [CONTAINER ID or NAME]
For instance, use the following command to stop the prod2 container we started earlier:
# docker stop prod2
Again, you’ll only see the container name as the output of the command. So, it is a good practice to check the container’s status after running the stop command.
For this, use the docker ps command to ensure the container is no longer active. For a more comprehensive check, add the -a flag to list all containers.
For instance, the following command will get the status of all containers and then use the grep utility to extract the information about the target container.
# docker ps -a | grep prod2
By default, when you tell a Docker container to stop, it waits for 10 seconds before actually stopping the services inside the container. This is like giving the container a 10-second warning to finish what it’s doing. If you want to give it more or less time (in seconds), add the –time flag to the docker stop command.
For instance, the following command syntax will give 20 seconds to stop a container.
$ docker stop --time=20 container_id
If you’re in a hurry and want to force the container to stop immediately without waiting for the 10-second grace period, use the docker kill command like this:
$ docker kill container_id
In extreme cases, you might want to stop all active containers with the following command that gives containers 10 seconds to wind up their processes:
$ docker stop $(docker ps -a -q)
On the other hand, if you want to forcefully kill all running containers without giving them any time to finish, use the following command where docker stop is replaced with docker kill:
$ docker kill $(docker ps -a -q)
This way, you can control how containers are stopped or killed based on your needs.
Docker’s container management commands are integral to the orchestration of containerized environments. Mastering the list, start, and stop commands is essential for efficient Docker container management.
When your projects grow in complexity, consider a powerful hosting solution like RedSwitches that offers the performance and reliability you need to support high-demand Docker applications.
We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q. Can I start multiple Docker containers at once?
Yes, you can start multiple containers by passing the container IDs or names to the docker start command, separated by spaces.
Q. What does the docker ps -a command do?
The docker ps -a command lists all containers in Docker, including those that are currently running and those that have exited. The -a flag stands for “all”, showing the complete list of containers.
Q. What is the docker rm command used for?
The docker rm command is used to remove one or more Docker containers. You must stop a container before you can remove it unless you use the -f (force) option.
Q. What are Docker images?
Docker images are portable, executable packages that contain application code, libraries, and dependencies for running containers.
Q. Where can I find Docker images for various applications?
Docker Hub is a popular online repository for discovering and sharing Docker images.
Q. What is Docker Compose used for?
Docker Compose is a tool for defining and managing multi-container applications using a single configuration file.
Q. How can I view the output of a Docker container?
You can use the docker logs command to access the output and logs generated by a container.
Q. What does the docker exec command do?
docker exec allows you to run commands inside a running Docker container, facilitating interaction with its environment.
Q. What is a Docker host?
The Docker host is the system (physical or virtual) where Docker is installed and containers run.