Must Know Docker π³ Commands For Developers!
Mastering Docker: Basic Commands
Introduction
Docker has revolutionized the world of software development by making it easier to package, distribute, and run applications in a consistent environment. In this blog post, we will dive into Docker's fundamentals, explaining what it is and why it's essential for developers. We will also provide you with a guide to basic Docker commands that every developer should know!
What Is Docker?
Docker is a powerful platform for developing, shipping, and running applications. It revolutionized how software is packaged and deployed by introducing the concept of containers. A container is a standalone, lightweight, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools. Docker provides a standardized way to create, distribute, and deploy these containers, making it easier for developers to work in various environments and ensuring that applications run consistently across different systems.
In summary, Docker is a game-changer for modern application development, providing a consistent and efficient way to package, distribute, and run software in containers, making it easier to develop and manage complex applications across various environments.
Why Do We Need Docker?
Jokes Apart, here are some reasons why we need docker.
Consistency: Docker ensures that an application runs consistently across different environments, from a developer's laptop to production servers. This eliminates the "it works on my machine" problem and reduces compatibility issues.
Isolation: Containers provide a high degree of isolation, keeping applications and their dependencies separate. This isolation enhances security, as one container cannot directly interfere with another.
Portability: Docker containers are portable, meaning you can package an application and its dependencies into a container once, and it will run the same way in the system that supports Docker. This simplifies the deployment process and makes it easy to move applications between environments.
Scalability: Docker simplifies the process of scaling applications up or down, especially when used with orchestration tools like Kubernetes. This elasticity is crucial for modern, dynamic applications. The below image shows how vertical and horizontal scaling works.
Version Control: Docker allows you to version your containers and their dependencies. This means you can track changes over time.
Continuous Integration/Continuous Deployment (CI/CD): Docker Integrates seamlessly with CI/CD pipelines, making it easier to automate the testing, building and deployment of applications. The below image is the basic example of the ci/cd pipeline with docker.
Microservices: Docker is well-suited for microservices architecture, where applications are broken down into smaller, independently deployable services. Containers make it easier to manage and scale these microservices.
Key Docker Components:
Images:
In the context of Docker, a "Docker Image" is a lightweight, standalone, and executable package that contains everything needed to run a piece of software, including the code, runtime, libraries, and system tools. These images serve as the building blocks for containers, which are instances of images that can be run on a Docker-compatible platform.
A Docker image is essentially a snapshot of a file system that includes an application and all its dependencies. This encapsulation makes it easy to distribute and deploy applications consistently across different environments, from the developer's laptop to production servers.
Registries:
a "registry" is a centralized repository for storing and distributing docker images. The Docker registry plays a crucial role in managing and sharing Docker images among developers and across different environments. These registries provide a way to store, version and distribute Docker images, making it easy to collaborate on software projects and deploy applications consistently.
One example of a Registry is Docker Hub.
Setting Up Docker:
Before we dive into Docker commands, you need to set up Docker on your system. Follow the installation instructions for your platform:
To check your Docker version, open your terminal and run below command:
docker --version
Basic Docker Commands:
Pulling Docker Images:
To pull an image from the Docker Hub, use the
docker pull
command. For example, to pull a MongoDB imageBy default, the latest version of an image is pulled.
Make sure to choose the right image for your project.
Make sure you have Docker installed and running on your system before using these commands.
docker pull mongo // fetched latest version of mongodb docker pull mongo:6.4 // pulls 6.4 version of mongodb
Running a Container:
To create and start a Docker container, use the
docker run
command.This is the first step in running applications within containers.
For example, to start a MongoDB container in detached mode:
docker run -p 27017:27017 -d mongo
Manage Images Commands:
Some of the most common image management commands include:
Command | Explanation |
docker image ls | Lists all images |
docker image rm python | Removes image ( python in this case) |
docker history image | Displays the image history |
docker inspect image | Displays low-level information about an image |
Container Interaction Commands:
Interact with your Docker container with the following common commands:
Command | Explanation |
docker start container | Starts a new Container |
docker stop container | Stops a Container |
docker pause container | Pauses a Container |
docker restart container | Restarts a Container |
docker wait container | Blocks a Container |
docker create image | Creates a new container from an image |
Container Inspection Commands:
Sometimes, you need to inspect your containers for quality assurance or troubleshooting purposes. These commands help you get an overview of what different containers are doing:
Command | Explanation |
docker ps | Lists all running containers |
docker ps -a | Lists all containers including stopped containers |
docker diff container | Inspects changes to directories and files in the container filesystem |
docker inspect container | Displays low-level information about a container |
docker log container | Gathers the logs for a container |
docker stats container | Shows container resource usage statistics |
Clean Up Commands:
To keep your system clean and save disk space, itβs a great idea to clean up unused images, containers, and volumes. Check the commands below for more details:
Commands | Explanation |
docker image prune | Clears unused docker image |
docker image prune -a | Clears all images that are not being used by containers |
docker system prune | Removes all stopped containers, all networks not used by containers, all dangling images, and all build cache |
docker image rm image | Removes an image |
docker rm container | Removes running container |
docker rm $(docker ps -a -q) | Removes all stopped containers |
docker kill $ (docker ps -q) | Stops all running containers |
Conclusion
Understanding Docker and mastering these basic Docker commands is a vital skill for modern developers. With containerization becoming a standard practice, you'll find Docker invaluable for your projects. So, start exploring Docker and harness its power to simplify your development workflow.
Congratulations! ππ» You've mastered all the essential fundamentals of Docker π³.