Docker Explained: A Simple Guide for New Users

Docker Explained: A Simple Guide for New Users

Docker Version and Information

Here’s the table of top 100 Docker commands with their use cases and example.

1. Docker Version and Info

  1. Check Docker version
    Use Case: Ensure you're running the latest version of Docker for compatibility.

     bashCopy codedocker --version
    
  2. Show Docker version information
    Use Case: Verify the installed components and their versions.

     bashCopy codedocker version
    
  3. Show system-wide information about Docker
    Use Case: Get an overview of the Docker daemon, active containers, images, and volumes.

     bashCopy codedocker info
    

Image Management

  1. List all Docker images
    Use Case: Identify which images are available for deployment.

     bashCopy codedocker images
    
  2. Remove a Docker image
    Use Case: Clean up unused images to save disk space.

     bashCopy codedocker rmi <image_id>
    
  3. Pull an image from Docker Hub
    Use Case: Download the latest version of an image for deployment.

     bashCopy codedocker pull ubuntu
    
  4. Push an image to a repository
    Use Case: Share your image with a team or deploy it to production.

     bashCopy codedocker push username/repo:tag
    
  5. Build an image from a Dockerfile
    Use Case: Create a custom image tailored for your application.

     bashCopy codedocker build -t myapp .
    
  6. Tag an image
    Use Case: Prepare an image for a specific version release.

     bashCopy codedocker tag <image_id> username/repo:tag
    
  7. Save an image to a tar file
    Use Case: Backup an image for offline use or migration.

    bashCopy codedocker save -o myapp.tar myapp
    
  8. Load an image from a tar file
    Use Case: Restore an image from a backup.

    bashCopy codedocker load -i myapp.tar
    
  9. View the history of an image
    Use Case: Audit changes and layers in an image for compliance.

    bashCopy codedocker history myapp
    
  10. Inspect an image
    Use Case: Gather metadata and configuration details about an image.

    bashCopy codedocker inspect myapp
    
  11. Export a container's filesystem
    Use Case: Create a snapshot of a container's filesystem for analysis or debugging.

    bashCopy codedocker export <container_id> > mycontainer.tar
    
  12. Import a filesystem to create a new image
    Use Case: Create a new image from a previously exported filesystem.

    bashCopy codedocker import mycontainer.tar mynewimage
    

Container Operations

  1. Run a container from an image
    Use Case: Deploy a new instance of your application quickly.
bashCopy codedocker run ubuntu
  1. Run a container in detached mode
    Use Case: Run background processes such as web servers.
bashCopy codedocker run -d ubuntu
  1. Run a container interactively with a terminal
    Use Case: Troubleshoot applications directly within a container.
bashCopy codedocker run -it ubuntu
  1. List all running containers
    Use Case: Monitor active services and their status.
bashCopy codedocker ps
  1. List all containers (including stopped ones)
    Use Case: View the history of containers created for auditing purposes.
bashCopy codedocker ps -a
  1. Stop a running container
    Use Case: Gracefully shut down services for maintenance.
bashCopy codedocker stop <container_name>
  1. Start a stopped container
    Use Case: Quickly restart services without redeploying.
bashCopy codedocker start <container_name>
  1. Restart a container
    Use Case: Apply configuration changes that require a restart.
bashCopy codedocker restart <container_name>
  1. Remove a stopped container
    Use Case: Clean up resources after a deployment.
bashCopy codedocker rm <container_name>
  1. Execute a command inside a running container
    Use Case: Debug or modify running applications without stopping them.
bashCopy codedocker exec -it <container_name> bash
  1. View logs from a container
    Use Case: Monitor application output for debugging or performance analysis.
bashCopy codedocker logs <container_name>
  1. Display running processes in a container
    Use Case: Troubleshoot performance issues by checking active processes.
bashCopy codedocker top <container_name>
  1. Inspect a container
    Use Case: Review configuration and environment variables for troubleshooting.
bashCopy codedocker inspect <container_name>
  1. Pause a running container
    Use Case: Temporarily stop processes without terminating the container.
bashCopy codedocker pause <container_name>
  1. Unpause a paused container
    Use Case: Resume a paused service seamlessly.
bashCopy codedocker unpause <container_name>
  1. Kill a running container
    Use Case: Forcefully stop a container that is unresponsive.
bashCopy codedocker kill <container_name>
  1. Rename a container
    Use Case: Change the name of a container for better identification.
bashCopy codedocker rename <old_name> <new_name>
  1. Commit changes to a new image
    Use Case: Save modifications made to a container for future deployments.
