Learn Docker Beginner to Expert

Nivas DevSecOps
9 min readOct 4, 2024

--

Docker is an open-source software platform that enables developers to create, deploy, and manage applications in a wide variety of computing environments.

It provides a container-based virtualization system that allows developers to package their applications into isolated containers, which can then be deployed on any operating system or cloud platform.

With Docker, developers can quickly and easily create, test, and deploy applications without having to worry about compatibility issues or hardware requirements.

Docker Image: A Docker image is a lightweight, standalone, and executable package that contains everything needed to run an application, including the code, runtime environment, libraries, and system tools.

It provides a consistent and reproducible platform for deploying applications regardless of the underlying infrastructure or host operating system. Docker images are created from a base image by defining a set of instructions called a Dockerfile that specify how to build the image step by step.

Docker Container: A container is an isolated environment for your code. This means that a container has no knowledge of your operating system or your files.

It runs on the environment provided to you by Docker Desktop. This is why a container usually has everything that your code needs in order to run, down to a base operating system. You can use Docker Desktop to manage and explore your containers.

Dockerfile: Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Docker concepts

Docker is a platform for developers and sysadmins to build, share, and run applications with containers. The use of containers to deploy applications is called containerisation. Containers are not new, but their use for easily deploying applications is.

Containerisation is increasingly popular because containers are:

  • Flexible: Even the most complex applications can be containerised.
  • Lightweight: Containers leverage and share the host kernel, making them much more efficient in terms of system resources than virtual machines.
  • Portable: You can build locally, deploy to the cloud, and run anywhere.
  • Loosely coupled: Containers are highly self sufficient and encapsulated, allowing you to replace or upgrade one without disrupting others.
  • Scalable: You can increase and automatically distribute container replicas across a datacenter.
  • Secure: Containers apply aggressive constraints and isolations to processes without any configuration required on the part of the user.

Docker Compose: Docker Compose is a powerful and efficient tool used to define and manage multi-container Docker applications.

It allows developers to create an application stack, describing the services, networks, and volumes needed for the containers to run seamlessly together. With Docker Compose, the orchestration of complex architectures becomes much simpler as it automates the deployment process.

Docker Daemon: The background service running on the host that manages to build, run, and distribute Docker containers. The daemon is the process that runs in the operating system to which clients talk.

Docker Client: The command line tool that allows the user to interact with the daemon.

Docker Hub: A registry of Docker images. You can think of the registry as a directory of all available Docker images. Docker registries can use them for pushing/pulling images.

Containers and virtual machines

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs incur a lot of overhead beyond what is being consumed by your application logic.

lets me summarise the learning till now:

  • Virtual Machines are slow and take a lot of time to boot.
  • Containers are fast and boots quickly as it uses host operating system and shares the relevant libraries.
  • Containers do not waste or block host resources unlike virtual machines.
  • Containers have isolated libraries and binaries specific to the application they are running.
  • Containers are handled by Containerisation engine.
  • Docker is one of the containerisation platforms which can be used to create and run containers.

Why do we use docker?

So we have discussed what Docker is. However, what is the need for the Docker? Well, Docker containers are lightweight and they are super easy to create and deploy.

Docker provides us with containers. And containerization consists of an entire runtime environment, an application, all its dependencies, libraries, binaries and configuration files needed to run it, bundled into one package. Each application runs separately from the other. Docker solves the dependency problem by keeping the dependency contained inside the containers. It unites developers against dependency of their project.

Benefits of using Containers over Virtual Machines

Now let’s discuss what is the benefit of Docker over VMs.

  • Unlike VMs( Virtual Machines ) that run on a Guest OS, using a hypervisor, Docker containers run directly on a host server (for Linux), using a Docker engine, making it faster and lightweight.
  • Docker containers can be easily integrated compared to VMs.
  • With a fully virtualized system, you get more isolation. However, it requires more resources. With Docker, you get less isolation. However, as it requires fewer resources, you can run thousands of container on a host.
  • A VM can take a minimum of one minute to start, while a Docker container usually starts in a fraction of seconds.
  • Containers are easier to break out of than a Virtual Machine.
  • Unlike VMs there is no need to preallocate the RAM. Hence docker containers utilize less RAM compared to VMs. So only the amount of RAM that is required is used.

How does Docker work?

