Chapter 1 - Introduction to Docker

yusbuntu

Yusuf Isah

Posted on August 9, 2024

Chapter 1 - Introduction to Docker

Image description

What is Docker?

The world first got to know about Docker when Solomon Hykes, founder and CEO of dotCloud, presented it in a five-minute lightning talk at the Python Developers Conference in Santa Clara, California, on March 15, 2013. At the time of this announcement, only about 40 people outside dotCloud had been given the opportunity to play with Docker.

Since then, Docker has grown exponentially in popularity, with Tech Giants like Google, Amazon, and Microsoft adopting the platform. Today, Docker is widely used in production environments, and its ecosystem includes a bountiful array of tools and services for building, deploying, and managing containerized applications. Despite its popularity, many people, especially in the developing part of the world have never heard about Docker.

Docker is a containerization platform that allows developers to package, ship, and run applications in containers. Because containers are lightweight and portable, they provide a consistent and reliable way to deploy applications across different environments.

You might be asking yourself; what is a container? Think of a container like a shipping container that holds everything an application needs to run, such as the code, libraries, dependencies, and settings. This container can be easily moved between environments, such as from your laptop to a testing server, or from a staging environment to production.

Docker provides an environment for containers to run in isolation from each other and from the host system. This ensures that applications run consistently and without conflicts, making it easier to develop, test, and deploy software.

Understanding Containerization and Its Benefits

Containerization is an awesome technology that allows multiple applications to run on a single host operating system (OS), with each application running in its own isolated environment called a container. Containers share the same kernel as the host OS but have their own isolated processes, memory, and filesystems.

Docker is a container runtime, which simply means it is a software or engine that runs containers on a host OS. In other words, a container runtime manages the life cycle of containers, including creating, starting, stopping, and deleting containers. Containerization works by using a container runtime, such as Docker, to create and manage containers.

Some of the benefits of containerization include:

  • Lightweight: Containers are much lighter than virtual machines, as they don't require a separate OS instance for each application.

  • Portability: Containers are highly portable and can run on any system that supports the container runtime, without requiring modifications to the application.

  • Isolation: Containers provide a high level of isolation between applications, ensuring that they don't interfere with each other.

  • Scalability: Containerization makes it easy to scale applications horizontally, by simply creating more containers as needed.

  • Efficiency: Containers consume fewer resources than virtual machines, making them more efficient in terms of CPU, memory, and storage usage.

  • Flexibility: Containers support a wide range of applications, from web servers to databases to microservices.

  • Manageability: Containers provide a simple and consistent way to manage applications, using tools like Docker Compose and Kubernetes.

Differences Between Containers and Virtual Machines

Both containers and virtual machines (VMs) are used to run applications in isolated environments, but they differ in their approach and architecture.

Virtual Machines: A VM is a self-contained operating environment that runs on top of a host machine's OS. Each virtual machine includes:

  • A guest operating system (e.g., Windows, Linux)

  • Application software

  • Virtualized hardware (e.g., CPU, memory, storage)

VMs use a hypervisor (e.g., VMware, VirtualBox) to create and manage virtualized hardware resources. Each virtual machine is a heavyweight, standalone entity, consuming significant resources.

Containers: A container is a lightweight, stand-alone package that includes:

  • Application software

  • Dependencies (e.g., libraries, frameworks)

  • Configuration files

Containers share the host operating system's kernel and use a container runtime (e.g., Docker) to manage resources and provide isolation. Containers are considered ephemeral, as they are created and deleted as needed.

Other key differences between virtual machines and containers include:

  • Portability: Containers are highly portable across environments, while VMs are tied to specific hypervisors.

  • Boot time: Containers start instantly, while VMs require significant boot time.

Docker Architecture

Docker architecture includes several key components that work together to manage and run containers. It is designed to provide a lightweight, portable, and flexible way to deploy applications. It consists of the following components:

  • Docker Engine: This refers to the container runtime (e.g., runc). It is the core component responsible for creating and running Docker containers.

  • Docker Client: The Docker client is the primary interface for interacting with Docker. It is a command-line tool that allows users to interact with the Docker Daemon. It's used to create, manage, and run containers. The client sends requests to the Docker daemon, which executes the requests.

  • Docker Daemon: The Docker daemon (dockerd) is the background service responsible for managing Docker containers and images. It listens for requests from the Docker client and executes them. The daemon runs on the host machine and is responsible for:

  • Managing containers (create, start, stop, delete)

  • Managing images (pull, push, build)

  • Providing a REST API for interacting with Docker

  • Docker Registry: The Docker registry (e.g., Docker Hub) is a centralized storage for storing and sharing Docker images. Images are pushed to the registry, and then pulled by the Docker daemon when needed.