bashCopy codedocker commit <container_name> mynewimage
  1. Monitor resource usage of containers
    Use Case: Analyze CPU and memory usage for performance tuning.
bashCopy codedocker stats

Volume Management

  1. List all Docker volumes
    Use Case: Identify available volumes for data persistence.
bashCopy codedocker volume ls
  1. Create a new Docker volume
    Use Case: Store data persistently across container restarts.
bashCopy codedocker volume create myvolume
  1. Inspect a volume
    Use Case: Review volume configuration and usage details.
bashCopy codedocker volume inspect myvolume
  1. Remove a Docker volume
    Use Case: Clean up unused volumes to free up disk space.
bashCopy codedocker volume rm myvolume
  1. Remove all unused volumes
    Use Case: Optimize storage by cleaning up orphaned volumes.
bashCopy codedocker volume prune

Network Management

  1. List all Docker networks
    Use Case: Understand network configurations for container communication.
bashCopy codedocker network ls
  1. Create a new Docker network
    Use Case: Isolate applications by creating a dedicated network.
bashCopy codedocker network create mynetwork
  1. Inspect a network
    Use Case: Review network settings and connected containers.
bashCopy codedocker network inspect mynetwork
  1. Connect a container to a network
    Use Case: Enable inter-container communication on a specific network.
bashCopy codedocker network connect mynetwork <container_name>
  1. Disconnect a container from a network
    Use Case: Restrict access of a container to a network.
bashCopy codedocker network disconnect mynetwork <container_name>
  1. Remove a Docker network
    Use Case: Clean up unused networks to reduce clutter.
bashCopy codedocker network rm mynetwork
  1. Remove all unused networks
    Use Case: Optimize network resources by cleaning up unused configurations.
bashCopy codedocker network prune

Docker Compose

  1. Start services defined in docker-compose.yml
    Use Case: Quickly deploy multi-container applications.
bashCopy codedocker-compose up
  1. Stop services
    Use Case: Cleanly shut down an application stack.
bashCopy codedocker-compose down
  1. List running services
    Use Case: Monitor the status of multi-container applications.
bashCopy codedocker-compose ps
  1. Build services defined in docker-compose.yml
    Use Case: Compile Docker images defined in the Compose file.
bashCopy codedocker-compose build
  1. Show logs from services
    Use Case: Monitor output from all services for debugging.
bashCopy codedocker-compose logs
  1. Execute a command in a service container
    Use Case: Troubleshoot or manage services directly.
bashCopy codedocker-compose exec <service_name> <command>
  1. Stop services
    Use Case: Temporarily halt application services.
bashCopy codedocker-compose stop
  1. Start stopped services
    Use Case: Quickly restart specific services without redeploying.
bashCopy codedocker-compose start
  1. Restart services
    Use Case: Apply configuration changes that require a restart.
bashCopy codedocker-compose restart
  1. Pull images for services
    Use Case: Update service images to the latest version.
bashCopy codedocker-compose pull
  1. Scale a service
    Use Case: Increase the number of replicas for a service to handle more load.
bashCopy codedocker-compose scale <service_name>=<replica_count>

Swarm Management

  1. Initialize a Docker Swarm
    Use Case: Set up a new cluster for distributed application deployment.
bashCopy codedocker swarm init
  1. Join an existing Swarm as a node
    Use Case: Add a new node to the cluster for scaling.
bashCopy codedocker swarm join --token <token> <manager_ip>:<port>
  1. Leave a Swarm
    Use Case: Remove a node from the cluster when it's no longer needed.
bashCopy codedocker swarm leave
  1. Create a service in a Docker Swarm
    Use Case: Deploy a new service in a distributed environment.
bashCopy codedocker service create --name myservice nginx
  1. List services in a Swarm
    Use Case: Monitor all active services in the cluster.
bashCopy codedocker service ls
  1. Inspect a service
    Use Case: Get detailed information about a service's configuration and status.
bashCopy codedocker service inspect myservice
  1. Scale a service
    Use Case: Adjust the number of running replicas based on load.
bashCopy codedocker service scale myservice=5
  1. Remove a service
    Use Case: Clean up services that are no longer needed.
bashCopy codedocker service rm myservice
  1. List nodes in the Swarm
    Use Case: View all nodes currently part of the Swarm.
bashCopy codedocker node ls
  1. Inspect a node
    Use Case: Gather details about a specific node's status and configuration.
bashCopy codedocker node inspect <node_name>
  1. Promote a node to manager
    Use Case: Upgrade a worker node to manager for cluster management.