Since we now understand the benefits of using Docker. Let’s talk above the functioning of Docker. Docker has a docker engine, which is the heart of Docker system. It is a client-server application. It has three main components:

  • A server which is a type of long-running process called a daemon process.
  • A client which is Docker CLI( Command Line Interface), and
  • A REST API which is used to communicate between the client( Docker CLI ) and the server ( Docker Daemon )

The Docker daemon receives the command from the client and manages Docker objects, such as images, containers, networks, and volumes. The Docker client and daemon can either run on the same system, or you can connect a Docker client to a remote Docker daemon. They can communicate using a REST API, over UNIX sockets or a network interface.

In Linux, Docker host runs docker daemon and docker client can be accessed from the terminal.

In Windows/OS X, there is an additional tool called Docker toolbox. This toolbox installs the docker environment on Win/OS system. This toolbox installs the following: Docker Client, Compose, Kitematic, Machine, and Virtual Box

Technology Used in Docker

The programming language used in Docker is GO. Docker takes advantage of various features of Linux kernel like namespaces and cgroups.

namespaces: Docker uses namespaces to provide isolated workspace called containers. When a container is run, docker creates a set of namespaces for it, providing a layer of isolation. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.

cgroups( control groups ): croups are used to limit and isolate the resource usage( CPU, memory, Disk I/O, network etc ) of a collection of processes. cgroups allow Docker engine to share the available hardware resources to containers and optionally enforce limit and constraints.

UnionFS( Union file systems ): are file systems that operate by creating layers, making them very lightweight and fast.It is used by Docker engine to provide the building blocks for containers.

Docker Engine combines the namespaces, cgroups, and UnionFS into a wrapper called a container format. The default container format is libcontainer.

Docker Commands

docker run — run a container from an image

docker pull — pull an image from a registry

docker push — push an image to a registry

docker build — build an image from a Dockerfile

docker ps — list running containers

docker stop — stop a running container

docker start — start a stopped container

docker restart — restart a container

docker logs — show the logs of a container

docker exec — execute a command inside a running container

docker images — list available images

docker rm — remove a container

docker rmi — remove an image

docker inspect — show information about a container

docker network create — create a network for containers to communicate

docker network connect — connect a container to a network

docker network disconnect — disconnect a container from a network

docker port — show the mapped ports of a container

docker cp — copy files between a container and the host

docker commit — create a new image from a container’s changes

docker login — log in to a registry

docker logout — log out of a registry

docker tag — tag an image with a new name

docker export — export the contents of a container as a tar archive

docker import — create a new image from a tar archive

docker save — save an image as a tar archive

docker load — load an image from a tar archive

docker top — show the processes running inside a container

docker stats — show resource usage statistics of containers

docker diff — show the changes made to a container’s filesystem

docker events — show the events generated by Docker

docker history — show the history of an image

docker pause — pause a running container

docker unpause — unpause a paused container

docker kill — send a signal to a container to stop it abruptly

docker wait — wait for a container to exit and return its exit code

docker attach — attach to a running container’s console

docker buildx — build and push multi-platform images

docker compose — manage multi-container applications with Docker Compose

docker swarm — create and manage a cluster of Docker nodes

docker volume create — create a named volume for persistent data storage

docker volume ls — list available volumes

docker volume rm — remove a named volume

docker system prune — remove all unused objects from Docker

docker system df — show the usage of Docker objects

docker system events — show the events generated by Docker on the system

docker system info — show the system-wide information about Docker

docker system inspect — show detailed information about Docker objects

docker system logs — show the system logs of Docker

docker system version — show the version of Docker installed on the system

Most commonly used tags in a Dockerfile:

  • FROM: Specifies the base image to use for the image being built.
  • RUN: Executes a command during the build process.
  • CMD: Specifies the default command to run when the container is started.
  • ENV: Sets an environment variable in the container.
  • COPY: This copies files or directories from the host machine only into the container.
  • ADD : Add is similar to copy with addition it allows A URL instead of a local file/directory & extracts tar from the source directory into the destination.
  • EXPOSE: Exposes a specific port or ports to be used by the container.
  • LABEL: Adds metadata to the image in the form of key-value pairs.
  • USER: Specifies the user to use when running the container.
  • WORKDIR: As the name says, it sets the working directory for the container.

Hope it helps..!

Thank you for reading so far! Before you go:

--

--

Nivas DevSecOps
Nivas DevSecOps

Written by Nivas DevSecOps

Cloud | DevSecOps| AWS ⭐Passionate Cloud and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.

No responses yet