Docker Made Easy: From Beginner to Advanced in One Guide

Utsav Desai
13 min readMar 23, 2023

--

What is Docker?

Docker is a popular open-source platform that allows developers to build, deploy, and run applications in containers. Containers are lightweight, standalone executable packages that contain everything needed to run an application, including the code, runtime, system tools, libraries, and settings. Containers provide a consistent and predictable environment for applications to run, regardless of the underlying infrastructure.

Docker simplifies the process of creating and managing containers by providing a standard format for packaging applications and their dependencies. Docker allows developers to package their applications and deploy them on any system that supports Docker, including local machines, data centers, and cloud platforms.

Docker also provides a range of tools for managing containers, including tools for building images, managing networks, and orchestrating container deployments across multiple hosts. These tools make it easier for developers and operations teams to manage and scale their applications, reducing the time and effort required to deploy and maintain applications.

Why We Need Docker?

Docker is a popular tool used for containerization, which provides a number of benefits for developers, IT professionals, and organizations. Here are some reasons why Docker is commonly used:

  1. Consistent environment: Docker allows you to create a consistent environment for your application, which can help eliminate issues related to differences in the underlying infrastructure, dependencies, or configurations. This can make it easier to build, test, and deploy your application.
  2. Faster development: Docker enables developers to quickly spin up containers that contain all the necessary dependencies, which can save time and make the development process more efficient.
  3. Portability: Docker containers can run on any machine that has Docker installed, regardless of the underlying infrastructure. This makes it easier to move applications between environments and reduces the risk of compatibility issues.
  4. Scalability: Docker makes it easy to scale applications horizontally, by spinning up multiple containers to handle increased load.
  5. Resource efficiency: Docker containers are lightweight and share the host machine’s resources, which can reduce overall resource usage and improve efficiency.
  6. Version control: Docker allows you to version control your images, making it easier to roll back to a previous version if necessary.
  7. Security: Docker provides a number of security features, such as isolation and resource constraints, which can help protect your applications from security vulnerabilities.

Overall, Docker provides a number of benefits that can help developers and organizations create more efficient, portable, and scalable applications.

What is Container?

A container is a lightweight, standalone executable package that contains everything needed to run an application, including the code, runtime, system tools, libraries, and settings. Containers provide a consistent and predictable environment for applications to run, regardless of the underlying infrastructure.

Containers are similar to virtual machines (VMs), but they are much lighter weight and faster to start up. While VMs include a full operating system and virtual hardware, containers share the host operating system and only include the minimum components needed to run the application.

Containers are created from images, which are pre-built templates that contain all the necessary components for a specific application or service. Images can be shared and reused, allowing developers to easily deploy applications on any system that supports containers.

Containers have become popular in software development and operations because they provide a way to isolate applications and their dependencies, making it easier to deploy and manage them in a consistent and repeatable way. Containers also enable developers to build microservices architectures, where applications are broken down into small, independently deployable components.

What is Docker Image?

In the context of Docker and containerization, an image is a pre-built template that contains all the necessary components to run an application or service in a container. An image includes the application’s code, runtime, system tools, libraries, and settings, as well as any other dependencies required for the application to run.

Images are built from a set of instructions written in a Dockerfile, which specifies the application’s dependencies, environment, and other settings. Once an image is built, it can be shared and reused across multiple environments, making it easier to deploy and manage applications consistently.

Images are stored in a registry, such as Docker Hub or a private registry, and can be pulled and run on any system that supports Docker. Images can also be versioned, allowing developers to track changes and roll back to previous versions if necessary.

When a container is started from an image, it creates a running instance of the application in a container, which can be managed and scaled independently of other containers running on the same system. Images and containers are the key building blocks of Docker and containerization, enabling developers to easily package and deploy applications across a variety of environments.

Brief Overview of Docker Engine

Docker Engine is the core technology behind Docker that enables the containerization of applications. It is an open-source container runtime that provides a platform for developing, packaging, and running containerized applications.

The Docker Engine consists of three main components:

  1. Docker daemon: The Docker daemon is a background process that manages the lifecycle of containers. It is responsible for building, running, and stopping containers. It also communicates with the Docker client to respond to commands and monitor the status of containers.
  2. Docker API: The Docker API provides a set of RESTful APIs that allow developers to interact with the Docker daemon. The Docker API is used by the Docker client, as well as by other tools and services that integrate with Docker.
  3. Docker client: The Docker client is a command-line tool that allows developers to interact with the Docker daemon. It provides a set of commands that can be used to build, run, and manage Docker containers.

The Docker Engine is designed to be platform-agnostic and can run on a variety of operating systems and cloud providers. It provides a number of benefits, including increased portability, faster development cycles, and improved resource efficiency. The Docker Engine also includes a number of features for managing containers, such as network and storage management, as well as security and isolation capabilities. Overall, the Docker Engine is a powerful tool for containerization that is widely used by developers and IT professionals around the world.

