Kahibaro
Discord Login Register

4.7.3 Docker fundamentals

Why Docker Matters

Docker is a container runtime and tooling ecosystem focused on:

In this chapter, “Docker” means:

Higher‑level container concepts and alternatives (LXC, Podman, etc.) are covered in other chapters; here we focus on Docker itself.

Key strengths of Docker:

Installing and Running Docker (Overview)

Installation details vary per distro, so here’s only the high‑level picture.

Typical components after installation:

On most systems, you will:

  1. Install Docker Engine via your distro’s package manager or from Docker’s repo.
  2. Ensure the service is running: systemctl status docker
  3. Add your user to the docker group (optional, to avoid always using sudo).
  4. Test with:
   docker run hello-world

This pulls a test image and runs a tiny container to verify the setup.

Core Docker Concepts

Images

An image is a read‑only template used to create containers.

Common commands:

Images are immutable: you never “change” an image; you create a new image/layer and tag it.

Containers

A container is a running (or stopped) instance of an image.

Core lifecycle:

Basic commands:

Registries and Repositories

A registry is a service that stores and distributes images.

A repository is a named collection of related images in a registry:

Workflow:

Running Containers

The `docker run` Command

docker run is the central command for starting containers:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Key ideas:

Examples

Run a one‑off command in a container:

docker run --rm alpine echo "Hello from Alpine"

Run an interactive shell:

docker run -it --rm alpine sh

Detached Containers

To run in the background, use -d:

docker run -d --name web nginx

See it running:

docker ps

Stop and remove:

docker stop web
docker rm web

Port Mapping

Containers have their own network namespace. To expose container ports to the host, use -p:

docker run -d --name web -p 8080:80 nginx

You can map different host ports or multiple ports:

docker run -d -p 8000:80 -p 8443:443 nginx

Managing Container Lifecycle

Useful commands:

Example: debug a running container:

docker exec -it web sh

This starts sh inside the existing web container (Nginx remains running).

Docker Volumes and Storage Basics

Containers are ephemeral by default:

To persist or share data, Docker provides:

Volumes

A volume is managed by Docker and stored outside the container’s writable layer.

Create and use a volume:

docker volume create mydata
docker run -d \
  -v mydata:/var/lib/mysql \
  --name db mysql:8

List volumes:

docker volume ls
docker volume inspect mydata

Remove unused volumes:

docker volume rm mydata

Bind Mounts

A bind mount maps an existing host path into the container.

Example:

docker run -d \
  -v /home/user/web:/usr/share/nginx/html:ro \
  -p 8080:80 \
  --name web \
  nginx

Bind mounts are useful for:

Basic Docker Networking

A full treatment of container networking is elsewhere; here are the essentials to use Docker comfortably.

Default Networks

Docker creates some networks automatically:

Check networks:

docker network ls

Inspect a network:

docker network inspect bridge

User-Defined Bridge Networks

User-defined bridge networks allow containers to communicate by name:

docker network create mynet
docker run -d --name db --network mynet mysql:8
docker run -d --name app --network mynet myapp:latest

Inside app, the hostname db resolves automatically to the DB container’s IP.

Advantages:

Attach an existing container:

docker network connect mynet some_container

Building Images with Dockerfiles

A Dockerfile describes how to build an image step by step.

Common structure:

FROM ubuntu:22.04
# Install dependencies
RUN apt-get update && apt-get install -y \
    curl \
    ca-certificates \
 && rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy application files
COPY . /app
# Set environment variable
ENV APP_ENV=production
# Expose port (documentation only)
EXPOSE 8080
# Default command
CMD ["./start.sh"]

Key instructions (high‑level overview):

Building an Image

From a directory containing a Dockerfile:

docker build -t myapp:1.0.0 .

Then run it:

docker run -d -p 8080:8080 --name myapp myapp:1.0.0

Image Tagging and Versioning Basics

Tags act like labels:

Common practices:

Tagging an existing image:

docker tag myapp:1.0.0 myregistry.local/myteam/myapp:1.0.0

Pushing to a registry:

docker push myregistry.local/myteam/myapp:1.0.0

Inspecting and Debugging Containers

Useful tools for understanding what’s happening inside Docker.

Inspecting Containers and Images

Example:

docker inspect web | jq '.[0].NetworkSettings.IPAddress'

Interactive Debugging

To troubleshoot a running container:

docker exec -it web /bin/sh
# or /bin/bash depending on the image

If the container exits immediately and you want to keep it alive to inspect:

docker run -it --entrypoint sh IMAGE

This overrides the default entrypoint and gives you a shell.

Cleaning Up

Containers and images can accumulate over time.

Basic cleanup:

  docker container prune
  docker image prune
  docker system prune

Use --volumes with caution to also prune unused volumes.

Security and Best Practices (Intro Level)

Full container security is a larger topic; here are some Docker‑specific fundamentals:

  RUN useradd -m appuser
  USER appuser

Putting It Together: A Simple Example Workflow

  1. Develop a simple web service (code + Dockerfile).
  2. Build:
   docker build -t myuser/simple-web:1.0.0 .
  1. Run locally:
   docker run -d -p 8080:80 --name simple-web myuser/simple-web:1.0.0
  1. Test via curl http://localhost:8080.
  2. Tag and push to a registry:
   docker tag myuser/simple-web:1.0.0 myregistry.local/myuser/simple-web:1.0.0
   docker push myregistry.local/myuser/simple-web:1.0.0
  1. Deploy the same image on another host that has Docker installed and access to the registry:
   docker pull myregistry.local/myuser/simple-web:1.0.0
   docker run -d -p 80:80 myregistry.local/myuser/simple-web:1.0.0

This encapsulates the fundamental Docker pattern: build once, run anywhere that supports Docker.

Views: 71

Comments

Please login to add a comment.

Don't have an account? Register now!