Quick Start Guide to Docker for Beginners

Ashwini DekaAshwini Deka
5 min read

What does Docker mean for a layman??

Docker is a tool used to create, deploy, and manage containerized applications. It allows you to package an application and everything it needs (like libraries and dependencies) into a container. This container acts like a lightweight, portable box that can run consistently anywhere—on your laptop, a server, or in the cloud—without compatibility issues.

In simple terms, Docker ensures your application works the same way, no matter where you run it.

Why docker is used?

Docker is used to simplify the process of building, deploying, and managing applications.

Docker guarantees that applications operate uniformly across production servers, testing servers, and developers' laptops by bundling them into containers that contain all of their dependencies.

Containers are both portable and extremely lightweight. These can be effortlessly transferred between various environments or platforms, such as from local development to cloud services.

Additionally, containers are more lightweight and faster than virtual machines (VMs) because they share the kernel of the same host operating system.

The above snapshot is an illustration for differentiating between virtual machines and containers.

Key Elements of Docker

  1. Docker Engine: It is the core part of Docker, which is responsible for building, running, and managing containers on a Docker host system.

    Components of Docker Engine:

    i) Docker Daemon: Its Docker’s integral service that manages containers.

    ii) Docker CLI: It is the command-line interface that is used to interact with Docker.

    iii) REST API: Docker offers a REST API (Docker Engine API) that enables interaction with the Docker Engine, thereby enabling the automation and management of Docker resources through HTTP requests.

  2. Docker File: A Dockerfile is a text file that contains a set of instructions for the creation of a Docker image. It specifies the main image, application code, dependencies, and configurations necessary to run your application within a container environment.

  3. Docker Images: A Docker image is a read-only, lightweight template that includes the application code, runtime, libraries, and dependencies required to execute an application. A Docker image functions as a blueprint or template for creating a Docker container.

  4. Docker Container: Containers are nothing but running instance environments from Docker images. It is a live, isolated environment where the application runs. In other words, containers are lightweight, independent, and portable instances that share the host system's kernel while maintaining isolation.

Uninstall if there is any preinstalled Docker Engine

Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

Images, containers, volumes, or custom configuration files on your host aren't automatically removed. To delete all images, containers, and volumes, kindly use the below commands:

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

For more details, you can visit documentation section of Docker official website: https://docs.docker.com/engine/install/ubuntu/

Next step is to install Docker the easy way

In my case, I will be using a script to install Docker, which is an easier way to do so.

curl -fsSL https://get.docker.com -o get-docker.sh

The above command will download the script from https://get.docker.com

sudo sh get-docker.sh

The above command will execute the downloaded script

Verify Docker version

docker version
docker --version

Any one of the above commands can be used to check the docker version, while the docker version command will provide you with detailed information.

Pulling a new docker image from docker hub

docker pull nginx

The above command will download the Docker nginx image from https://hub.docker.com/ and store it on the Docker host.

docker run nginx

docker run nginx will look for the nginx image from the Docker host and then create and then run the container from the Docker image nginx.

(The above method runs the nginx image in non-detach mode.) When detach mode is not enabled, the container would operate in the foreground, causing the terminal to become congested and displaying logs.

docker run -d nginx The above is detach mode, which operates the container in the background, thereby clearing up your terminal session. Flag-d enables the container to operate in the background, thereby freeing up your terminal for other tasks.

Check for installed docker images

docker images

Commanddocker images list all the Docker images available locally on your system or the Docker host. Docker Image IDs are unique.

Deleting a docker image

Make sure that there is no container instance of nginx running in the background. To verify this, use the docker ps -a command.

docker ps -a

The docker ps -a command lists all containers on your system, including those that are currently running, stopped, or exited. Above, we can see two containers running from the same nginx image.

Note: Docker Image ID and Container ID are different. Docker image ID will be unique and can be used to create multiple containers. Containers are nothing but running instance environments from Docker images; each container has its own isolated environment and unique container ID. You can run multiple containers from the same Docker image, each with its own unique container ID but all based on the same image.

docker rmi nginx

Since from the above image two container instances are already running, trying to delete the Docker image directly will give an error.

One can delete the Docker image directly using the flag -f docker rmi -f nginx .

However, it is recommended using the correct method for deleting Docker images, which involves first stopping the containers, then deleting them, and finally deleting the Docker image itself.

docker stop CONTAINER_ID

Once you stop the running container, please proceed with deleting the container.

docker rm CONTAINER_ID

After you have successfully removed the container associated with the Docker image, you can delete the Docker image from the Docker host machine using the command below.

docker rmi nginx

In conclusion, Docker is an essential tool for modern software development and deployment, offering a streamlined approach to managing applications through containerization. By encapsulating applications and their dependencies into containers, Docker ensures consistent performance across different environments, from local development to cloud deployment. Its lightweight nature and efficiency make it a preferred choice over traditional virtual machines, providing developers with the flexibility to build, test, and deploy applications seamlessly.

As you embark on your Docker journey, this guide serves as a foundational resource to help you understand and leverage Docker's capabilities effectively.

0
Subscribe to my newsletter

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

Written by

Ashwini Deka
Ashwini Deka