A Comprehensive Guide to Docker Commands: Simplified for Everyone

Docker is an essential tool for modern developers, allowing them to create, deploy, and run applications in containers. Containers are lightweight, portable, and ensure consistency across multiple environments. In this guide, we’ll break down Docker commands into easy-to-understand categories, helping you navigate Docker effortlessly.

Docker Basics

Before diving into Docker commands, let’s start with the basics:

  • Checking Docker Version

    1docker --version
    

    This command shows the installed Docker version.

  • Displaying System Information

    1docker info
    

    Get detailed information about your Docker setup.

Working with Docker Images

Docker images are templates for creating containers. Here’s how to manage them:

  • Listing Images

    1docker images
    

    View all available images on your system.

  • Pulling an Image

    1docker pull [image]
    

    Download an image from Docker Hub or another registry.

  • Building an Image

    1docker build -t [name:tag] [path]
    

    Create an image from a Dockerfile.

  • Removing an Image

    1docker rmi [image]
    

    Delete one or more images.

  • Tagging an Image

    1docker tag [image] [new_image:tag]
    

    Add a new tag to an image for easier reference.

Managing Docker Containers

Containers are running instances of Docker images. Here’s how to handle them:

  • Listing Running Containers

    1docker ps
    

    See all active containers.

  • Listing All Containers

    1docker ps -a
    

    View all containers, including stopped ones.

  • Running a Container

    1docker run [options] [image]
    

    Start a new container from an image.

  • Starting a Stopped Container

    1docker start [container]
    

    Restart a previously stopped container.

  • Stopping a Running Container

    1docker stop [container]
    

    Gracefully stop a running container.

  • Restarting a Container

    1docker restart [container]
    

    Restart a container.

  • Removing a Container

    1docker rm [container]
    

    Delete one or more containers.

  • Fetching Container Logs

    1docker logs [container]
    

    View the logs of a container.

  • Executing Commands in a Container

    1docker exec [options] [container] [command]
    

    Run commands inside a running container.

  • Attaching to a Running Container

    1docker attach [container]
    

    Connect to a running container.

  • Inspecting Containers

    1docker inspect [container/image]
    

    Get detailed information about containers or images.

  • Pausing Container Processes

    1docker pause [container]
    

    Pause all processes within one or more containers.

  • Unpausing Container Processes

    1docker unpause [container]
    

    Resume all processes within one or more containers.

  • Killing a Running Container

    1docker kill [container]
    

    Forcefully stop a container.

Managing Docker Volumes

Volumes persist data generated by and used by Docker containers:

  • Creating a Volume

    1docker volume create [volume_name]
    

    Create a new volume.

  • Listing Volumes

    1docker volume ls
    

    View all volumes.

  • Removing a Volume

    1docker volume rm [volume]
    

    Delete one or more volumes.

  • Inspecting Volumes

    1docker volume inspect [volume]
    

    Get detailed information about a volume.

Managing Docker Networks

Networks connect Docker containers:

  • Creating a Network

    1docker network create [network_name]
    

    Set up a new network.

  • Listing Networks

    1docker network ls
    

    View all networks.

  • Removing a Network

    1docker network rm [network]
    

    Delete one or more networks.

  • Inspecting Networks

    1docker network inspect [network]
    

    Get detailed information about a network.

  • Connecting a Container to a Network

    1docker network connect [network] [container]
    

    Attach a container to a network.

  • Disconnecting a Container from a Network

    1docker network disconnect [network] [container]
    

    Detach a container from a network.

Docker Compose Commands

Docker Compose simplifies multi-container Docker applications:

  • Starting Services

    1docker-compose up
    

    Build, create, start, and attach to containers for a service.

  • Stopping Services

    1docker-compose down
    

    Stop and remove containers, networks, images, and volumes.

  • Listing Services

    1docker-compose ps
    

    View all containers in a service.

  • Building Services

    1docker-compose build
    

    Build or rebuild services.

  • Viewing Logs

    1docker-compose logs
    

    See logs from containers.

  • Restarting Services

    1docker-compose restart
    

    Restart services.

  • Stopping Services

    1docker-compose stop
    

    Stop running services.

  • Removing Services

    1docker-compose rm
    

    Remove stopped service containers.

Docker Swarm Commands

Docker Swarm manages clusters of Docker nodes:

  • Initializing a Swarm

    1docker swarm init
    

    Set up a new swarm.

  • Displaying Join Command for Workers

    1docker swarm join-token worker
    

    Show the command to add worker nodes.

  • Displaying Join Command for Managers

    1docker swarm join-token manager
    

    Show the command to add manager nodes.

  • Listing Swarm Nodes

    1docker node ls
    

    View all nodes in the swarm.

  • Creating a Service

    1docker service create [options] [image]
    

    Create a new service.

  • Listing Services

    1docker service ls
    

    View all services.

  • Listing Service Tasks

    1docker service ps [service]
    

    View tasks of a service.

  • Scaling a Service

    1docker service scale [service]=[replicas]
    

    Adjust the number of replicas for a service.

Docker Configuration Management

Manage Docker configurations:

  • Creating a Configuration

    1docker config create [config_name] [file]
    

    Create a new configuration.

  • Listing Configurations

    1docker config ls
    

    View all configurations.

  • Removing a Configuration

    1docker config rm [config]
    

    Delete one or more configurations.

  • Inspecting Configurations

    1docker config inspect [config]
    

    Get detailed information about a configuration.

Docker Secrets

Securely manage sensitive data with Docker secrets:

  • Creating a Secret

    1docker secret create [secret_name] [file]
    

    Create a new secret.

  • Listing Secrets

    1docker secret ls
    

    View all secrets.

  • Removing a Secret

    1docker secret rm [secret]
    

    Delete one or more secrets.

  • Inspecting Secrets

    1docker secret inspect [secret]
    

    Get detailed information about a secret.

Docker Container Management

Handle advanced container management tasks:

  • Copying Files from Container to Host

    1docker cp [container]:[path] [host_path]
    

    Copy files from a container to your local machine.

  • Copying Files from Host to Container

    1docker cp [host_path] [container]:[path]
    

    Transfer files from your machine to a container.

  • Renaming a Container

    1docker rename [container] [new_name]
    

    Change the name of a container.

  • Updating Container Configuration

    1docker update [options] [container]
    

    Modify container settings.

  • Displaying Container Stats

    1docker stats [container]
    

    See live resource usage statistics for a container.

  • Displaying Running Processes in a Container

    1docker top [container]
    

    List processes running inside a container.

Docker Pruning

Clean up unused Docker resources:

  • Pruning the System

    1docker system prune
    

    Remove unused data, including images, containers, volumes, and networks.

  • Pruning Images

    1docker image prune
    

    Remove unused images.

  • Pruning Containers

    1docker container prune
    

    Remove stopped containers.

  • Pruning Volumes

    1docker volume prune
    

    Delete unused volumes.

  • Pruning Networks

    1docker network prune
    

    Remove unused networks.

Conclusion

Docker simplifies the development and deployment of applications by packaging them in containers. This guide provides a comprehensive list of Docker commands, organized for easy understanding. Whether you're managing images, containers, volumes, or networks, these commands will help you make the most of Docker's powerful capabilities.