VM vs Containers

Containers and virtual machines (VMs) are both technologies used for isolating and running applications, but they work in different ways and serve different purposes.

A virtual machine simulates a complete hardware environment, including a virtual CPU, memory, storage, and network interface, allowing multiple operating systems and applications to run on a single physical host. Each VM runs its own guest operating system, which is completely isolated from the host system and other VMs. VMs are typically used to run multiple applications or services on a single physical server, or to create sandboxes for testing and development.

A container, on the other hand, shares the host operating system and only includes the minimum components needed to run the application. Containers provide a lightweight and efficient way to package and deploy applications, and they can be easily moved between different environments, such as from a developer’s laptop to a production server. Containers are typically used to run a single application or service, and they are often used in the context of microservices architecture.

Compared to VMs, containers are faster to start up, use less memory, and are more efficient in terms of resource utilization. However, because containers share the host operating system, they may not provide the same level of isolation and security as VMs. Therefore, it is important to carefully consider the requirements of each application and choose the appropriate technology for the job.

Benefits of using Containers over Virtual Machines

There are several benefits to using containers over virtual machines (VMs), including:

  1. Resource efficiency: Containers share the host operating system, which means they require less memory and CPU resources than VMs. This makes it possible to run more containers on a single physical host than VMs.
  2. Faster startup time: Containers can be started in seconds, while VMs can take minutes to start up. This makes it easier to scale applications up and down quickly to meet changing demand.
  3. Smaller footprint: Containers are smaller than VMs, which makes them faster to deploy and easier to move between environments. This can be particularly useful in cloud environments, where applications need to be deployed and redeployed frequently.
  4. Greater portability: Because containers include everything needed to run an application, they can be easily moved between different environments, such as from a developer’s laptop to a production server. This makes it easier to ensure consistency and reliability across different stages of the development and deployment pipeline.
  5. Better resource utilization: Containers can be more efficient in terms of resource utilization, because they can be dynamically allocated and deallocated based on demand. This makes it possible to achieve higher levels of resource utilization and reduce infrastructure costs.

Overall, containers are a powerful tool for developers and operations teams looking to deploy applications quickly and efficiently, and to scale them up and down as needed to meet changing demand. While there are some cases where VMs may be a better choice, containers are generally a more efficient and flexible technology for modern application development and deployment.

Technology Used in Docker

Docker is built on several core technologies, including:

  1. Containerization: Docker uses containerization technology to package applications and their dependencies into a single unit that can be easily moved and run in any environment that supports Docker.
  2. Linux Kernel: Docker is built on top of the Linux operating system and leverages several kernel features, such as cgroups and namespaces, to provide container isolation and resource management.
  3. Docker Engine: Docker Engine is the core runtime that powers Docker. It provides the ability to build, run, and manage Docker containers, and includes several key components, such as the Docker daemon, Docker API, and Docker CLI.
  4. Docker Hub: Docker Hub is a public registry that provides access to a large library of Docker images that can be used to quickly deploy and run applications. Docker Hub also supports private repositories, making it easy to share and manage Docker images within organizations.
  5. Docker Compose: Docker Compose is a tool that allows developers to define and run multi-container Docker applications. It makes it easy to configure and orchestrate multiple Docker containers, allowing developers to define complex applications using a simple YAML file.
  6. Docker Swarm: Docker Swarm is a container orchestration platform that allows developers to manage and scale large numbers of Docker containers across multiple hosts. It provides features such as load balancing, service discovery, and automatic scaling, making it easier to manage complex containerized applications.

Overall, Docker is built on a powerful combination of containerization technology, Linux kernel features, and a suite of tools and platforms that make it easy to build, deploy, and manage containerized applications at scale.

Useful Docker Commands

Here are some common Docker commands, along with a brief explanation of what they do and an example of how to use them:

-> docker run: This command is used to run a Docker container. It takes an image name as an argument and can be used to set various options such as port mappings, environment variables, and volumes.

Example: docker run -p 8080:80 nginx will run an Nginx web server container and map port 8080 on the host to port 80 on the container.

-> docker ps: This command is used to list running Docker containers. By default, it will show only the containers that are currently running.

Example: docker ps will show a list of running containers.

-> docker images: This command is used to list the Docker images that are currently available on the host.

Example: docker images will show a list of available images.

-> docker build: This command is used to build a Docker image from a Dockerfile, which is a text file that contains instructions for building the image.

Example: docker build -t my-image:latest . will build an image called "my-image" using the Dockerfile in the current directory.

-> docker push: This command is used to push a Docker image to a Docker registry, such as Docker Hub or a private registry.

Example: docker push my-registry/my-image:latest will push the "my-image" image to the "my-registry" registry.

