Docker

Main components

Image
A blueprint or template
Read-only
prebuilt images are available in a public registry (DockerHub)
Container
A running instance of an image
Lightweight, fast, disposable
You can start, stop, delete it anytime
Dockerfile A recipe that tells Docker how to build an image
Docker Engine The actual software that builds images and runs containers
Install Docker & Docker Compose
On Ubuntu / Debian Linux
System update
Install Docker
Start Docker service
Check Docker and Docker Compose installed versions:
Run Docker without sudo
sudoLog 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
Install Docker Desktop
Enable WSL 2
Dockerfile
Plain text file that contains instructions telling Docker how to build an image.
The instructions are usually in this order:
FROMWORKDIRCOPY/ADDRUNEXPOSECMD/ENTRYPOINT
FROM
FROMBase image (e.g.
python:3.11)Every Dockerfile starts with this (except
scratch)Example: Ubuntu, Node, Java, Python
WORKDIR
WORKDIRSets the working directory inside the container
Like
cd /appRecommended instead of
RUN cd ...
COPY
COPYCopies files from your machine into the image
First path = host
Second path = container
ADD
ADDSimilar to COPY, but more powerful
Can extract archives
Can download URLs
RUN
RUNExecutes commands at build time
Creates a new image layer
Used for installing dependencies
EXPOSE
EXPOSEDocuments which port the app uses
Does NOT publish the port
Used mainly for readability and tools
ENV
ENVSets environment variables
Available inside the container
CMD
CMDDefault command when container starts
Can be overridden with
docker run
ENTRYPOINT
ENTRYPOINTFixed 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
-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