Learn Docker Practically

Before we start learning Docker, we must understand what Docker does. Let’s check what problem it solves. As a developer, I am sure you might have come across one issue when you or one of your team members said “It works on my machine” when something went wrong. Do you know why this happens? There can be multiple reasons for this like library dependencies, environmental issues, software versions or even OS versions. Docker thus helps you solve these issues. Docker is a platform that enables developers to automate the deployment of applications inside lightweight, isolated portable containers. Docker Containers encapsulate an application and its dependencies, including libraries, binaries, and runtime, ensuring that the application runs consistently across various environments.

Why Use Docker?

  1. Consistency: Docker ensures that your application runs the same way in development, testing, and production environments, reducing the “it works on my machine” problem.
  2. Isolation: Containers provide process and filesystem isolation, allowing you to run multiple applications on the same host without interference.
  3. Portability: Docker containers can run on any system that supports Docker, making it easy to move applications between environments.
  4. Resource Efficiency: Containers share the host OS kernel, which means they are more lightweight than traditional virtual machines.

What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security lets you run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you don’t need to rely on what’s installed on the host. You can share containers while you work, and be sure that everyone you share with gets the same container that works in the same way. Docker consists of 3 main parts:

  1. Containers: Docker containers are lightweight, standalone, and executable packages that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
  2. Images: Docker images are the templates for containers. They contain the application code and its dependencies, ensuring consistency across different environments.
  3. Docker Daemon/Engine: The Docker engine is the core of Docker. It’s a lightweight runtime and tooling that manages containers.

Docker Architecture

Docker employs a client-server structure where the Docker client communicates with the Docker daemon. The daemon is responsible for the essential tasks of constructing, executing, and disseminating Docker containers. Both the Docker client and daemon can coexist on a single system, or you have the option to link a Docker client to a distant Docker daemon. Their interaction occurs through a REST API, utilizing either UNIX sockets or a network interface. Notably, Docker Compose serves as an additional Docker client, offering the capability to manage applications composed of a collection of containers.

Docker daemon : The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

Docker client: The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

Docker Desktop: Docker Desktop is an easy-to-install application for your Mac, Windows or Linux environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. For more information, see Docker Desktop.

Docker registries: A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker looks for images on Docker Hub by default. You can even run your own private registry. When you use the docker pull or docker run commands, Docker pulls the required images from your configured registry. When you use the docker push command, Docker pushes your image to your configured registry.

Docker objects: Docker uses different objects like images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

Difference between Docker & Virtual Machine

Containers and virtual machines have similar resource isolation and allocation benefits, but function differently because containers virtualize the operating system instead of hardware. Containers are more portable and efficient.

How to Use Docker?

To use docker, you must first install it and ensure that it works perfectly fine. Use the below steps to install and run your first container.

Install Docker:

Run Your First Container:

Open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and run the following command to download and run a simple container:

# Check if docker is working on your computer
$docker -v
Docker version 24.0.6, build ed223bc

# Run first container
docker run hello-world

This command downloads the “hello-world” image from Docker Hub and runs it in a container.

These are the basic steps to get started with Docker. Stay tuned for more articles to get a detailed practical understanding of Docker.

Leave a Comment

Share this