Introduction to Containerization with Docker

Docker has revolutionized the way software is deployed. It offers a flexible and highly efficient method of packaging applications, ensuring consistent environments from development through production. But what powers this game-changer?

At the core of Docker is the concept of containerization—a lightweight alternative to full machine virtualization that involves encapsulating an application and its environment into a closed box. In this blog, we’ll lay down the foundational understanding of Docker and guide you through the setup.

Prerequisites

To follow this guide, you should have:

  • A computer with Linux installed (e.g., Ubuntu)
  • A user account with sudo privileges
  • Basic understanding of Linux command line

Installing Docker on Linux

Before utilizing Docker, it must be installed on your Linux distribution. Use the following instructions to install Docker Engine on an Ubuntu system:

# Update your existing list of packages
sudo apt-get update

# Install the necessary packages for apt to use a repository over HTTPS
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y

# Add the GPG key for the official Docker repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add the Docker repository to APT sources
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# Update the package database with Docker packages from the newly added repo
sudo apt-get update

# Make sure you are installing from the Docker repository instead of the default Ubuntu repository
apt-cache policy docker-ce

# Install Docker
sudo apt-get install docker-ce -y

# Verify Docker installation
sudo systemctl status docker

Understanding Docker Architecture

Docker uses a client-server architecture:

  • Docker Client: The client uses the Docker CLI (Command Line Interface) to communicate with the Docker server.
  • Docker Server (Daemon): Handles requests from Docker client and manages Docker objects such as images, containers, networks, and volumes.
  • Docker Images: Immutable files that contain the source code, libraries, dependencies, tools, and other files needed for your application.
  • Docker Containers: A running instance of a Docker image. Containers can access OLTP (Online License Terms and Policies) directly without any hypervisor overhead.

Docker Architecture

First Steps with Docker

Let’s dive into some Docker commands to get a feel of how Docker operates:

Pulling Images

Docker Hub provides a massive library of images. Begin by pulling an image from Docker Hub:

# Pull the `hello-world` image from Docker Hub
sudo docker pull hello-world

Running a Simple Container

Once the image is pulled, you can run a container:

# Run the `hello-world` container
sudo docker run hello-world

This will download the hello-world image, create a container, and execute the container that instantly prints a welcoming message. If the image is already downloaded, it simply creates and executes the container.

Listing Images and Containers

To list all Docker images you have on your system, use:

sudo docker images

For all running containers, the command is:

sudo docker ps

To see all containers (including stopped ones), use:

sudo docker ps -a

Stopping and Removing Containers

You can stop a running container using:

# Replace CONTAINER_ID with your container's ID
sudo docker stop CONTAINER_ID

To remove stopped containers, execute:

# Replace CONTAINER_ID with your container's ID
sudo docker rm CONTAINER_ID

Conclusion

Containerization, with Docker at its helm, offers a vital toolset for developers. Docker containers are light, consistent, and ensure that applications run smoothly across different environments, from test stages through to production. By mastering Docker’s foundational concepts and commands, you’ll harness the full potential of your development environment.