Easy Steps to Master Docker: A Complete Guide

Shashank VimalShashank Vimal
6 min read

What is a container ?

  1. Container: A container is a standardized software unit that bundles code and all its dependencies, ensuring the application runs smoothly and consistently across different computing environments.

  2. Docker Container Image: A Docker container image is a compact, self-sufficient, executable software package that contains everything necessary to run an application, including the code, runtime, system tools, system libraries, and settings.

    A container is a bundle of Application, Application libraries required to run your application and the minimum system dependencies.

Containers vs Virtual Machine

Containers and virtual machines are both technologies used to isolate applications and their dependencies, but they have some key differences:

  1. Resource Utilization: Containers utilize the host operating system’s kernel, making them more lightweight and faster compared to virtual machines (VMs). VMs, on the other hand, include a complete operating system and hypervisor, which makes them more resource-intensive.

  2. Portability: Containers are inherently portable and can operate on any system with a compatible host OS. In contrast, VMs require a compatible hypervisor, making them less portable.

  3. Security: VMs offer a higher level of security since each VM runs its own operating system, providing isolation from the host and other VMs. Containers, sharing the host OS, offer less isolation and therefore, a different security model.

  4. Management: Managing containers is generally simpler than managing virtual machines (VMs) because containers are designed to be lightweight and agile.

Why are containers light weight ?

Containers are efficient and lightweight due to containerization technology, which enables them to share the host OS’s kernel and libraries. This sharing provides isolation for applications and their dependencies without the need for a full operating system, resulting in a smaller footprint compared to traditional virtual machines. Moreover, Docker containers are designed to be minimalistic, including only the essentials required for the application to run, further reducing their size.

For example:

The official Ubuntu base image for containers is remarkably small, at around 22 MB. In contrast, an official Ubuntu VM image is approximately 2.3 GB. This means the container base image is nearly 100 times smaller than the VM image. Isn’t that impressive?

Files and Folders in containers base images

    /bin: it contains binary executable files, such as the ls, cp, and ps commands.

    /sbin: it contains system binary executable files, such as the init and shutdown commands.

    /etc: it contains configuration files for various system services.

    /lib:  it contains library files that are used by the binary executables.

    /usr:  it contains user-related files and utilities, such as applications, libraries, and documentation.

    /var: it contains variable data, such as log files, spool files, and temporary files.

    /root: it is the home directory of the root user.

In summary, container base images are generally much smaller than VM images because they are designed to be minimalistic, including only the essential components needed to run a specific application or service. Conversely, VMs emulate a complete operating system with all its libraries, utilities, and system files, leading to a significantly larger size.

Docker

What is docker?

Docker is a containerization platform that simplifies the process of containerizing your applications. With Docker, you can build container images, run these images to create containers, and push these containers to registries like DockerHub etc. containerization is a concept or technology, and Docker is a tool that implements this concept.

Docker architecture ?

The Gif picture, clearly indicates that Docker Deamon is brain of Docker.

Docker lifeCycle

Here are three key Docker commands:

  • docker build: Builds Docker images from a Dockerfile.

  • docker run: Runs a container from a Docker image.

  • docker push: Pushes the container image to public or private registries to share the Docker images.

These commands are fundamental for working with Docker and managing your containerized applications.

Docker Client - The Docker client (docker) is the main way users interact with Docker. Commands like docker run are sent from the client to the daemon, which executes them. The Docker client can communicate with multiple daemons.

Docker Desktop - Docker Desktop is an easy-to-install application for Mac, Windows, or Linux that allows you to build and share containerized applications and microservices. It includes the Docker daemon, Docker client, Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper.

Docker Registries - A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can also run your own private registry. Commands like docker pull and docker run pull images from the registry, while docker push uploads images to the registry.

Docker Daemon - The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects like images, containers, networks, and volumes. It can also communicate with other daemons to manage Docker services.

Docker Objects - When using Docker, you work with various objects such as images, containers, networks, volumes, and plugins. Here’s a brief overview:

  • Dockerfile: A file containing the steps to build a Docker image.

  • Images: Read-only templates with instructions for creating a Docker container. Images can be based on other images with additional customizations. For example, you might build an image based on the Ubuntu image that includes the Apache web server and your application. Each instruction in a Dockerfile creates a layer in the image, making images lightweight, small, and fast compared to other virtualization technologies

Install Docker

You can find comprehensive instructions for installing Docker at the following link.

https://docs.docker.com/get-docker/

For Demo,

You can create an Ubuntu EC2 Instance on AWS and run the below commands to install docker.

sudo apt update
sudo apt install docker.io -y

Start Docker and Grant Access

A common mistake beginners make is forgetting to start the Docker daemon and grant access to the user after installing Docker with sudo access.

Always ensure the Docker daemon is up and running.

To verify your Docker installation, you can run the following command:

docker run hello-world

If the output says:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

This could indicate two issues:

  1. The Docker daemon is not running.

  2. Your user does not have access to run Docker commands.

To start the Docker daemon and verify it’s active, use the following command:

sudo systemctl status docker

If you find that the Docker daemon is not running, you can start it using the following command:

sudo systemctl start docker

Grant Access to Your User to Run Docker Commands

To allow your user to run Docker commands, you need to add the user to the Docker group. This group is created by default when Docker is installed. Here’s how you can do it:

sudo usermod -aG docker ubuntu

After running this command, log out and log back in for the changes to take effect. This will grant your user the necessary permissions to run Docker commands without needing sudo.

In the above command, ubuntu is the username. You can replace it with your specific username.

Note: You need to log out and log back in for the changes to take effect.

Docker is now installed, up, and running! 🥳🥳

To verify that Docker is working correctly, run the following command:

docker run hello-world

The output should look something like this:

....
....
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
...

This confirms that your Docker installation is functioning properly.

19
Subscribe to my newsletter

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

Written by

Shashank Vimal
Shashank Vimal

An individual who uses AI prompts, stack overflow threads and coffee to assemble software that occasionally works as expected...