bashCopy codedocker node promote <node_name>
  1. Demote a manager to worker
    Use Case: Remove management privileges from a node.
bashCopy codedocker node demote <node_name>

Pruning and Cleanup

  1. Remove all stopped containers
    Use Case: Free up resources by cleaning up old containers.
bashCopy codedocker container prune
  1. Remove all unused images
    Use Case: Clean up disk space by removing images not associated with running containers.
bashCopy codedocker image prune
  1. Remove all unused volumes
    Use Case: Free up space by deleting volumes that are not in use.
bashCopy codedocker volume prune
  1. Remove all unused networks
    Use Case: Optimize Docker's networking configuration.
bashCopy codedocker network prune
  1. Remove all unused data
    Use Case: Perform a comprehensive cleanup of unused containers, images, and networks.
bashCopy codedocker system prune
  1. Show Docker disk usage
    Use Case: Analyze storage usage to identify potential areas for cleanup.
bashCopy codedocker system df

Authentication

  1. Log into Docker Hub
    Use Case: Authenticate to push images to your Docker Hub account.
bashCopy codedocker login
  1. Log out from Docker Hub
    Use Case: End your session for security purposes.
bashCopy codedocker logout

Health Checks and Events

  1. Show Docker events
    Use Case: Monitor real-time events in the Docker daemon for troubleshooting.
bashCopy codedocker events
  1. Check container health status
    Use Case: Verify if a container is healthy based on defined health checks.
bashCopy codedocker inspect --format='{{.State.Health.Status}}' <container_name>

Miscellaneous

  1. Copy files from container to host
    Use Case: Retrieve logs or configuration files from a container for analysis.
bashCopy codedocker cp <container_name>:<path> <host_path>
  1. Copy files from host to container
    Use Case: Update configuration files in a running container.
bashCopy codedocker cp <host_path> <container_name>:<path>
  1. Get the ID of a container
    Use Case: Obtain the container ID for further management.
bashCopy codedocker inspect --format '{{.Id}}' <container_name>
  1. Run a container with environment variables
    Use Case: Pass sensitive configurations such as API keys at runtime.
bashCopy codedocker run -e "API_KEY=myapikey" myapp
  1. Limit CPU and memory resources for a container
    Use Case: Control resource allocation for performance tuning.
bashCopy codedocker run --memory="256m" --cpus="1.0" myapp
  1. Run a container with a specific user
    Use Case: Execute a container process under a specific user for security.
bashCopy codedocker run -u 1000 myapp
  1. Run a container with port mapping
    Use Case: Expose container services to the host network.
bashCopy codedocker run -p 8080:80 myapp
  1. Run a container with volume mapping
    Use Case: Persist data generated by a container.
bashCopy codedocker run -v myvolume:/data myapp
  1. Run a container in the background with logs
    Use Case: Monitor logs while running a service in the background.
bashCopy codedocker run -d --name myapp myapp && docker logs -f myapp
  1. Run a container with a restart policy
    Use Case: Ensure that critical services are always running.
bashCopy codedocker run --restart unless-stopped myapp
  1. Remove all stopped containers and unused images in one command
    Use Case: Perform a quick cleanup after testing.
bashCopy codedocker system prune -a
  1. Use a Docker Compose file for multiple environments
    Use Case: Define separate configurations for development, testing, and production.
yamlCopy codeversion: '3'
services:
  web:
    image: myapp
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./data:/data
  1. Set up a health check in Dockerfile
    Use Case: Monitor the health of a service automatically.
dockerfileCopy codeHEALTHCHECK CMD curl --fail http://localhost:80/ || exit 1
  1. Access Docker daemon remotely
    Use Case: Manage Docker instances from a remote location.
bashCopy codeexport DOCKER_HOST=tcp://<remote_host>:2375
  1. Use a custom Docker network for inter-container communication
    Use Case: Allow containers to communicate without exposing services publicly.
bashCopy codedocker network create mycustomnetwork
docker run --network mycustomnetwork myapp
  1. Inspect the differences between two images
    Use Case: Analyze changes between two versions of an image.
bashCopy codedocker diff <container_name>
  1. Attach to a running container
    Use Case: Connect to a running container to interact with it directly.
bashCopy codedocker attach <container_name>
  1. Change the CPU priority of a container
    Use Case: Adjust container scheduling to optimize resource usage.
bashCopy codedocker run --cpu-shares=512 myapp
  1. Create a container from an existing one
    Use Case: Clone a container for testing or backup.
bashCopy codedocker create --name myclone myapp
  1. Check the exit status of a container
    Use Case: Determine if a container completed successfully.
