Docker: A Good to-Have Arsenal for Modern Development and Deployment

In the ever-evolving landscape of software development, Docker has emerged as an indispensable tool that streamlines the process of building, shipping, and running applications. Whether you are a developer, sysadmin, or part of a DevOps team, Docker offers a multitude of benefits that can significantly enhance your productivity and efficiency.
What is Docker?
Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers include everything an application needs to run, such as libraries, system tools, code, and runtime. This ensures that the application runs consistently regardless of the environment.
Why Use Docker?
1. Portability
Docker containers encapsulate an application and its dependencies, ensuring that it runs consistently across different environments. This eliminates the classic “works on my machine” problem and simplifies the deployment process.
2. Isolation
Each Docker container runs in isolation, using its own resources and environment variables. This ensures that applications do not interfere with each other, leading to a more secure and stable environment.
3. Efficiency
Containers are lightweight because they share the host OS kernel, unlike virtual machines that require separate operating systems. This results in lower overhead and better utilization of system resources.
4. Scalability
Docker makes it easy to scale applications horizontally by spinning up multiple container instances. This is particularly useful for load balancing and high-availability setups.
5. Simplified Development Workflow
Docker streamlines the development workflow by providing consistent development, testing, and production environments. This speeds up the development cycle and reduces the time needed to debug environment-specific issues.
Good To Know Docker Commands
1. docker version
Understanding Docker — version:
The docker --version
command is a simple yet crucial tool for verifying your Docker installation. When executed, it returns the current version of Docker installed on your system. This is particularly useful for troubleshooting and ensuring compatibility with Docker containers and images that may require specific Docker versions. For instance, certain features and bug fixes are only available in newer releases. By keeping track of your Docker version, you can make informed decisions about upgrades and manage dependencies effectively.
2. docker images
Managing Docker Images with Docker images:
The docker images
command lists all the Docker images stored locally on your machine. Each image represents a template used to create Docker containers, encapsulating all the necessary binaries, libraries, and dependencies. This command displays essential information such as repository name, tag, image ID, creation date, and size. It helps in managing your images efficiently, identifying which ones are no longer needed, and freeing up space. For example, after running multiple containers from various images, you can quickly identify and remove outdated or redundant images to maintain a clean and optimized Docker environment.
3. docker run
Running Containers with docker run:
The docker run
command is the cornerstone of Docker operations, used to create and start a new container from a specified image. With an array of options, you can tailor the container's runtime behaviour, including interactive terminal sessions (-it
), background running (-d
), and port mapping (-p
). For instance, docker run -it ubuntu
launches an interactive terminal in an Ubuntu container. This command's flexibility allows you to configure resource limits, environment variables, and volume mounts, enabling seamless deployment and scaling of applications in isolated environments.
4. docker ps
Monitoring Containers with docker ps:
The docker ps
command provides a snapshot of currently running containers, displaying critical information such as container IDs, image names, command executed, creation time, status, ports, and names. By default, it lists only active containers, but using docker ps -a
will show all containers, including those that are stopped. This command is indispensable for monitoring container states, identifying issues, and managing container lifecycles. For example, if a container isn't behaving as expected, docker ps
can quickly help you identify the problematic container and take corrective actions.
5. docker stop
Stopping Containers Gracefully with docker stop:
The docker stop
command is used to gracefully halt a running container. It sends a SIGTERM signal, allowing the container to complete its current operations and shut down cleanly. If the container does not stop within a specified timeout period, it sends a SIGKILL signal to force the stop. This command is crucial for maintaining data integrity and ensuring that applications can release resources and perform cleanup tasks before shutting down. For instance, stopping a database container gracefully ensures that all pending transactions are properly committed.
6. docker start
Starting Containers with docker start: The docker start
command is used to start a previously stopped container. Unlike docker run
, which creates and starts a new container, docker start
restarts an existing one, preserving its previous state and configurations. This is particularly useful for quickly resuming operations after maintenance or updates without having to reconfigure the container. For example, after stopping a web server container to update its configuration, you can use docker start <container_id>
to bring it back online swiftly.
7. docker rm
Removing Containers with docker rm: The docker rm
command is used to delete one or more stopped containers. This helps in keeping your Docker environment clean and free from unused containers that can consume disk space. It’s important to note that you cannot remove a running container with this command; you need to stop it first by using docker stop
. For instance, after testing a development build in a container and stopping it, you can remove the container to avoid clutter and reclaim resources using docker rm <container_id>
.
8. docker rmi
Managing Images with docker rmi: The docker rmi
command is used to delete one or more Docker images from your local storage. This is crucial for managing disk space and removing outdated or unused images. When you run docker rmi <image_id>
, Docker deletes the image if no containers are using it. For example, after upgrading your application and creating a new image, you can remove the old image to prevent confusion and save storage. This command helps maintain an organized and efficient Docker environment.
9. docker exec
Executing Commands Inside Containers with docker exec: The docker exec
command allows you to run commands inside an already running container. This is useful for debugging, inspecting, or modifying the container’s state without stopping it. For instance, docker exec -it <container_id> bash
opens an interactive terminal inside the container, allowing you to navigate the file system, install packages, or check logs. This command provides a powerful way to interact with containers dynamically and perform necessary maintenance tasks.
10. docker logs
Fetching Container Logs with docker logs: The docker logs
command retrieves the logs of a specific container, providing insight into its behaviour and helping with debugging. By default, it shows the standard output (stdout) and standard error (stderr) streams of the container. You can use options like -f
to follow the logs in real time or --tail
to view the last few lines. For instance, docker logs -f <container_id>
allows you to monitor a web server's logs live, helping diagnose issues and verify that the container is functioning correctly.
11. docker build
Building Images with docker build: The docker build
command creates a Docker image from a Dockerfile and a context. The context is the set of files located in the specified PATH or URL. This command reads the Dockerfile, processes each instruction, and generates a new image. For example, docker build -t myapp:latest .
builds an image tagged as myapp:latest
from the Dockerfile in the current directory. This command is fundamental for creating consistent, reproducible environments, and encapsulating your application and its dependencies.
12. docker tag
Tagging Images with docker tag: The docker tag
command creates a new tag for an existing image, allowing you to reference the same image with different names or versions. This is useful for managing image versions and repository organization. For instance, docker tag myapp:latest myrepo/myapp:1.0
tags the myapp:latest
image as myrepo/myapp:1.0
, making it easier to push to a remote repository or distinguish between different versions of the same image. Proper tagging helps maintain a clear versioning system and streamline CI/CD pipelines.
13. docker push
Pushing Images to a Registry with docker push: The docker push
command uploads a locally tagged image to a Docker registry, such as Docker Hub or a private registry. This enables you to share images with others or deploy them on different machines. For example, docker push myrepo/myapp:1.0
pushes the myapp:1.0
image to the myrepo
repository on Docker Hub. Using docker push
facilitates the distribution of your application images across different environments, ensuring consistency and reliability in deployments.
14. docker inspect
Inspecting Docker Objects with Docker Inspect: The docker inspect
command provides detailed information about Docker objects, such as containers, images, volumes, and networks. It returns JSON-formatted data, including configuration settings, state, and resource usage. For example, docker inspect <container_id>
gives you comprehensive details about a specific container, aiding in troubleshooting and verifying configurations. This command is invaluable for gaining deep insights into your Docker environment and ensuring everything is configured as expected.
15. docker network ls
Listing Docker Networks with docker network ls: The docker network ls
command displays all Docker networks available on your system, including bridge, host, and overlay networks. This helps you manage and troubleshoot network configurations, ensuring that containers can communicate as intended. For example, running docker network ls
shows a list of networks with their names, IDs, drivers, and scopes. Understanding and managing networks is crucial for setting up multi-container applications and ensuring secure, isolated communication channels.
16. docker volume ls
Managing Volumes with docker volume ls: The docker volume ls
command lists all Docker volumes on your system. Volumes are used for persistent storage, allowing data to persist even after a container is removed. This command helps you identify and manage volumes, ensuring that your data is stored correctly and can be shared between containers. For example, running docker volume ls
displays all volumes, making it easier to find and clean up unused volumes, thereby optimizing your storage management.
17. docker commit
Creating Images from Containers with docker commit: The docker commit
command creates a new image from an existing container's changes. This is useful for saving the state of a container as an image, which can then be reused or shared. For example, if you have a running container with updates or new configurations, you can run docker commit <container_id> mynewimage
to create a new image reflecting those changes. This command is handy for capturing a container's state for future deployments or for creating a baseline image during development and testing.
18. docker update
The docker update
command is a powerful tool for dynamically adjusting the resources and configuration of running containers without needing to stop them. This flexibility is crucial for optimizing performance, managing resource allocation, and improving the stability of your containerized applications. By modifying parameters such as CPU and memory limits on the fly, docker update
allows you to respond to changing demands and workloads efficiently, ensuring your containers have the resources they need to operate smoothly and effectively.
docker update — memory 512m <container_id>
19. docker system prune
The docker system prune
command is an essential tool for maintaining a clean and efficient Docker environment by removing all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes. Over time, these unused objects can consume significant disk space and resources, leading to a cluttered and inefficient setup. By running docker system prune
, you can quickly free up space and optimize your Docker environment, ensuring it remains lean and manageable. This command is particularly valuable for developers and sysadmins who want to keep their systems running smoothly without manually tracking down and deleting each unused component.
20. docker save
The docker save
command is used to export Docker images into a tar archive file, making it a vital tool for managing image backups and distribution. By saving Docker images to tar files, users can ensure portability across different environments and facilitate offline transfers. This command simplifies tasks such as moving images between development, testing, and production environments, as well as sharing them with collaborators who may not have direct access to Docker registries. Overall, docker save
enhances flexibility and reliability in Docker image management, supporting efficient workflows and backup strategies.
docker save -o myapp.tar myapp:latest
21. docker load
The docker load
command is used to import Docker images from tar archive files into a Docker environment. This command is indispensable for deploying and managing Docker images across different systems and environments. By loading images from tar files created with docker save
, users can quickly restore backups, share images offline, or distribute applications without relying on direct internet access to Docker registries. It simplifies the process of transferring Docker images between development, testing, and production environments, ensuring seamless deployment and operational continuity. Overall, docker load
plays a crucial role in maintaining image portability and facilitating efficient Docker workflows.
docker load < myapp.tar
22. docker restart
The docker restart
command is used to gracefully restart one or more running Docker containers. It allows containers to be refreshed without stopping their execution entirely, which is useful for applying configuration changes or updates to the underlying applications. By using docker restart
, you can ensure that containers maintain their state and configurations while seamlessly incorporating changes. This command helps minimize downtime and interruptions, making it an essential tool for maintaining the availability and performance of Dockerized applications in production environments.
docker restart abc123
23. docker stats
The docker stats
command provides real-time insights into the resource usage of running Docker containers. By running docker stats
, you can monitor essential metrics such as CPU usage, memory consumption, network I/O, and block I/O of each container. This command is invaluable for performance monitoring, capacity planning, and troubleshooting in Docker environments. It displays a continuously updating stream of data, allowing users to identify resource bottlenecks, optimize container configurations, and ensure efficient resource allocation. Overall, docker stats
is a critical tool for maintaining the health and stability of Dockerized applications by providing visibility into their resource utilization in real-time.
docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a8d65c28c29f webapp 0.50% 32MiB / 1GiB 3.20% 280kB / 12kB 0B / 0B 2
eb5a6e74b99d database 1.20% 256MiB / 2GiB 12.80% 1.2MB / 45kB 0B / 0B 5
f6e49f3d2a1e cache 0.10% 64MiB / 1GiB 6.40% 800kB / 2MB 0B / 0B 1
24. docker kill
The docker kill
command forcibly terminates running Docker containers by sending a SIGKILL
signal, effectively stopping all processes within the container immediately. Unlike docker stop
, which initiates a graceful shutdown (SIGTERM
followed by SIGKILL
if necessary), docker kill
bypasses any cleanup or shutdown procedures, making it a swift but abrupt method to stop unresponsive or problematic containers. This command is typically used in situations where immediate termination is necessary, such as during troubleshooting or when normal stopping methods fail to work.
25. docker top
The docker top
command provides a concise view of the running processes within a Docker container, similar to how the top
command works in Linux. It lists the processes along with their associated process IDs (PID), user, cumulative CPU time, and the command being executed. This command is useful for real-time monitoring and troubleshooting within Docker containers, allowing administrators and developers to quickly identify active processes, analyze resource utilization, and diagnose any performance issues. It provides essential visibility into the container's inner workings, aiding in the efficient management and optimization of Dockerized applications.
docker top <container-id>
docker top my-nginx
PID USER TIME COMMAND
1 root 0:00 nginx: master process nginx -g daemon off;
7 nginx 0:00 nginx: worker process
26. docker cp
The docker cp
command is used to copy files or directories between a Docker container and the local filesystem. This command facilitates the transfer of data between the host machine and running containers, or between containers themselves.
Example:
To copy a file named example.txt
from a Docker container with an ID abc123
to the current directory on the host machine, you would use:
docker cp abc123:/path/to/example.txt ./example.txt
Conversely, to copy a file from the local filesystem into a Docker container, you would use:
docker cp ./localfile.txt abc123:/path/inside/container/
In summary, Docker commands form a comprehensive toolkit essential for modern containerized application development and deployment. From foundational commands like docker build
for creating images to docker run
for launching containers, Docker simplifies the process of managing and scaling applications across different environments. docker-compose
enhances orchestration capabilities, allowing multiple services to be defined and managed with ease. Monitoring container performance is made accessible with docker stats
, while troubleshooting and debugging are facilitated by docker logs
and docker exec
. docker cp
enables seamless file transfer between containers and the host system, supporting efficient data management. Overall, Docker commands empower developers and operations teams to build, deploy, and maintain applications effectively, fostering agility and reliability in software development lifecycles.
Rohit Kumar is a passionate software evangelist. Who loves implementing, breaking and engineering software products. He actively engages on platforms such as LinkedIn, GitHub, & Medium through email.