Key Docker Concepts

Docker uses several key concepts to provide a powerful and flexible platform for containerization. Understanding these concepts is essential for effective Docker usage.

  • Docker Image: A Docker image is a lightweight, stand-alone, and executable package that includes everything an application needs to run, such as:

  • Code

  • Libraries

  • Dependencies

  • Configuration files

  • Environment variables

Images are the basis for containers and are stored in a Docker registry (e.g.,
Docker Hub).

  • Docker Container: A Docker container is a runnable instance of a Docker image.

  • Dockerfile: A script containing a series of instructions on how to build a Docker image. It specifies:

  • Base image

  • Commands to run

  • Files to copy

  • Environment variables

  • Ports to expose

Docker builds an image by executing the instructions in the Dockerfile.

Installing Docker

Docker is available for Windows, MacOS, and Linux. The installation process varies slightly depending on your operating system.

Installing Docker on Windows

  • Download Docker Desktop from the Docker website.

  • Run the installer and follow the installation instructions.

  • Open Docker Desktop to start Docker.

Installing Docker on MacOS:

  • Download Docker Desktop for Mac from the Docker website.
  • Open the downloaded .dmg file and drag Docker to the Applications folder.
  • Open Docker from the Applications folder.

Installing Docker on Linux:

  • Update your package database:
  sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
  • Install Docker
  sudo apt-get install docker-ce docker-ce-cli containerd.io
Enter fullscreen mode Exit fullscreen mode
  • Start the Docker service
  sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Basic Docker Commands

Docker provides a wide range of commands to manage containers, images, and volumes. Here are some basic Docker commands to get you started:

  • docker run: This command runs a container from an image, e.g, in the code below, we will run the hello-world container using the docker run command:
  docker run hello-world
Enter fullscreen mode Exit fullscreen mode
  • docker ps: This command lists running containers.
  docker ps
Enter fullscreen mode Exit fullscreen mode
  • docker ps -a: This command lists all containers, including the stopped ones.
  docker ps -a
Enter fullscreen mode Exit fullscreen mode
  • docker images: This commands lists all Docker images on your machine.
  docker images
Enter fullscreen mode Exit fullscreen mode
  • docker stop: This command stops a running container.
  docker stop hello-world
Enter fullscreen mode Exit fullscreen mode
  • docker kill: This command is used to forcibly stop a running container. It can be used as an alternative to the docker stop command.
  docker kill hello-world
Enter fullscreen mode Exit fullscreen mode
  • docker start: This command starts a stopped container.
  docker start hello-world
Enter fullscreen mode Exit fullscreen mode
  • docker rm: This command removes a container.
  docker rm hello-world
Enter fullscreen mode Exit fullscreen mode

Make sure to stop the hello-world container before attempting to remove it.

  • docker build: This command builds an image from Dockerfile.
  docker build -t my-image .
Enter fullscreen mode Exit fullscreen mode

NB: In the code above, -t stands for tag, the name you want to give your image which in this case is my-image. The . at the end of the code represents the current working directory, which is where the Dockerfile is usually located.

  • docker pull: This command pulls an image from a registry.
  docker pull ubuntu
Enter fullscreen mode Exit fullscreen mode
  • docker rmi: This command removes an image from your machine.
  docker rmi ubuntu
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we introduced Docker, discussed its benefits, key concepts, installation steps, and some basic commands. Docker is an essential tool in the DevOps toolkit, enabling developers and system administrators to create, deploy, and run applications in isolated containers efficiently.

Feel free to leave comments and share this article. Follow my blog for more insights on Docker!

💖 💪 🙅 🚩
yusbuntu
Yusuf Isah

Posted on August 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Docker Containers vs VMs
docker Docker Containers vs VMs

July 21, 2019