31+ Best Docker Commands You Should Learn in 2024

Best Docker Commands

In the dynamic world of software development in 2024, Docker stands as a pivotal tool. Docker, a platform that simplifies the process of building, running, and managing applications in isolated environments known as containers, has revolutionised the way developers work. Its ability to ensure consistency across various environments has made it indispensable.

Understanding Docker Commands and Their Use Cases

Docker commands are the backbone of interacting with Docker. They allow you to manage the lifecycle of Docker containers and images. Understanding these commands and their specific use cases is crucial for efficient Docker management.

Detailed Exploration of Top Docker Commands

1. Docker Version

Use this command to check the currently installed Docker version. It’s essential for ensuring compatibility and leveraging the latest features. Here is an example – 

Execution:

Output:

  • Docker version Indicates that the output is the Docker version information.
  • 20.10.7 Represents the version number of Docker installed on your system.
  • build f0df350 Specifies the unique identifier (hash) for the Docker build.

    Search for Docker images in the Docker Hub. Useful for finding the right base images for your projects. Here is an example – 

    Execution:

      Output:

      • Replace IMAGE_NAME with the actual name or keyword you want to search for.
      • The search was performed for the keyword “nginx.” The output shows details about the official Nginx image available on the Docker Hub.

      3. Docker Pull

      Downloads an image from Docker Hub to your local machine. It’s the starting point for running containers. Here is an example – 

      Execution:

      Output:

      • Replace IMAGE_NAME with the name of the Docker image and [:TAG] with an optional tag. If you don’t specify a tag, the latest version of the image is pulled by default.
      • This command pulls the official Nginx image from Docker Hub. If you don’t specify a tag, it will pull the latest version.

      4. Docker Run

      This command creates and starts a container from an image. It’s used to get your applications up and running. Here is an example – 

      Execution:

      Output:

      • Replace OPTIONS with any configuration options you need, and replace IMAGE_NAME[:TAG] with the name of the Docker image you want to use.
      • This command creates and runs a detached (-d) Docker container named mynginx, mapping port 8080 on the host to port 80 on the container, based on the official Nginx image.

        5. Docker Ps

        Lists all running containers. Essential for monitoring and managing active containers. Here is an example – 

        Execution:

        Output:

        • This command displays a list of currently running containers along with their details.

        6. Docker Stop

        Stops a running container. Use this to safely shut down containers without losing data. Here is an example – 

        Execution:

        Output:

        • Replace CONTAINER_ID or CONTAINER_NAME with the ID or name of the container you want to stop.
        • This command stops the Docker container with the name mynginx.

              7. Docker Restart

              Restarts a container. Ideal for applying updates or configuration changes. Here is an example – 

              Execution:

              Output:

              • Replace CONTAINER_ID or CONTAINER_NAME with the ID or name of the container you want to restart.
              • This command restarts the Docker container with the name mynginx.

                    8. Docker Kill:

                    Forcefully stops a container. Necessary when a container doesn’t respond to the standard stop command. Here is an example – 

                    Execution:

                    Output:

                    • Replace CONTAINER_ID or CONTAINER_NAME with the ID or name of the container you want to kill.
                    • This command forcefully terminates the Docker container with the name mynginx.

                          9. Docker Exec

                          Execute a command in a running container. It’s crucial for debugging and interactive processes. Here is an example – 

                          Execution:

                          Output:

                          • Replace OPTIONS, CONTAINER_ID or CONTAINER_NAME, and COMMAND [ARG...] with the appropriate values.
                          • This command lists the contents of the /usr/share/nginx/html directory inside the running container named mynginx.

                                10. Docker Login

                                Log in to a Docker registry. Essential for pushing and pulling private images. Here is an example – 

                                Execution:

                                Output:

                                • Replace [OPTIONS] and REGISTRY with the appropriate values.
                                • This command prompts you for your username and password to log in to the Docker registry located at myregistry.example.com.

                                    11. Docker Commit

                                    Create a new image from a container’s changes. Useful for versioning and snapshots. Here is an example – 

                                    Execution:

                                    Output:

                                    • Replace [OPTIONS], CONTAINER_ID or CONTAINER_NAME, and REPOSITORY[:TAG] with the appropriate values.
                                    • This command creates a new Docker image named mycustomimage with the tag latest based on the changes made to the container with the name mycontainer. The -m option is used to provide a commit message.

                                          12. Docker Push

                                          Uploads your custom image to Docker Hub or another registry. Key for sharing and collaboration. Here is an example – 

                                          Execution:

                                          Output:

                                          • Replace REGISTRY_IMAGE with the name and tag of the image you want to push.
                                          • This command pushes the local Docker image with the name myimage and tag tag to the Docker registry located at registry.example.com.

                                              13. Docker Network

                                              Manage Docker’s network settings. Crucial for setting up communication between containers. Here is an example – 

                                              Create a New Bridge Network:

                                              Example:

                                              • This command creates a new bridge network named mynetwork.

                                              14. Docker History

                                              Shows the history of an image, including its layers. Useful for understanding build processes and optimizations. Here is an example – 

                                              Execution:

                                              Output:

                                              • Replace IMAGE_NAME[:TAG] with the name and optional tag of the Docker image you want to inspect.
                                              • This command displays the history of the ubuntu image with the latest tag.

                                              15. Docker Rmi

                                              Removes images. Helps in maintaining a clean local development environment. Here is an example – 

                                              Command Structure:


                                              Example:

                                              • IMAGE: Replace this with the name or ID of the Docker image you want to remove.
                                              • [OPTIONS]: Various options can be specified to customize the removal process.
                                              • This command removes the Docker image named myimage with the specified tag.

                                                16. Docker Ps -a

                                                Lists all containers, both running and stopped. Essential for complete container management. Here is an example – 

                                                Command Structure:

                                                Example:

                                                • [OPTIONS]: Various options can be specified to customize the output or filter the list of containers.
                                                • This command lists all containers, both running and stopped, along with their details.

                                                  17. Docker Copy (Cp)

                                                  Copy files/folders between a container and the local file system. Useful for data migration and backup. Here is an example – 

                                                  Command Structure:

                                                  Examples:

                                                  • SRC_PATH: The source path on the local filesystem or inside the container.
                                                  • CONTAINER: The name or ID of the container.
                                                  • DEST_PATH: The destination path inside the container or on the local filesystem.

                                                  18. Docker Logs:

                                                  Displays logs of a container. Key for troubleshooting and monitoring. Here is an example – 

                                                  Command Structure:

                                                  Examples:

                                                  • CONTAINER: Replace this with the name or ID of the container whose logs you want to view.
                                                  • This command displays the logs of the running container named mycontainer.

                                                  19. Docker Volume

                                                  Manages persistent storage for containers. Essential for data persistence beyond the container lifecycle.

                                                  Creating a Docker Volume:

                                                  Command Structure:

                                                  Example:

                                                  • This command creates a Docker volume named myvolume.

                                                      20. Docker Logout

                                                      Logs out from a Docker registry. Important for security and switching between different registries. Here is an example – 

                                                      Command Structure:

                                                      Example:

                                                      • REGISTRY: (Optional) The URL of the Docker registry from which to log out. If not specified, it logs out from the default registry (Docker Hub).
                                                      • This command logs out from the default Docker registry (Docker Hub).

                                                      21. Docker System

                                                      Offers a wide range of system-level information. Useful for diagnostics and system optimization. Here is an example – 

                                                      Displaying Docker System Information:

                                                      Command Structure:

                                                      Example:

                                                        • This command displays detailed information about the Docker system, including resource usage, storage driver, and other relevant details.

                                                        22. Docker Context

                                                        Manages the context for Docker commands. Helpful for working with multiple Docker environments. Here is an example – 

                                                        Displaying Available Docker Contexts:

                                                        Example:

                                                        • This command lists the available Docker contexts along with their details, such as the current context, type, and name.

                                                          23. Docker Pause/Unpause

                                                          Temporarily suspends a container. Useful for resource management. Here is an example – 

                                                          Pause a Docker Container:

                                                          Command Structure:

                                                          Example:

                                                          • This command pauses the processes inside the running container named mycontainer.

                                                            Unpause a Docker Container:

                                                            Command Structure:

                                                            Example:

                                                            • This command resumes the processes inside the paused container named mycontainer.

                                                              24. Docker Plugin Install

                                                              Adds new functionalities to Docker. Enhances Docker capabilities. Here is an example – 

                                                              Command Structure:

                                                              Example:

                                                              • This command installs the vieux/sshfs Docker plugin. The --grant-all-permissions flag is optional and is used to automatically grant all required permissions to the plugin.

                                                                  25. Docker Image

                                                                  Manages Docker images. Central to Docker usage for building, listing, and removing images. Here is an example – 

                                                                  Pulling a Docker Image:

                                                                  Command Structure:

                                                                  Example:

                                                                  • This command pulls the nginx image from Docker Hub with the latest tag.

                                                                      26. Docker Import

                                                                      Creates an image from a tarball. Useful for importing pre-built environments. Here is an example – 

                                                                      Importing a Docker Image:

                                                                      Command Structure:

                                                                      Example:

                                                                      • This command creates a Docker image named myrepo/myimage with the tag mytag from the contents of the myarchive.tar.gz file.

                                                                      27. Docker Node

                                                                      Manages Swarm nodes. Important for Docker cluster management. Here is an example – 

                                                                      Initialize a Swarm on a Manager Node:

                                                                      • This command initializes a new Docker Swarm on the manager node and provides a token for worker nodes to join.
                                                                        MicrosoftTeams image 11 2 docker commands

                                                                        28. Docker Save

                                                                        Saves an image to a tar archive. Useful for image distribution and backups. Here is an example –

                                                                        Saving Docker Images:

                                                                        Command Structure:

                                                                        Example:

                                                                          • This command saves the ubuntu:latest and alpine:3.14 images to a tarball archive named my_images.tar.gz.

                                                                          29. Docker Tag

                                                                          Tags an image to a repository. Crucial for image versioning and organization. Here is an example – 

                                                                          Tagging a Docker Image:

                                                                          Command Structure:

                                                                          Example:

                                                                          • This command tags the local image myimage:latest with the new name myrepository/myimage:v1.0.

                                                                              30. Docker Login (Detailed)

                                                                              In-depth usage of the login command for various registries. Here is an example – 

                                                                              Logging into a Docker Registry:

                                                                              Command Structure:

                                                                              Example:

                                                                              • This command prompts you to enter your username and password to log in to the Docker registry located at myregistry.example.com.

                                                                                31. Docker Create

                                                                                Creates a container without starting it. Useful for setting up containers in advance. Here is an example – 

                                                                                Creating a Docker Container:

                                                                                Command Structure:

                                                                                Example:

                                                                                • This command creates a new container named mycontainer based on the nginx:latest image. It maps port 8080 on the host to port 80 on the container.

                                                                                    32. Docker Checkpoint

                                                                                    Manages checkpoints in container development. Useful for testing and rollbacks. Here is an example – 

                                                                                    Checkpoint the Container:

                                                                                    • This command uses CRIU to create a checkpoint of the specified process (container) and stores the checkpoint data in a directory.

                                                                                      33. Docker Cp (Detailed)

                                                                                      Detailed usage of copying files/folders to and from containers. Here is an example – 

                                                                                      Copying Files Between a Docker Container and the Local Filesystem:

                                                                                      Command Structure:

                                                                                      Examples:

                                                                                      Copy from a container to the local filesystem:

                                                                                      Copy from the local filesystem to a container:

                                                                                        To know more about docker command click here.

                                                                                        Conclusion

                                                                                        Mastering these top 31+ Docker commands in 2024 is vital for anyone working in software development, DevOps, or related fields. Understanding each command’s specific use case enhances your ability to effectively manage Docker containers and images, streamlining your development and deployment processes.

                                                                                            Check out – Top Python Data Visualization Libraries