-> docker pull: This command is used to pull a Docker image from a Docker registry.

Example: docker pull nginx will pull the latest version of the Nginx image from Docker Hub.

-> docker ps -a: This command lists all containers, including stopped ones.

Example: docker ps -a will show a list of all containers, including those that have stopped.

-> docker exec: This command runs a command inside a running container.

Example: docker exec my-container ls -l will run the ls -l command inside the my-container container.

-> docker stop: This command stops a running container.

Example: docker stop my-container will stop the my-container container.

-> docker kill: This command sends a SIGKILL signal to a running container, forcing it to stop immediately.

Example: docker kill my-container will immediately stop the my-container container.

-> docker commit: This command creates a new image from a container's changes.

Example: docker commit my-container my-image will create a new image called my-image based on the changes made to the my-container container.

-> docker login: This command logs in to a Docker registry.

Example: docker login my-registry.com will log in to the my-registry.com registry.

-> docker images: This command lists all Docker images on the host.

Example: docker images will show a list of all Docker images on the host.

-> docker rm: This command removes a container.

Example: docker rm my-container will remove the my-container container.

-> docker rmi: This command removes an image.

Example: docker rmi my-image will remove the my-image image.

These are just a few of the many Docker commands available. For more information, you can refer to the Docker documentation or run docker --help to see a list of available commands and options.

Creating First Docker Application

here is a step-by-step guide to creating your first Docker application:

1. Choose your application: Decide which application you want to containerize. It could be a simple “Hello World” application or a more complex web application.

2. Write a Dockerfile: A Dockerfile is a script that contains instructions on how to build a Docker image. You’ll need to write a Dockerfile for your application. Here is an example Dockerfile for a simple “Hello World” application:

FROM alpine:latest
LABEL maintainer="Your Name <your.email@example.com>"
CMD echo "Hello World"

This Dockerfile starts with the latest version of the Alpine Linux distribution, adds a label with the maintainer’s name and email, and sets the command to print “Hello World”.

3. Build the Docker image: Use the docker build command to build the Docker image from the Dockerfile.

docker build -t my-first-docker-app .

This command builds the Docker image with the tag my-first-docker-app.

4. Run the Docker container: Use the docker run command to run the Docker container from the image you just built.

docker run my-first-docker-app

This command runs the Docker container from the my-first-docker-app image and should output "Hello World" to the console.

Docker-Compose

Docker Compose is a tool that allows you to define and run multi-container Docker applications. With Docker Compose, you can use a YAML file to configure the services that make up your application and start them all with a single command. This makes it easier to manage and deploy complex applications that have multiple dependencies.

Here is an example of a Docker Compose file that defines a web application and a database:

version: '3'
services:
web:
build: .
ports:
- "5000:5000"
db:
image: postgres
environment:
POSTGRES_PASSWORD: example

In this example, we have two services: web and db. The web service is defined to build an image from the Dockerfile in the current directory, and expose port 5000. The db service is defined to use the official Postgres image and set the POSTGRES_PASSWORD environment variable to "example".

To start these services, we can use the following command:

docker-compose up

This will build the web service image (if it doesn’t exist) and start both the web and db services.

If we want to stop the services, we can use the following command:

docker-compose down

This will stop and remove the containers created by the up command.

With Docker Compose, you can also use other features like specifying dependencies, scaling services, and managing environment variables. Overall, Docker Compose is a powerful tool that simplifies the management and deployment of multi-container applications.

Docker Compose YAML file that demonstrates some additional features:

version: '3'

services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
depends_on:
- db
networks:
- webnet
environment:
FLASK_APP: app.py
FLASK_ENV: development
volumes:
- .:/code
deploy:
resources:
limits:
cpus: '0.5'
memory: '512M'
reservations:
cpus: '0.2'
memory: '256M'


db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
POSTGRES_USER: user
POSTGRES_DB: mydatabase
volumes:
- db-data:/var/lib/postgresql/data
networks:
- webnet


volumes:
db-data:

networks:
webnet:

In this example, we have two services: web and db. . The web service is defined to build an image from the Dockerfile in the current directory, expose port 5000, set some environment variables, specify a dependency on the db service, define a network called webnet, mount the current directory to the /app directory in the container, and set resource constraints for CPU and memory usage. The db service is defined to use the official Postgres image, set some environment variables, create a named volume to store the database data, and join the webnet network.

To start these services, we can use the following command:

docker-compose up

This will build the web service image (if it doesn’t exist) and start both the web and db services.

If we want to scale the web service, we can use the following command:

docker-compose up --scale web=3

This will start three instances of the web service.

If we want to stop the services, we can use the following command:

docker-compose down

This will stop and remove the containers created by the up command.

--

--

Utsav Desai

Utsav Desai is a technology enthusiast with an interest in DevOps, App Development, and Web Development.