Must Know Docker 🐳 Commands For Developers!

Mastering Docker: Basic Commands

Β·

6 min read

Must Know Docker 🐳 Commands For Developers!

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. Version Control: Docker allows you to version your containers and their dependencies. This means you can track changes over time.

  6. 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.

  7. 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 image

  • By 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:

CommandExplanation
docker image lsLists all images
docker image rm pythonRemoves image ( python in this case)
docker history imageDisplays the image history
docker inspect imageDisplays low-level information about an image

Container Interaction Commands:

Interact with your Docker container with the following common commands:

CommandExplanation
docker start containerStarts a new Container
docker stop containerStops a Container
docker pause containerPauses a Container
docker restart containerRestarts a Container
docker wait containerBlocks a Container
docker create imageCreates 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:

CommandExplanation
docker psLists all running containers
docker ps -aLists all containers including stopped containers
docker diff containerInspects changes to directories and files in the container filesystem
docker inspect containerDisplays low-level information about a container
docker log containerGathers the logs for a container
docker stats containerShows 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:

CommandsExplanation
docker image pruneClears unused docker image
docker image prune -aClears all images that are not being used by containers
docker system pruneRemoves all stopped containers, all networks not used by containers, all dangling images, and all build cache
docker image rm imageRemoves an image
docker rm containerRemoves 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 🐳.

Keep Learning πŸ‘¨πŸ»β€πŸ’» and stay enthusiastic!!

Β