Docker

Main components

  1. Image

    • A blueprint or template

    • Read-only

    • prebuilt images are available in a public registry (DockerHub)

  2. Container

    • A running instance of an image

    • Lightweight, fast, disposable

    • You can start, stop, delete it anytime

  3. Dockerfile A recipe that tells Docker how to build an image

  4. Docker Engine The actual software that builds images and runs containers

Install Docker & Docker Compose

On Ubuntu / Debian Linux

  1. System update

  1. Install Docker

  1. Start Docker service

Check Docker and Docker Compose installed versions:

Run Docker without sudo

  • Log out and log back in, then test:

Install Docker Compose (Modern way)

Docker Compose is now built into Docker as a plugin.

If Docker Compose is missing:

macOS

Install Docker Desktop for Mac

Windows

  1. Install Docker Desktop

  2. Enable WSL 2

Dockerfile

Plain text file that contains instructions telling Docker how to build an image.

The instructions are usually in this order:

  • FROM

  • WORKDIR

  • COPY / ADD

  • RUN

  • EXPOSE

  • CMD / ENTRYPOINT

FROM

  • Base image (e.g. python:3.11)

  • Every Dockerfile starts with this (except scratch)

  • Example: Ubuntu, Node, Java, Python

WORKDIR

  • Sets the working directory inside the container

  • Like cd /app

  • Recommended instead of RUN cd ...

COPY

  • Copies files from your machine into the image

  • First path = host

  • Second path = container

ADD

  • Similar to COPY, but more powerful

  • Can extract archives

  • Can download URLs

RUN

  • Executes commands at build time

  • Creates a new image layer

  • Used for installing dependencies

EXPOSE

  • Documents which port the app uses

  • Does NOT publish the port

  • Used mainly for readability and tools

ENV

  • Sets environment variables

  • Available inside the container

CMD

  • Default command when container starts

  • Can be overridden with docker run

ENTRYPOINT

  • Fixed command

  • Arguments can be appended

  • Harder to override than CMD

Full simple example

Build & Run

  • Move to the folder containing the application and Dockerfile and build the image with <image_name> name

  • Run the container

  • Stop the container

Run command syntax

Flag
Purpose
What it does
Example

-d

Detached mode

Runs container in the background

docker run -d nginx

-it

Interactive terminal

-i keeps STDIN open, -t allocates a TTY (great for shells/debugging)

docker run -it ubuntu bash

--name

Container name

Assigns a custom name to the container

docker run --name my-nginx nginx

-p

Port mapping

Maps host port → container port

docker run -p 8080:80 nginx

-v

Volumes / bind mounts

Persists data or shares files between host and container

docker run -v mydata:/data myapp

-v

Bind mount

Mounts a host directory into the container

docker run -v /host/path:/container/path myapp

-e

Environment variables

Sets env vars inside the container

docker run -e DB_HOST=localhost myapp

-e (multiple)

Multiple env vars

Passes multiple env vars

docker run -e A=1 -e B=2 myapp

--rm

Auto-remove

Deletes container after it exits (one-off runs)

docker run --rm busybox echo "hi"

--restart

Restart policy

Controls container restart behavior (e.g. no by default, on-failure, always, unless-stopped)

docker run --restart unless-stopped nginx

--network

Networking

Connects container to a custom network

docker run --network mynet myapp

--env-file

Env vars from file

Loads env vars from a file (keeps secrets out of CLI history)

docker run --env-file .env myapp

--cpus

CPU limit

Limits CPU usage

docker run --cpus="1.5" myapp

--memory

Memory limit

Limits RAM usage

docker run --memory="512m" myapp

--user

Run as non-root

Runs container as a specific UID/GID

docker run --user 1000:1000 myapp

-w, --workdir

Working directory

Sets working directory inside container

docker run -w /app myimage

--entrypoint

Override entrypoint

Replaces image entrypoint (useful for debugging)

docker run --entrypoint bash myimage

Last updated