Containerization 101: Mastering Docker Essentials

Containerization 101: Mastering Docker Essentials

Introduction

Docker is a powerful platform that enables developers to create, deploy, and run applications in isolated environments called containers. Containers are lightweight and portable, ensuring that applications run consistently across different environments. This article will walk you through the basic concepts of Docker, along with the necessary commands to get you started.


What is Docker?

Docker is an open-source platform that automates the deployment of applications inside containers. Containers are isolated from one another and contain all the necessary executables, binary code, libraries, and configuration files.


Docker Architecture

Docker uses a client-server architecture:

  • Docker Client: The interface where users interact with Docker. Commands issued by the client are sent to the Docker Daemon.

  • Docker Daemon: The background service responsible for building, running, and managing Docker containers.

  • Docker Registry: Stores Docker images. Docker Hub is the default public registry.


Docker Images

Docker images are read-only templates used to create containers. They contain the application code and its dependencies.

Building an Image

Create a Dockerfile:

# Use an official Node.js runtime as a parent image
FROM node:20-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install app dependencies
RUN npm install

# Bundle app source
COPY . .

# Expose port 3000
EXPOSE 3000

# Define environment variable
ENV NODE_ENV=production

# Run the app
CMD ["node", "app.js"]

Build the image:

docker build -t my-node-app .

Listing Images

docker images

Removing an Image

docker rmi <image_id>

Docker Containers

Containers are runnable instances of Docker images. They are isolated environments where applications run.

Running a Container

docker run -d -p 3000:3000 [imageName]

Running the OS in a Container

docker run -it [imageName] sh

Listing Running Containers

docker ps

Listing All Containers (Running and Stopped)

docker ps -a

Stopping a Container

docker stop <container_id>

Removing a Container

docker rm <container_id>

Force Remove Container

docker rm <container_id> --force

Docker Volumes

Volumes are used to persist data generated and used by Docker containers. Volumes are the preferred mechanism for persisting data because they are managed by Docker and are independent of the container lifecycle.

Creating a Volume

docker volume create my-volume

Using a Volume

docker run -d -p 3000:3000 -v [volumeName]:/app [ImageName]

Listing Volumes

docker volume ls

Removing a Volume

docker volume rm [volumeName]

Docker Networks

Docker networks allow containers to communicate with each other. By default, Docker creates a bridge network for containers to use.

Creating a Network

docker network create my-network

Running a Container in a Network

docker run -d --network [networkName] [imageName]

Listing Networks

docker network ls

Removing a Network

docker network rm [networkName]

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications.

compose.yml Example

version: "3.8"

services:
  web:
    depends_on: 
      - api
    build: ./frontend
    ports:
      - 5173:5173
    environment:
      VITE_API_URL: http://localhost:8000
    develop:
      watch:
        - path: ./frontend/package.json
          action: rebuild
        - path: ./frontend/package-lock.json
          action: rebuild
        - path: ./frontend
          target: /app
          action: sync

  api: 
    depends_on: 
      - db
    build: ./backend
    ports: 
      - 8000:8000
    environment: 
      DB_URL: mongodb://db/anime
    develop:
      watch:
        - path: ./backend/package.json
          action: rebuild
        - path: ./backend/package-lock.json
          action: rebuild
        - path: ./backend
          target: /app
          action: sync

  db:
    image: mongo:latest
    environment: 
      - mongodb://localhost:27017/mydatabase
      - anime:/data/db

volumes:
  anime:

Running Docker Compose

sudo compose up

Stopping Docker Compose

sudo compose down

Docker Compose Watch

docker compose watch automatically restarts containers when files change. It's useful during development for a live-reload experience.

Example:

sudo docker compose -f compose.yml watch

Docker Hub

Docker Hub is a cloud-based registry where you can find and share container images.

Logging In to Docker Hub

docker login

Tagging an Image

docker tag [imageName] username/my-node-app:tagname

Pushing an Image to Docker Hub

docker push username/my-node-app:tagname

Pulling an Image from Docker Hub

docker pull username/my-node-app:tagname

Docker Secrets

Docker secrets are used to securely manage sensitive data, such as passwords, API keys, and certificates, in Docker Swarm mode.

Creating a Secret

echo "my_secret_password" | docker secret create my_secret -

Listing Secrets

docker secret ls

Using a Secret in a Service

In a docker-compose.yml file:

version: "3.8"

services:
  web:
    image: my-web-app
    secrets:
      - my_secret

secrets:
  my_secret:
    external: true

Removing a Secret

docker secret rm my_secret

Docker Scout

Docker Scout is a tool designed to analyze your Docker images and suggest improvements. It can help identify vulnerabilities, outdated packages, and other potential issues within your images.

Installing Docker Scout

Docker Scout is integrated into Docker Desktop. If you're using Docker Desktop, it's already available. For other environments, you can install it via Docker CLI.

Running Docker Scout

docker scout quickview <image_name>

This command provides a quick overview of your image's security status, including vulnerabilities and suggestions for remediation.


Docker Init

docker init is a command to quickly create a Dockerfile and .dockerignore file for a new or existing project.

Example:

docker init

This will interactively guide you through setting up a Dockerfile for your project.