Docker Basics
Basic Docker and Docker Compose concepts explained in a public-safe way.
Docker runs applications in containers. Containers are isolated environments that include an application and the things it needs to run.
What is Docker?
Docker is a tool for running applications in containers.
A container is not the same as a full virtual machine. It shares the host system kernel, but runs the application in an isolated environment.
Common uses:
web applications
databases
test environments
self-hosted services
development tools
background workers
What is an image?
An image is a template used to create a container.
Example:
nginx image
postgres image
redis image
application image
Pull an image:
docker pull IMAGE_NAME
Example:
docker pull nginx
What is a container?
A container is a running instance of an image.
Show running containers:
docker ps
Show all containers, including stopped ones:
docker ps -a
Run a simple container
Example:
docker run hello-world
Run nginx in the background:
docker run -d --name web-test -p 8080:80 nginx
This means:
-d = run in background
--name = container name
-p 8080:80 = host port 8080 maps to container port 80
nginx = image name
Test:
curl -I http://localhost:8080
Stop and remove:
docker stop web-test
docker rm web-test
Docker Compose
Docker Compose uses a YAML file to define one or more containers.
Common file name:
docker-compose.yml
or:
compose.yml
Start services:
docker compose up -d
Stop services:
docker compose down
Show logs:
docker compose logs
Follow logs:
docker compose logs -f
Simple Compose example
services:
web:
image: nginx:latest
container_name: web-test
ports:
- "8080:80"
restart: unless-stopped
Start:
docker compose up -d
Check:
docker ps
curl -I http://localhost:8080
Stop:
docker compose down
Ports
Ports expose a service from a container to the host.
Example:
ports:
- "8080:80"
Meaning:
host port 8080 โ container port 80
Then access:
http://localhost:8080
Volumes
Volumes store data outside the container.
This matters because containers can be deleted and recreated.
Example:
volumes:
- ./data:/data
Meaning:
./data on host โ /data inside container
Use volumes for:
database data
uploads
configuration
persistent application files
Environment variables
Environment variables pass settings into containers.
Example:
environment:
- TZ=Europe/Prague
- APP_ENV=production
Do not put real secrets in public examples.
Restart policy
Common restart policies:
no
always
unless-stopped
on-failure
Example:
restart: unless-stopped
Meaning:
restart container unless it was manually stopped
Container logs
Show logs for one container:
docker logs CONTAINER_NAME
Follow logs:
docker logs -f CONTAINER_NAME
With Compose:
docker compose logs -f SERVICE_NAME
Execute command inside container
docker exec -it CONTAINER_NAME bash
If bash is not available:
docker exec -it CONTAINER_NAME sh
Stop and remove containers
Stop:
docker stop CONTAINER_NAME
Remove:
docker rm CONTAINER_NAME
Stop and remove Compose stack:
docker compose down
Pull updates
Pull newer images:
docker compose pull
Recreate containers:
docker compose up -d
Check:
docker ps
docker compose logs
Docker networks
Docker creates networks so containers can talk to each other.
List networks:
docker network ls
Inspect network:
docker network inspect NETWORK_NAME
In Compose, services can usually reach each other by service name.
Example:
database service name: db
application can connect to: db
Docker volumes
List volumes:
docker volume ls
Inspect volume:
docker volume inspect VOLUME_NAME
Remove unused volumes:
docker volume prune
Be careful: removing volumes can delete persistent data.
Disk usage
Show Docker disk usage:
docker system df
Remove unused data:
docker system prune
Dangerous version:
docker system prune -a --volumes
This can remove images, containers, networks, and volumes that may contain data.
First command set
docker ps
docker ps -a
docker compose ps
docker compose logs -f
docker images
docker network ls
docker volume ls
docker system df
Common troubleshooting
Container is not running
docker ps -a
docker logs CONTAINER_NAME
With Compose:
docker compose ps
docker compose logs SERVICE_NAME
Port is already in use
Check listening ports:
ss -tulpn | grep :PORT
Then change the host port in Compose.
Example:
ports:
- "8081:80"
Container restarts repeatedly
Check logs:
docker logs CONTAINER_NAME
or:
docker compose logs SERVICE_NAME
Look for:
permission errors
missing config
wrong environment variables
database connection problems
port conflicts
Dangerous actions
Be careful with:
docker rm
docker volume rm
docker volume prune
docker system prune -a --volumes
docker compose down -v
deleting bind mount folders
changing volume paths
These can delete data.