Docker Container Management (with examples)

Docker Container Management (with examples)

Docker is a widely popular and versatile platform used to build, package, and distribute applications as lightweight containers. One of its key commands is docker container, which allows users to manage Docker containers efficiently. In this article, we will explore eight different use cases of the docker container command, providing code examples, motivations, explanations for each argument, and example outputs.

List currently running Docker containers

To list all the currently running Docker containers, we can use the following command:

docker container ls

Motivation: This command is useful when you want to quickly check the status of running containers on your system.

Explanation: The ls option lists all the running containers. Alternatively, we can use docker ps as a shorthand for the same command.

Example output:

CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS        PORTS     NAMES
f8a0a8936b67   nginx:latest          "/docker-entrypoint.…"   2 hours ago    Up 2 hours    80/tcp    nginx-container
3c0916d52059   postgres:12.3         "docker-entrypoint.s…"   5 days ago     Up 5 days     5432/tcp  postgres-container

Start one or more stopped containers

To start one or more stopped containers, we can use the following command:

docker container start container1_name container2_name

Motivation: This command is handy to restart containers that have been previously stopped.

Explanation: Replace container1_name and container2_name with the names or container IDs of the containers you want to start. Multiple container names can be specified as space-separated values.

Example usage:

docker container start nginx-container

Example output:

nginx-container

Kill one or more running containers

To forcefully terminate one or more running containers, we can use the following command:

docker container kill container_name

Motivation: This command is useful when you need to stop a container immediately, bypassing the regular shutdown process.

Explanation: Replace container_name with the name or container ID of the container you want to kill. Multiple containers can be specified as space-separated values.

Example usage:

docker container kill postgres-container

Example output:

postgres-container

Stop one or more running containers

To stop one or more running containers gracefully, we can use the following command:

docker container stop container_name

Motivation: This command is used when you want to stop a running container and give it the opportunity to clean up before exiting.

Explanation: Replace container_name with the name or container ID of the container you want to stop. Multiple containers can be specified as space-separated values.

Example usage:

docker container stop nginx-container

Example output:

nginx-container

Pause all processes within one or more containers

To pause all processes within one or more running containers, we can use the following command:

docker container pause container_name

Motivation: Pausing containers can be useful when you need to diagnose issues within a running container without stopping its processes.

Explanation: Replace container_name with the name or container ID of the container you want to pause. Multiple containers can be specified as space-separated values.

Example usage:

docker container pause nginx-container

Example output: (No output displayed. The container’s processes are paused.)

Display detailed information on one or more containers

To view detailed information about one or more containers, we can use the following command:

docker container inspect container_name

Motivation: This command is handy for obtaining comprehensive details about a specific container, including its networking configuration, environment variables, volumes, and more.

Explanation: Replace container_name with the name or container ID of the container you want to inspect. Multiple containers can be specified as space-separated values.

Example usage:

docker container inspect nginx-container

Example output: (A JSON document containing detailed information about the container)

{
    "Id": "f8a0a8936b67147...",
    "Created": "2022-01-01T00:00:00Z",
    "Path": "/docker-entrypoint.sh",
    "Args": [
        "nginx",
        "-g",
        "daemon off;"
    ],
    "State": {
        ...
    },
    "Config": {
        ...
    },
    ...
}

Export a container’s filesystem as a tar archive

To export a container’s filesystem as a tar archive, we can use the following command:

docker container export container_name

Motivation: Exporting a container’s filesystem allows for offline analysis, backup, or sharing of a container’s contents with others.

Explanation: Replace container_name with the name or container ID of the container you want to export.

Example usage:

docker container export nginx-container > nginx.tar

Example output: (No output displayed. A tar archive named “nginx.tar” is created in the current directory.)

Create a new image from a container’s changes

To create a new image from a container’s changes, we can use the following command:

docker container commit container_name

Motivation: This command is useful when you want to capture the current state of a container and convert it into a new image for future use.

Explanation: Replace container_name with the name or container ID of the container you want to convert into an image.

Example usage:

docker container commit nginx-container my-custom-nginx-image

Example output:

sha256:2aeb0327b631159...

By understanding and utilizing the various functionalities provided by the docker container command, you can effectively manage and manipulate Docker containers based on your specific requirements.

Related Posts

How to use the command 'tox' (with examples)

How to use the command 'tox' (with examples)

Tox is a command-line tool that allows you to automate Python testing across multiple Python versions.

Read More
How to use the command guake (with examples)

How to use the command guake (with examples)

Guake is a drop-down terminal for GNOME. It provides a quick and easy way to access the terminal without opening a separate window.

Read More
Using the fkill Command (with examples)

Using the fkill Command (with examples)

The fkill command is a powerful cross-platform tool that allows users to easily kill processes by their PID, name, or port.

Read More