Container runtime essentials – build, run, manage, and orchestrate All commands assume Docker Engine is installed and running. Use sudo if not in docker group: sudo usermod -aG docker $USER → relogin.
1. Setup & Info
Task
Command
Check version
docker --version
Docker info
docker info
Test installation
docker run hello-world
Enable/start Docker
sudo systemctl enable --now docker
Add user to docker group
sudo usermod -aG docker $USER
View Docker disk usage
docker system df
2. Images
Action
Command
Search image
docker search nginx
Pull image
docker pull nginx:latest
List images
docker images or docker image ls
Remove image
docker rmi nginx
Remove dangling
docker image prune
Remove all unused
docker image prune -a
Build from Dockerfile
docker build -t myapp:1.0 .
Tag image
docker tag myapp:1.0 username/myapp:1.0
Push to registry
docker push username/myapp:1.0
Inspect image
docker image inspect nginx
3. Containers
Action
Command
Run container
docker run nginx
Run detached
docker run -d --name web nginx
Run with port
docker run -d -p 8080:80 --name web nginx
Run interactive
docker run -it ubuntu bash
List running
docker ps
List all (incl. stopped)
docker ps -a
Stop container
docker stop web
Start stopped
docker start web
Restart
docker restart web
Remove container
docker rm web
Remove all stopped
docker container prune
Attach to running
docker attach web
Exec into container
docker exec -it web bash
View logs
docker logs web
Follow logs
docker logs -f web
Stats (CPU/RAM)
docker stats
Inspect container
docker inspect web
4. Volumes & Bind Mounts
Type
Command
Volume (managed)
docker run -v myvol:/data ubuntu
Create volume
docker volume create myvol
List volumes
docker volume ls
Inspect volume
docker volume inspect myvol
Remove volume
docker volume rm myvol
Bind mount
docker run -v /host/path:/container/path ubuntu
Read-only
docker run -v /data:/app:ro nginx
5. Networks
Action
Command
List networks
docker network ls
Create bridge
docker network create mynet
Run in network
docker run --network mynet --name db -d mysql
Connect container
docker network connect mynet web
Disconnect
docker network disconnect mynet web
Inspect
docker network inspect bridge
6. Dockerfile Essentials
# syntax: docker/dockerfile:1
FROM ubuntu:22.04
LABEL maintainer="you@example.com"
# Update & install
RUN apt update && apt install -y curl vim
# Copy files
COPY app.py /app/
ADD https://example.com/file.tar.gz /tmp/
# Set workdir
WORKDIR /app
# Expose port
EXPOSE 8000
# Environment
ENV DEBUG=true
# Run command
CMD ["python3", "app.py"]
# Remove all stopped containers, unused networks, dangling images
docker system prune -a
# Remove all containers
docker rm -f $(docker ps -aq)
# Remove all images
docker rmi -f $(docker images -q)
# Remove all volumes
docker volume prune
10. Docker Hub & Registry
Action
Command
Login
docker login
Logout
docker logout
Push
docker push username/app:tag
Run from private
docker run username/app:tag
11. Debug & Inspect
Tool
Command
Container processes
docker top web
Resource usage
docker stats
Events stream
docker events
Image history
docker history nginx
Diff (changed files)
docker diff web
Export container
docker export web > web.tar
Import as image
cat web.tar | docker import - newimage
12. One-Liners
# Run Ubuntu with bash
docker run -it --rm ubuntu bash
# Quick web server
docker run -d -p 80:80 --name web --restart always nginx
# Shell into running container
docker exec -it web sh
# Build and run in one line
docker build -t myapp . && docker run -p 5000:5000 myapp
# Cleanup everything
docker system prune -a --volumes
# Monitor logs + stats
docker stats & docker logs -f web
13. Best Practices
Use official images when possible
Use specific tags (nginx:1.25), avoid :latest in prod