Containerization & Docker Explained

What are Containers?

Containers are lightweight, standalone, executable packages that include everything required to run a piece of software: code, runtime, system tools, libraries, and settings. This ensures consistent operation across various environments, irrespective of differences in the underlying infrastructure. Additionally, containers share the host operating system's resources while isolating application processes

What is Docker?

Docker is the platform that popularized containerization. It provides tools to create, deploy, and manage containers. With Docker, you can package an application and its dependencies into a Docker image, which can then be shared and run anywhere.

Key Components of Docker

When using Docker, there are a few key names, that you should know.

  • Docker Engine: The core of Docker, responsible for creating and running Docker containers
  • Docker Images: Read-only templates used to create containers. They include the application and its dependencies
  • Docker Containers: Running instances of Docker images. Containers are isolated and have their own filesystem, networking, and processes
  • Docker Hub: A public registry where Docker images can be stored and shared. It allows you to find and use images created by others
  • Docker Compose: A tool for defining and running multi-container Docker applications. With a simple YAML file, you can configure your application's services, networks, and volumes.
  • Docker Swarm: Docker’s native clustering and orchestration tool. It allows you to manage a swarm of Docker Engines (nodes) and deploy services across them

How to use Docker and Docker Compose

So, how do you actually use Docker? First, you need to install Docker on your system by following the Docker Documentation. Once installed, you can run Docker either using CLI commands or using YAML files with Docker Compose.

Using Docker CLI

The CLI commands are simpler if you just want to test something or only want to deploy a few containers. For example, to create a simple Nginx container:

docker run --name reverse-proxy -d -p 80:80 -p 443:443 -v /nginx.conf:/etc/nginx/nginx.conf -v /var/www:/var/www nginx:latest

Using Docker Compose

services:
  nginx:
    container_name: reverse-proxy
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx-config:/etc/nginx/conf.d:ro
      - /var/www:/var/www

Common Pitfalls

  • Non-persistent Storage: By default, Docker containers use non-persistent storage, meaning data is lost when the container stops. To avoid this, mount volumes to the container.
  • Public Ports: On a container, the ports are publicly accessible by default. This can be restricted by creating an internal network or binding ports to localhost (127.0.0.1). This can especially be an issue if you use a VPS.

Benefits of Using Containers

Containers offer numerous benefits, making them a go-to solution for modern application development:

  • Portability: Containers can run consistently across different environments, allowing copy configuration between different environments for moveability to different systems
  • Scalability: Containers can be easily scaled up or down to handle varying loads, making them ideal for microservices and cloud-native applications
  • Efficiency: Containers share the host system’s kernel and resources, reducing overhead compared to traditional virtual machines (VMs)
  • Isolation: Containers isolate applications from each other and the host system, enhancing security and stability
  • Rapid Deployment: Containers can be quickly started or stopped, speeding up development, testing, and deployment processes

Issues with Containers

While containers bring many advantages, they also come with some challenges:

  • Complexity: Managing a large number of containers and orchestrating them (e.g., using Kubernetes) can get rather complex with a lot more challenges then just running a few one
  • Networking: Networking can become complicated in containerized environments, requiring careful planning and management