bashCopy codedocker inspect -f '{{.State.ExitCode}}' <container_name>
  1. Run a one-off command in a service
    Use Case: Execute maintenance tasks in a service without affecting the running instances.
bashCopy codedocker-compose run --rm <service_name> <command>
CommandUse CaseExample

docker --version

Ensure you're running the latest version of Docker for compatibility.

docker --version

docker version

Verify the installed components and their versions.

docker version

docker info

Get an overview of the Docker daemon, active containers, images, and volumes.

docker info

docker images

Identify which images are available for deployment.

docker images

docker rmi <image_id>

Clean up unused images to save disk space.

docker rmi myimage:latest

docker pull ubuntu

Download the latest version of an image for deployment.

docker pull ubuntu

docker push username/repo:tag

Share your image with a team or deploy it to production.

docker push myusername/myapp:latest

docker build -t myapp .

Create a custom image tailored for your application.

docker build -t myapp .

docker tag <image_id> username/repo:tag

Prepare an image for a specific version release.

docker tag myapp:latest myusername/myapp:v1

docker save -o myapp.tar myapp

Backup an image for offline use or migration.

docker save -o myapp.tar myapp:latest

docker load -i myapp.tar

Restore an image from a backup.

docker load -i myapp.tar

docker history myapp

Audit changes and layers in an image for compliance.

docker history myapp

docker inspect myapp

Gather metadata and configuration details about an image.

docker inspect myapp

docker export <container_id> > mycontainer.tar

Create a snapshot of a container's filesystem for analysis or debugging.

docker export mycontainer > mycontainer.tar

docker import mycontainer.tar mynewimage

Create a new image from a previously exported filesystem.

docker import mycontainer.tar mynewimage

docker run ubuntu

Deploy a new instance of your application quickly.

docker run ubuntu

docker run -d ubuntu

Run background processes such as web servers.

docker run -d nginx

docker run -it ubuntu

Troubleshoot applications directly within a container.

docker run -it ubuntu /bin/bash

docker ps

Monitor active services and their status.

docker ps

docker ps -a

View the history of containers created for auditing purposes.

docker ps -a

docker stop <container_name>

Gracefully shut down services for maintenance.

docker stop myapp

docker start <container_name>

Quickly restart services without redeploying.

docker start myapp

docker restart <container_name>

Apply configuration changes that require a restart.

docker restart myapp

docker rm <container_name>

Clean up resources after a deployment.

docker rm myapp

docker exec -it <container_name> bash

Debug or modify running applications without stopping them.

docker exec -it myapp bash

docker logs <container_name>

Monitor application output for debugging or performance analysis.

docker logs myapp

docker top <container_name>

Troubleshoot performance issues by checking active processes.

docker top myapp

docker inspect <container_name>

Review configuration and environment variables for troubleshooting.

docker inspect myapp

docker pause <container_name>

Temporarily stop processes without terminating the container.

docker pause myapp

docker unpause <container_name>

Resume a paused service seamlessly.

docker unpause myapp

docker kill <container_name>

Forcefully stop a container that is unresponsive.

docker kill myapp

docker rename <old_name> <new_name>

Change the name of a container for better identification.

docker rename myapp myapp_old

docker commit <container_name> mynewimage

Save modifications made to a container for future deployments.

docker commit myapp mynewimage

docker stats

Analyze CPU and memory usage for performance tuning.

docker stats

docker volume ls

Identify available volumes for data persistence.

docker volume ls

docker volume create myvolume

Store data persistently across container restarts.

docker volume create myvolume

docker volume inspect myvolume

Review volume configuration and usage details.

docker volume inspect myvolume

docker volume rm myvolume

Clean up unused volumes to free up disk space.

docker volume rm myvolume

docker volume prune

Optimize storage by cleaning up orphaned volumes.

docker volume prune

docker network ls

Understand network configurations for container communication.

docker network ls

docker network create mynetwork

Isolate applications by creating a dedicated network.

docker network create mynetwork

docker network inspect mynetwork

Review network settings and connected containers.

docker network inspect mynetwork

docker network connect mynetwork <container_name>

Enable inter-container communication on a specific network.

docker network connect mynetwork myapp

docker network disconnect mynetwork <container_name>

Restrict access of a container to a network.

docker network disconnect mynetwork myapp

docker network rm mynetwork

Clean up unused networks to reduce clutter.

docker network rm mynetwork

docker network prune

Optimize network resources by cleaning up unused configurations.

docker network prune

docker-compose up

