DOCKER



This content originally appeared on DEV Community and was authored by Sarah Muriithi

GETTING STARTED WITH DOCKER AND DOCKER COMPOSE: A BEGINNER GUIDE

1. What is Docker?

  • Docker is a tool used for building, running and managing containers.
  • A Container is a lightweight, portable package that has everything an application needs to run-code, libraries, system tools and settings.

2. Install Docker

  • On Ubuntu/Linux
    sudo apt install gnome-terminal
    sudo apt update
    sudo apt install -y docker.io
    sudo systemctl enable -now docker

  • On Windows/Mac
    Install Docker Desktop

  • After installation, run docker version to verify that docker is working.

3. Basic Docker Concepts

– Image

  • It is a blueprint that contains everything needed to run an app.(OS libraries, dependencies and your code)

  • They are immutable.

– Container

  • It is a running instance of an image.

  • Containers are ephemeral -> you can start,stop, delete, and recreate them anytime.

– Dockerfile

  • It is a text file with instructions to build a custom image.

  • An example:
    FROM python:3.12
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install -r requirements.txt
    COPY . .
    CMD ["python", "app.py"]

– Docker Hub/Registry

  • A registry is where images are stored.

  • Docker hub is the default public registry and one can also have private registries.

– Docker Engine

  • The underlying runtime that builds and runs containers.
  • Includes:

  • Deamon(dockerd) -> background services.

  • CLI(docker) -> command-line tool.

  • API -> programmatic access.

4. Basic Docker Commands

  • List images docker images
  • List running containers docker ps
  • Run a container docker run -d -p 8080:80 nginx
  • Then visit http://localhost:8080 in your browser
  • Stop a container docker stop <container id>

5. What is Docker Compose

  • Docker Compose lets you define and run multi-container applications.
  • Instead of starting containers one by one, you define everything in a docker-compose.yml file and run: docker compose up

6. Install Docker Compose

  • If you installed Docker Desktop, Compose is already included.
  • On Linux: sudo apt install docker-compose-plugin docker compose version

7. Useful Docker compose Commands

  • Start containers:
    docker compose up -d

  • Stop containers:
    docker compose down

  • View logs:
    docker compose logs -f

  • Restart a service:
    docker compose restart web


This content originally appeared on DEV Community and was authored by Sarah Muriithi