Docker

Docker

What is a Container?

A container is a standard unit of software that packages up code and all its dependencies, ensuring that an application runs quickly and reliably across different computing environments. A Docker container image is a lightweight, standalone, executable package that includes everything needed to run an application—code, runtime, system tools, libraries, and settings.

Simplified Definition

A container is essentially:

  • Application code

  • Application libraries required to run the application

  • Minimal system dependencies

Visualizing Containers vs Virtual Machines (VMs)

  • Containers share the host OS kernel, making them lightweight and faster.

  • VMs include a full OS and a hypervisor, which makes them more resource-intensive.

FeatureContainersVirtual Machines (VMs)
Resource UsageShares host OS kernel, lightweightIncludes full OS, resource-intensive
PortabilityHighly portableLess portable
SecurityLess isolatedMore isolated
ManagementEasierMore complex

Containers will effectively use the virtual machines to reduce the problem further.

Problems with physical servers solved some extend using virtual machines. Now, the problem of virtual machines will solved to some extend using containers.

Why Are Containers Lightweight?

Containers leverage containerization technology, which allows them to share the host OS kernel and libraries while isolating the application and its dependencies. This results in a smaller footprint compared to traditional VMs.

Example: Ubuntu Base Image vs VM Image

  • Ubuntu Container Image: ~22 MB

  • Ubuntu VM Image: ~2.3 GB

Containers are designed to include only the components necessary for the application to run, whereas VMs emulate an entire operating system, resulting in larger sizes.


Docker

What is Docker?

Docker is a containerization platform that simplifies the process of:

  1. Building container images

  2. Running containers

  3. Pushing container images to registries (e.g., DockerHub)

Containerization is the concept, and Docker is its implementation.


Docker Architecture

Key Components:

  • Docker Daemon (dockerd): Manages Docker objects like images, containers, and networks.

  • Docker Client: The primary interface for users to interact with Docker.

  • Docker Registries: Stores Docker images (e.g., DockerHub).


Docker Lifecycle

  1. docker build: Builds Docker images from a Dockerfile.

  2. docker run: Runs containers from Docker images.

  3. docker push: Pushes images to public/private registries.


Installing Docker

Follow the detailed instructions provided in the Docker Docs for your platform.

Quick Installation on Ubuntu

  1. Update the package list:

     sudo apt update
    
  2. Install Docker:

     sudo apt install docker.io -y
    

    -y : option is for "yes" passing directly so that, while installing terminal don't prompt us to hit yes.

Verify Installation

  1. Check docker version:

      docker --version
    
  2. Check Docker daemon status:

     sudo systemctl status docker
    

    If it’s not running, start/enable it:

     sudo systemctl start docker
     sudo systemctl enable docker
    
  3. Run the test container:

     docker run hello-world
    

    If you encounter permission issues, grant your user access:

     sudo usermod -aG docker $USER
    

    Logout and login again for changes to take effect.


Writing Your First Dockerfile

A Dockerfile is a script of instructions to build a Docker image.

Example Dockerfile

FROM ubuntu:latest
WORKDIR /app
COPY . /app
RUN apt-get update && apt-get install -y python3 python3-pip
ENV NAME World
CMD ["python3", "app.py"]

Build the Docker Image

docker build -t my-first-docker-image .
  • -t my-first-docker-image: Specifies the name (tag) for the image.

  • .: Refers to the current directory, where the Dockerfile is located.


Verify the Image

docker images

Run the Docker Container

docker run -it my-first-docker-image
  • -i: Keeps the STDIN open for the container (interactive mode).

  • -t: Allocates a pseudo-TTY for the container.

  • my-first-docker-image: Refers to the image name you created earlier.

Push the Image to DockerHub

  1. Login to Docker:

     docker login
    
  2. Push the image:

     docker push <your-username>/my-first-docker-image
    
  • <docker-image-name>: my-file-docker-image in this example.

Summary

Key Terms

  • Container: Lightweight, portable application environment.

  • Docker: Platform for containerization.

  • Dockerfile: Script to build Docker images.

  • Image: Blueprint for creating containers.

  • Container: Running instance of an image.

Commands to Remember

  1. Build an image:

     docker build -t <image-name> .
    
  2. Run a container:

     docker run -it <image-name>
    
  3. Push an image:

     docker push <username>/<image-name>