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
Check Docker version
Use Case: Ensure you're running the latest version of Docker for compatibility.bashCopy codedocker --versionShow Docker version information
Use Case: Verify the installed components and their versions.bashCopy codedocker versionShow system-wide information about Docker
Use Case: Get an overview of the Docker daemon, active containers, images, and volumes.bashCopy codedocker info
Image Management
List all Docker images
Use Case: Identify which images are available for deployment.bashCopy codedocker imagesRemove a Docker image
Use Case: Clean up unused images to save disk space.bashCopy codedocker rmi <image_id>Pull an image from Docker Hub
Use Case: Download the latest version of an image for deployment.bashCopy codedocker pull ubuntuPush an image to a repository
Use Case: Share your image with a team or deploy it to production.bashCopy codedocker push username/repo:tagBuild an image from a Dockerfile
Use Case: Create a custom image tailored for your application.bashCopy codedocker build -t myapp .Tag an image
Use Case: Prepare an image for a specific version release.bashCopy codedocker tag <image_id> username/repo:tagSave an image to a tar file
Use Case: Backup an image for offline use or migration.bashCopy codedocker save -o myapp.tar myappLoad an image from a tar file
Use Case: Restore an image from a backup.bashCopy codedocker load -i myapp.tarView the history of an image
Use Case: Audit changes and layers in an image for compliance.bashCopy codedocker history myappInspect an image
Use Case: Gather metadata and configuration details about an image.bashCopy codedocker inspect myappExport a container's filesystem
Use Case: Create a snapshot of a container's filesystem for analysis or debugging.bashCopy codedocker export <container_id> > mycontainer.tarImport 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
- Run a container from an image
Use Case: Deploy a new instance of your application quickly.
bashCopy codedocker run ubuntu
- Run a container in detached mode
Use Case: Run background processes such as web servers.
bashCopy codedocker run -d ubuntu
- Run a container interactively with a terminal
Use Case: Troubleshoot applications directly within a container.
bashCopy codedocker run -it ubuntu
- List all running containers
Use Case: Monitor active services and their status.
bashCopy codedocker ps
- List all containers (including stopped ones)
Use Case: View the history of containers created for auditing purposes.
bashCopy codedocker ps -a
- Stop a running container
Use Case: Gracefully shut down services for maintenance.
bashCopy codedocker stop <container_name>
- Start a stopped container
Use Case: Quickly restart services without redeploying.
bashCopy codedocker start <container_name>
- Restart a container
Use Case: Apply configuration changes that require a restart.
bashCopy codedocker restart <container_name>
- Remove a stopped container
Use Case: Clean up resources after a deployment.
bashCopy codedocker rm <container_name>
- Execute a command inside a running container
Use Case: Debug or modify running applications without stopping them.
bashCopy codedocker exec -it <container_name> bash
- View logs from a container
Use Case: Monitor application output for debugging or performance analysis.
bashCopy codedocker logs <container_name>
- Display running processes in a container
Use Case: Troubleshoot performance issues by checking active processes.
bashCopy codedocker top <container_name>
- Inspect a container
Use Case: Review configuration and environment variables for troubleshooting.
bashCopy codedocker inspect <container_name>
- Pause a running container
Use Case: Temporarily stop processes without terminating the container.
bashCopy codedocker pause <container_name>
- Unpause a paused container
Use Case: Resume a paused service seamlessly.
bashCopy codedocker unpause <container_name>
- Kill a running container
Use Case: Forcefully stop a container that is unresponsive.
bashCopy codedocker kill <container_name>
- Rename a container
Use Case: Change the name of a container for better identification.
bashCopy codedocker rename <old_name> <new_name>
- Commit changes to a new image
Use Case: Save modifications made to a container for future deployments.
bashCopy codedocker commit <container_name> mynewimage
- Monitor resource usage of containers
Use Case: Analyze CPU and memory usage for performance tuning.
bashCopy codedocker stats
Volume Management
- List all Docker volumes
Use Case: Identify available volumes for data persistence.
bashCopy codedocker volume ls
- Create a new Docker volume
Use Case: Store data persistently across container restarts.
bashCopy codedocker volume create myvolume
- Inspect a volume
Use Case: Review volume configuration and usage details.
bashCopy codedocker volume inspect myvolume
- Remove a Docker volume
Use Case: Clean up unused volumes to free up disk space.
bashCopy codedocker volume rm myvolume
- Remove all unused volumes
Use Case: Optimize storage by cleaning up orphaned volumes.
bashCopy codedocker volume prune
Network Management
- List all Docker networks
Use Case: Understand network configurations for container communication.
bashCopy codedocker network ls
- Create a new Docker network
Use Case: Isolate applications by creating a dedicated network.
bashCopy codedocker network create mynetwork
- Inspect a network
Use Case: Review network settings and connected containers.
bashCopy codedocker network inspect mynetwork
- Connect a container to a network
Use Case: Enable inter-container communication on a specific network.
bashCopy codedocker network connect mynetwork <container_name>
- Disconnect a container from a network
Use Case: Restrict access of a container to a network.
bashCopy codedocker network disconnect mynetwork <container_name>
- Remove a Docker network
Use Case: Clean up unused networks to reduce clutter.
bashCopy codedocker network rm mynetwork
- Remove all unused networks
Use Case: Optimize network resources by cleaning up unused configurations.
bashCopy codedocker network prune
Docker Compose
- Start services defined in docker-compose.yml
Use Case: Quickly deploy multi-container applications.
bashCopy codedocker-compose up
- Stop services
Use Case: Cleanly shut down an application stack.
bashCopy codedocker-compose down
- List running services
Use Case: Monitor the status of multi-container applications.
bashCopy codedocker-compose ps
- Build services defined in docker-compose.yml
Use Case: Compile Docker images defined in the Compose file.
bashCopy codedocker-compose build
- Show logs from services
Use Case: Monitor output from all services for debugging.
bashCopy codedocker-compose logs
- Execute a command in a service container
Use Case: Troubleshoot or manage services directly.
bashCopy codedocker-compose exec <service_name> <command>
- Stop services
Use Case: Temporarily halt application services.
bashCopy codedocker-compose stop
- Start stopped services
Use Case: Quickly restart specific services without redeploying.
bashCopy codedocker-compose start
- Restart services
Use Case: Apply configuration changes that require a restart.
bashCopy codedocker-compose restart
- Pull images for services
Use Case: Update service images to the latest version.
bashCopy codedocker-compose pull
- 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
- Initialize a Docker Swarm
Use Case: Set up a new cluster for distributed application deployment.
bashCopy codedocker swarm init
- 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>
- Leave a Swarm
Use Case: Remove a node from the cluster when it's no longer needed.
bashCopy codedocker swarm leave
- Create a service in a Docker Swarm
Use Case: Deploy a new service in a distributed environment.
bashCopy codedocker service create --name myservice nginx
- List services in a Swarm
Use Case: Monitor all active services in the cluster.
bashCopy codedocker service ls
- Inspect a service
Use Case: Get detailed information about a service's configuration and status.
bashCopy codedocker service inspect myservice
- Scale a service
Use Case: Adjust the number of running replicas based on load.
bashCopy codedocker service scale myservice=5
- Remove a service
Use Case: Clean up services that are no longer needed.
bashCopy codedocker service rm myservice
- List nodes in the Swarm
Use Case: View all nodes currently part of the Swarm.
bashCopy codedocker node ls
- Inspect a node
Use Case: Gather details about a specific node's status and configuration.
bashCopy codedocker node inspect <node_name>
- Promote a node to manager
Use Case: Upgrade a worker node to manager for cluster management.
bashCopy codedocker node promote <node_name>
- Demote a manager to worker
Use Case: Remove management privileges from a node.
bashCopy codedocker node demote <node_name>
Pruning and Cleanup
- Remove all stopped containers
Use Case: Free up resources by cleaning up old containers.
bashCopy codedocker container prune
- Remove all unused images
Use Case: Clean up disk space by removing images not associated with running containers.
bashCopy codedocker image prune
- Remove all unused volumes
Use Case: Free up space by deleting volumes that are not in use.
bashCopy codedocker volume prune
- Remove all unused networks
Use Case: Optimize Docker's networking configuration.
bashCopy codedocker network prune
- Remove all unused data
Use Case: Perform a comprehensive cleanup of unused containers, images, and networks.
bashCopy codedocker system prune
- Show Docker disk usage
Use Case: Analyze storage usage to identify potential areas for cleanup.
bashCopy codedocker system df
Authentication
- Log into Docker Hub
Use Case: Authenticate to push images to your Docker Hub account.
bashCopy codedocker login
- Log out from Docker Hub
Use Case: End your session for security purposes.
bashCopy codedocker logout
Health Checks and Events
- Show Docker events
Use Case: Monitor real-time events in the Docker daemon for troubleshooting.
bashCopy codedocker events
- 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
- 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>
- Copy files from host to container
Use Case: Update configuration files in a running container.
bashCopy codedocker cp <host_path> <container_name>:<path>
- Get the ID of a container
Use Case: Obtain the container ID for further management.
bashCopy codedocker inspect --format '{{.Id}}' <container_name>
- 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
- 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
- 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
- Run a container with port mapping
Use Case: Expose container services to the host network.
bashCopy codedocker run -p 8080:80 myapp
- Run a container with volume mapping
Use Case: Persist data generated by a container.
bashCopy codedocker run -v myvolume:/data myapp
- 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
- Run a container with a restart policy
Use Case: Ensure that critical services are always running.
bashCopy codedocker run --restart unless-stopped myapp
- Remove all stopped containers and unused images in one command
Use Case: Perform a quick cleanup after testing.
bashCopy codedocker system prune -a
- 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
- 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
- Access Docker daemon remotely
Use Case: Manage Docker instances from a remote location.
bashCopy codeexport DOCKER_HOST=tcp://<remote_host>:2375
- 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
- Inspect the differences between two images
Use Case: Analyze changes between two versions of an image.
bashCopy codedocker diff <container_name>
- Attach to a running container
Use Case: Connect to a running container to interact with it directly.
bashCopy codedocker attach <container_name>
- Change the CPU priority of a container
Use Case: Adjust container scheduling to optimize resource usage.
bashCopy codedocker run --cpu-shares=512 myapp
- Create a container from an existing one
Use Case: Clone a container for testing or backup.
bashCopy codedocker create --name myclone myapp
- Check the exit status of a container
Use Case: Determine if a container completed successfully.
bashCopy codedocker inspect -f '{{.State.ExitCode}}' <container_name>
- 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>
| Command | Use Case | Example |
| Ensure you're running the latest version of Docker for compatibility. |
|
| Verify the installed components and their versions. |
|
| Get an overview of the Docker daemon, active containers, images, and volumes. |
|
| Identify which images are available for deployment. |
|
| Clean up unused images to save disk space. |
|
| Download the latest version of an image for deployment. |
|
| Share your image with a team or deploy it to production. |
|
| Create a custom image tailored for your application. |
|
| Prepare an image for a specific version release. |
|
| Backup an image for offline use or migration. |
|
| Restore an image from a backup. |
|
| Audit changes and layers in an image for compliance. |
|
| Gather metadata and configuration details about an image. |
|
| Create a snapshot of a container's filesystem for analysis or debugging. |
|
| Create a new image from a previously exported filesystem. |
|
| Deploy a new instance of your application quickly. |
|
| Run background processes such as web servers. |
|
| Troubleshoot applications directly within a container. |
|
| Monitor active services and their status. |
|
| View the history of containers created for auditing purposes. |
|
| Gracefully shut down services for maintenance. |
|
| Quickly restart services without redeploying. |
|
| Apply configuration changes that require a restart. |
|
| Clean up resources after a deployment. |
|
| Debug or modify running applications without stopping them. |
|
| Monitor application output for debugging or performance analysis. |
|
| Troubleshoot performance issues by checking active processes. |
|
| Review configuration and environment variables for troubleshooting. |
|
| Temporarily stop processes without terminating the container. |
|
| Resume a paused service seamlessly. |
|
| Forcefully stop a container that is unresponsive. |
|
| Change the name of a container for better identification. |
|
| Save modifications made to a container for future deployments. |
|
| Analyze CPU and memory usage for performance tuning. |
|
| Identify available volumes for data persistence. |
|
| Store data persistently across container restarts. |
|
| Review volume configuration and usage details. |
|
| Clean up unused volumes to free up disk space. |
|
| Optimize storage by cleaning up orphaned volumes. |
|
| Understand network configurations for container communication. |
|
| Isolate applications by creating a dedicated network. |
|
| Review network settings and connected containers. |
|
| Enable inter-container communication on a specific network. |
|
| Restrict access of a container to a network. |
|
| Clean up unused networks to reduce clutter. |
|
| Optimize network resources by cleaning up unused configurations. |
|
| Quickly deploy multi-container applications. |
|
| Cleanly shut down an application stack. |
|
| Monitor the status of multi-container applications. |
|
| Compile Docker images defined in the Compose file. |
|
| Monitor output from all services for debugging. |
|
| Troubleshoot or manage services directly. |
|
| Temporarily halt application services. |
|
| Quickly restart specific services without redeploying. |
|
| Apply configuration changes that require a restart. |
|
| Update service images to the latest version. |
|
| Increase the number of replicas for a service to handle more load. |
|
| Set up a new cluster for distributed application deployment. |
|
| Add a new node to the cluster for scaling. |
|
| Remove a node from the cluster when it's no longer needed. |
|
| Deploy a new service in a distributed environment. |
|
| Monitor all active services in the cluster. |
|
| Get detailed information about a service's configuration and status. |
|
| Adjust the number of running replicas based on load. |
|
| Clean up services that are no longer needed. |
|
| View all nodes currently part of the Swarm. |
|
| Gather details about a specific node's status and configuration. |
|
| Upgrade a worker node to manager for cluster management. |
|
| Remove management privileges from a node. |
|
| Free up resources by cleaning up old containers. |
|
| Clean up disk space by removing images not associated with running containers. |
|
| Free up space by deleting volumes that are not in use. |
|
| Optimize Docker's networking configuration. |
|
| Perform a comprehensive cleanup of unused containers, images, and networks. |
|
| Analyze storage usage to identify potential areas for cleanup. |
|
| Authenticate to push images to your Docker Hub account. |
|
| End your session for security purposes. |
|
| Monitor real-time events in the Docker daemon for troubleshooting. |
|
| Verify if a container is healthy based on defined health checks. |
|
| Retrieve logs or configuration files from a container for analysis. |
|
| Update configuration files in a running container. |
|
| Obtain the container ID for further management. |
|
| Pass sensitive configurations such as API keys at runtime. |
|
| Control resource allocation for performance tuning. |
|
| Execute a container process under a specific user for security. |
|
| Expose container services to the host network. |
|
| Persist data generated by a container. |
|
| Monitor logs while running a service in the background. |
|
| Ensure that critical services are always running. |
|
| Perform a quick cleanup after testing. |
|
| Define separate configurations for development, testing, and production. |
|
| Make sure a service is running well by checking its status regularly. |
|
About the author
- "Hi, I'm Pratik Lahamge, an IT professional at HCL Tech with over 3 years of experience in application support and DevOps support. I specialize in streamlining operations, enhancing system reliability, and automating workflows to drive efficient IT processes. My skill set includes cloud technologies (AWS, Azure), CI/CD pipelines, containerization (Docker, Kubernetes), infrastructure as code, and monitoring tools like Grafana, ticketing tools, job monitoring, and deployment."
Comments
Join the discussionNo comments yet. Be the first to comment.


