Enhancing Skills

Docker commands: I use the most

Docker is a powerful containerization platform that provides a wide range of commands for managing containers, images, and other data. The commands available in Docker are designed to be easy to use and allow developers to perform a variety of tasks, such as building, running, and managing containers, as well as managing images and other data.

Some of the most commonly used Docker commands include docker build, which is used to create an image from a Dockerfile, and docker run, which is used to start a container based on an image. Other useful Docker commands include docker ps, which is used to list all containers running on the system, and docker stop, which is used to stop a running container. Please reference Docker commands a simple guide and Docker commands: I use as BASH with Variables

Docker also provides a number of advanced features, such as support for multiple operating systems and architectures, as well as the ability to run containers in swarm mode. These advanced features allow developers to create highly scalable and resilient containerized applications, making it easier than ever to deploy and manage complex software environments.

Overall, Docker provides a wide range of commands that make it easy for developers to build, deploy, and manage containerized applications. Whether you’re just starting out with Docker or looking to take your skills to the next level, there’s plenty of information available to help you get started today.

Before doing any damage, lets see some commands

Info

The docker info command is used to display information about the Docker system. It displays a variety of information about the system, including its version, storage driver, and other details.

Here are some of the columns that are displayed by default:

  • DOCKER VERSION: The version of Docker running on the system.
  • STORAGE DRIVER: The storage driver used to manage images and containers.
  • RUNNING CONTAINERS: The number of containers currently running on the system.
  • IMAGES: The total number of images available on the system.

You can also use options like -q to display only a few columns, or --no-trunc to display the full ID of each image.

Here’s an example of how you might use docker info to display information about the Docker system:

$ docker info
Containers: 2
  Running: 1
  Paused: 0
  Stopped: 1
Images: 7
Server Version: 19.03.12
Storage Driver: overlay2
  Backend 1: overlay
...

This command shows a summary of the Docker system, including the version of Docker running on the system, the storage driver used, and other details. You can also use options like -q to display only a few columns, or --no-trunc to display the full ID of each image

Container

The docker container ls command is used to list all containers on a Docker system. It displays a variety of information about each container, including its ID, name, status, and other details.

Here are some of the columns that are displayed by default:

  • ID: A unique identifier for the container. This can be used to refer to the container in future commands.
  • Image: The name of the Docker image that was used to create the container.
  • Command: The command that was used to start the container.
  • Created: The date and time when the container was created.
  • Status: The current status of the container, such as “running” or “exited”.
  • Ports: Any ports that are exposed by the container.
  • Names: The names assigned to the container.

You can also use options like -a to display all containers, including those that are not running, and -q to display only the container IDs.

Here’s an example of how you might use docker container ls to list all containers on a system:

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
26907381c64e        ubuntu              "bash"              2 days ago          Exited (0) 2 days ago                           my_container

This command shows a list of all containers on the system, including the ID, image name, command used to start it, creation date and time, current status, exposed ports, and names assigned to each container.

Image

The docker image ls command is used to list all images on a Docker system. It displays a variety of information about each image, including its ID, name, tag, and other details.

Here are some of the columns that are displayed by default:

  • REPOSITORY: The repository from which the image was pulled.
  • TAG: The tag used to pull the image.
  • IMAGE ID: A unique identifier for the image. This can be used to refer to the image in future commands.
  • CREATED: The date and time when the image was created.
  • SIZE: The size of the image in bytes.

You can also use options like -q to display only the image IDs, or --digests to display the digest for each image.

Here’s an example of how you might use docker image ls to list all images on a system:

$ docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu               latest              26907381c64e        2 days ago         119MB

This command shows a list of all images on the system, including the repository, tag, image ID, creation date and time, and size. You can also use options like -q to display only the image IDs, or --digests to display the digest for each image

Network

The docker network ls command is used to list all networks on a Docker system. It displays a variety of information about each network, including its name, ID, driver, and other details.

Here are some of the columns that are displayed by default:

  • NAME: The name assigned to the network.
  • ID: A unique identifier for the network. This can be used to refer to the network in future commands.
  • DRIVER: The driver used to create the network.
  • SCOPE: The scope of the network, which determines how it is attached to containers and other networks.

You can also use options like -q to display only the network names, or --no-trunc to display the full ID of each network.

Here’s an example of how you might use docker network ls to list all networks on a system:

$ docker network ls -a
NAME                  ID                DRIVER              SCOPE
my_network            65c2947803d1       bridge              local
other_network         6f0af1f847e7       bridge              global

This command shows a list of all networks on the system, including their names, IDs, drivers, and scopes. You can also use options like -q to display only the network names, or --no-trunc to display the full ID of each network

Volume

The docker volume ls command is used to list all volumes on a Docker system. It displays a variety of information about each volume, including its name, driver, and other details.

Here are some of the columns that are displayed by default:

  • NAME: The name assigned to the volume.
  • DRIVER: The driver used to create the volume.
  • STATUS: The status of the volume, which can be “created” or “in use”.
  • SIZE: The size of the volume in bytes.

You can also use options like -q to display only the volume names, or --no-trunc to display the full ID of each volume.

Here’s an example of how you might use docker volume ls to list all volumes on a system:

$ docker volume ls -a
NAME                  DRIVER              STATUS            SIZE
my_volume             local                created           10GB
other_volume          local                in use            5GB

This command shows a list of all volumes on the system, including their names, drivers, statuses, and sizes. You can also use options like -q to display only the volume names, or --no-trunc to display the full ID of each volume

Docker system prune

The docker system prune command is used to remove unused data from a Docker system. It can be used to free up storage space on the host machine by removing any unused images, containers, volumes, and networks.

The -a option tells docker system prune to remove all unused objects, including images, containers, volumes, and networks. The --force option allows you to override any prompts or warnings that might be displayed during the removal process. The --volume option tells docker system prune to only remove unused volumes.

Here’s an example of how you might use docker system prune to remove all unused objects on a system:

$ docker system prune -a --force --volume

This command will prompt you for confirmation before removing any objects, and it will only remove unused volumes. If you want to skip the prompt and force the removal of all unused objects, you can add the --force option again:

$ docker system prune -a --force --volume --force

Builder Prune

The docker builder prune command is used to remove temporary build files and other data that are created during a Docker build process. These files can take up a significant amount of disk space and can be safely removed once the build is complete.

When you run the docker builder prune command, Docker will search for and delete any temporary build files and other data that have not been used in the last 14 days by default. You can adjust this time period using the --keep-for flag, which takes a duration string as its argument (e.g., --keep-for 30d).

This command is typically run after a build process has completed, but it can also be run at any time to remove any unnecessary data that may have accumulated over time. By doing so, you can help free up disk space on your system and ensure that your Docker builds are running as efficiently as possible.

docker builder prune

Other arguments

In addition to --all, --force, and --volume, there are several other flags that can be used with the docker system prune command. Here are some of them:

  • --filter: This flag allows you to specify a filter expression that will be applied to the data that is being pruned. For example, you could use the --filter=until=14d flag to only remove data that has not been used in the last 14 days.
  • --label: This flag allows you to specify a label expression that will be applied to the data that is being pruned. For example, you could use the --label=env=production flag to only remove data that has the “env=production” label set.
  • --until-used: This flag tells Docker to remove any unused data that has not been used since a specified point in time. For example, you could use the --until-used 24h flag to remove any data that has not been used in the last 24 hours.
  • --volumes-filter: This flag allows you to specify a filter expression for volumes. For example, you could use the --volumes-filter=^/data flag to only remove volumes whose names start with “/data”.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.