Beginner's Guide To Docker: Learn Containerization Step-By-Step

Avishek SinghAvishek Singh
7 min read

What is Docker?

  • Docker is a tool that packages your application and its dependencies into a single unit, ready to ship and deploy on any platform, regardless of the operating system.

What is not Docker?

  • Docker is not a virtual machine. While both Docker containers and virtual machines provide isolated environments for running applications, they operate differently. Virtual machines include a full operating system along with the application and its dependencies, which makes them heavier and slower to start. In contrast, Docker containers share the host operating system's kernel and only include the application and its dependencies, making them lightweight and faster to start. Docker focuses on process-level isolation, whereas virtual machines provide full isolation with a separate OS for each instance.

Why Docker?

  • Problem: It works on my machine :)

    Let's say you are working on an application that requires:

    • Java 17, React 16, MongoDB 5.0, Kafka 3.X, and many more dependencies

You have installed all of these dependencies on your machine and are running the application successfully. However, when you deploy the application to a production environment on a Linux box, it fails because some dependencies are not compatible. So, you debug and identify the correct set of dependencies and reinstall the right ones. This entire process can take a couple of hours or sometimes even a few days. So, what's the solution?

  • Solution 1: You can create a virtual machine on your physical server, install all the required dependencies once, and then deploy your application multiple times. Installing the new dependencies is a one-time process, and you don't need to worry about any conflicting dependencies on your virtual machine since you control the installation.

  • Solution 2: You can package your application along with all the required dependencies into a single unit (image) and ship it to production or any other machine for deployment. That's it—no installation needed and no dependency conflicts..

AspectDockerVirtual Machine (VM)
ArchitectureContainer-based, shares host OS kernel.Hypervisor-based, includes entire guest OS.
Resource UsageLightweight, uses fewer resources (RAM, CPU).Heavy, requires more resources (RAM, CPU).
Startup TimeFast (seconds).Slower (minutes).
IsolationProcess-level isolation using containers.Full isolation with a separate OS per VM.
PerformanceNear-native performance due to minimal overhead.More overhead due to full OS virtualization.
StorageSmaller footprint, stores only required libraries and files.Larger footprint, includes full OS files and libraries.
PortabilityHighly portable, can run on any OS supporting Docker.Less portable, depends on the VM configuration.
Use CasesIdeal for microservices, CI/CD, and quick deployment.Suitable for running multiple OS environments or legacy applications.
ScalabilityEasily scalable for large applications.Less efficient scaling compared to containers.
SecurityDepends on shared kernel security.Stronger isolation due to separate OS, better for critical workloads.

Docker keywords:

Docker KeywordDescription
ImageA lightweight, standalone, and executable package that includes everything needed to run a piece of software (code, runtime, libraries, environment variables).
ContainerAn instance of a Docker image that runs as an isolated application with its own filesystem, processes, and network.
DockerfileA text file containing instructions to build a Docker image. It defines the steps to create an image layer by layer.
VolumeA mechanism to persist data generated by and used by Docker containers, even after the container stops.
Port MappingA technique to connect a port on your local machine (host) to a port in a Docker container, allowing external access.
TagA label assigned to a Docker image that specifies a version (e.g., nginx:latest). By default, :latest is used if no tag is provided.
RegistryA storage and content delivery system for Docker images. Docker Hub is a popular public registry, while private registries are also used.
NetworkA layer that allows Docker containers to communicate with each other or with external systems. Docker creates a default bridge network, but you can create custom networks.
LayerEach instruction in a Dockerfile creates a layer in the image. Layers are cached and reused, improving build performance.
BuildThe process of creating a Docker image from a Dockerfile. The resulting image can be run as a container.
PruneA command to clean up unused containers, images, volumes, and networks, freeing up disk space.
RepositoryA collection of related Docker images, often with different tags representing versions (e.g., nginx, mysql).
OrchestrationThe management of containerized applications at scale. Tools like Kubernetes and Docker Swarm handle this by deploying, managing, and scaling containers across multiple hosts.

Docker Commands

Check Docker Version

Verify the installed Docker version on your system.

docker --version

This command outputs the current version of Docker installed on your machine.

Pull an Image

Download a Docker image from a repository.

docker pull <image_name>

For example, docker pull nginx downloads the latest Nginx image from Docker Hub.

List Docker Images

Display all Docker images available on your system.

docker images

This command lists all the images you have downloaded, along with their tags and sizes.

Run a Container (detached, with port mapping)

Start a Docker container in the background with specified port mappings.

docker run -d -p <host_port>:<container_port> <image_name>

For example, docker run -d -p 80:80 nginx runs an Nginx container in detached mode, mapping port 80 of the host to port 80 of the container.

List Running Containers

Show all currently running Docker containers.

docker ps

This command lists all active containers, displaying their IDs, names, and other details.

List All Containers (including stopped)

List all Docker containers, including those that are stopped.

docker ps -a

This command shows all containers, whether they are running or stopped.

Stop a Running Container

Terminate a running Docker container.

docker stop <container_id>

Replace <container_id> with the actual ID of the container you want to stop.

Remove a Stopped Container

Delete a Docker container that is not running.

docker rm <container_id>

This command removes a container that has been stopped, freeing up system resources.

Access a Running Container’s Shell

Open a shell session inside a running Docker container.

docker exec -it <container_id> /bin/bash

This command allows you to interact with the container's file system and execute commands.

Build an Image from a Dockerfile

Create a Docker image from a specified Dockerfile.

docker build -t <image_name> <path_to_dockerfile>

For example, docker build -t myapp . builds an image named "myapp" from the Dockerfile in the current directory.

View Container Logs

Display the logs of a Docker container.

docker logs <container_id>

This command shows the standard output and error logs of a specified container.

Remove All Stopped Containers

Delete all Docker containers that are not running.

docker container prune

This command removes all stopped containers, helping to clean up your system.

Remove All Unused Images

Remove all Docker images that are not in use.

docker image prune

This command deletes all dangling images, freeing up disk space.

Clean Up All Unused Data

Free up space by removing all unused Docker data.

docker system prune

This command removes all stopped containers, unused networks, dangling images, and build cache.

Conclusion:

In conclusion, Docker is a powerful tool that simplifies the process of developing, shipping, and deploying applications by packaging them and their dependencies into lightweight, portable containers. Unlike traditional virtual machines, Docker containers share the host OS kernel, making them more efficient and faster to start. This guide has provided a comprehensive overview of Docker's key concepts, commands, and benefits, making it an essential resource for anyone looking to streamline their development workflow and ensure consistent application performance across different environments. By mastering Docker, you can significantly reduce deployment times, avoid dependency conflicts, and enhance the scalability and portability of your applications.

Suggested Reads:

  1. Docker Documentation

  2. Docker for Beginners: An Introduction

  3. Docker vs. Virtual Machines: Understanding the Differences

  4. Best Practices for Writing Dockerfiles

  5. Docker Networking: A Comprehensive Guide

  6. Kubernetes vs. Docker: Understanding the Key Differences

  7. Docker Security Best Practices

  8. Continuous Integration and Continuous Deployment with Docker

  9. Scaling Applications with Docker Swarm

  10. Introduction to Docker Compose

0
Subscribe to my newsletter

Read articles from Avishek Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Avishek Singh
Avishek Singh