Quickly deploy multi-container applications.

docker-compose up

docker-compose down

Cleanly shut down an application stack.

docker-compose down

docker-compose ps

Monitor the status of multi-container applications.

docker-compose ps

docker-compose build

Compile Docker images defined in the Compose file.

docker-compose build

docker-compose logs

Monitor output from all services for debugging.

docker-compose logs

docker-compose exec <service_name> <command>

Troubleshoot or manage services directly.

docker-compose exec web bash

docker-compose stop

Temporarily halt application services.

docker-compose stop

docker-compose start

Quickly restart specific services without redeploying.

docker-compose start

docker-compose restart

Apply configuration changes that require a restart.

docker-compose restart

docker-compose pull

Update service images to the latest version.

docker-compose pull

docker-compose scale <service_name>=<replica_count>

Increase the number of replicas for a service to handle more load.

docker-compose scale web=3

docker swarm init

Set up a new cluster for distributed application deployment.

docker swarm init

docker swarm join --token <token> <manager_ip>:<port>

Add a new node to the cluster for scaling.

docker swarm join --token <token> 192.168.1.2:2377

docker swarm leave

Remove a node from the cluster when it's no longer needed.

docker swarm leave

docker service create --name myservice nginx

Deploy a new service in a distributed environment.

docker service create --name myservice nginx

docker service ls

Monitor all active services in the cluster.

docker service ls

docker service inspect myservice

Get detailed information about a service's configuration and status.

docker service inspect myservice

docker service scale myservice=5

Adjust the number of running replicas based on load.

docker service scale myservice=5

docker service rm myservice

Clean up services that are no longer needed.

docker service rm myservice

docker node ls

View all nodes currently part of the Swarm.

docker node ls

docker node inspect <node_name>

Gather details about a specific node's status and configuration.

docker node inspect mynode

docker node promote <node_name>

Upgrade a worker node to manager for cluster management.

docker node promote mynode

docker node demote <node_name>

Remove management privileges from a node.

docker node demote mynode

docker container prune

Free up resources by cleaning up old containers.

docker container prune

docker image prune

Clean up disk space by removing images not associated with running containers.

docker image prune

docker volume prune

Free up space by deleting volumes that are not in use.

docker volume prune

docker network prune

Optimize Docker's networking configuration.

docker network prune

docker system prune

Perform a comprehensive cleanup of unused containers, images, and networks.

docker system prune

docker system df

Analyze storage usage to identify potential areas for cleanup.

docker system df

docker login

Authenticate to push images to your Docker Hub account.

docker login

docker logout

End your session for security purposes.

docker logout

docker events

Monitor real-time events in the Docker daemon for troubleshooting.

docker events

docker inspect --format='{{.State.Health.Status}}' <container_name>

Verify if a container is healthy based on defined health checks.

docker inspect --format='{{.State.Health.Status}}' myapp

docker cp <container_name>:<path> <host_path>

Retrieve logs or configuration files from a container for analysis.

docker cp myapp:/var/log/app.log ./app.log

docker cp <host_path> <container_name>:<path>

Update configuration files in a running container.

docker cp ./config.conf myapp:/etc/config.conf

docker inspect --format '{{.Id}}' <container_name>

Obtain the container ID for further management.

docker inspect --format '{{.Id}}' myapp

docker run -e "API_KEY=myapikey" myapp

Pass sensitive configurations such as API keys at runtime.

docker run -e "API_KEY=myapikey" myapp

docker run --memory="256m" --cpus="1.0" myapp

Control resource allocation for performance tuning.

docker run --memory="256m" --cpus="1.0" myapp

docker run -u 1000 myapp

Execute a container process under a specific user for security.

docker run -u 1000 myapp

docker run -p 8080:80 myapp

Expose container services to the host network.

docker run -p 8080:80 myapp

docker run -v myvolume:/data myapp

Persist data generated by a container.

docker run -v myvolume:/data myapp

docker run -d --name myapp myapp && docker logs -f myapp

Monitor logs while running a service in the background.

docker run -d --name myapp myapp && docker logs -f myapp

docker run --restart unless-stopped myapp

Ensure that critical services are always running.

docker run --restart unless-stopped myapp

docker system prune -a

Perform a quick cleanup after testing.

docker system prune -a

docker-compose.yml

Define separate configurations for development, testing, and production.

docker-compose -f docker-compose.yml up

HEALTHCHECK CMD curl --fail http://localhost:80

Make sure a service is running well by checking its status regularly.

HEALTHCHECK CMD curl --fail http://localhost:80