Docker
Table of Contents
Platform for building, shipping, and running applications in containers. A container packages an application and all its dependencies into an isolated, portable unit that runs consistently across environments. Docker handles the image build and run lifecycle; Docker Compose handles multi-container applications; Kubernetes handles large-scale orchestration.
Related: Docker Compose Containerization Linux Containers Microservices Podman Container Escape
Core Concepts
| Term | Meaning |
|---|---|
| Image | Read-only template — the blueprint for a container |
| Container | Running instance of an image |
| Dockerfile | Script that defines how to build an image |
| Registry | Storage for images — Docker Hub is the default public registry |
| Volume | Persistent storage that survives container restarts |
| Network | Virtual network allowing containers to communicate |
Common Commands
Images
docker pull ubuntu:22.04 # pull image from registry docker images # list local images docker build -t myapp:1.0 . # build image from Dockerfile in current dir docker rmi myapp:1.0 # remove image docker tag myapp:1.0 repo/myapp:1.0 # tag for pushing docker push repo/myapp:1.0 # push to registry
Containers
docker run ubuntu:22.04 # create and start container docker run -it ubuntu:22.04 bash # interactive shell docker run -d nginx # run detached (background) docker run -d -p 8080:80 nginx # map host port 8080 to container port 80 docker run -v /host/path:/container/path nginx # mount volume docker run --name mycontainer nginx # named container docker ps # list running containers docker ps -a # all containers including stopped docker stop mycontainer # graceful stop docker kill mycontainer # immediate stop docker rm mycontainer # remove stopped container docker rm -f mycontainer # force remove running container
Interacting with Running Containers
docker exec -it mycontainer bash # shell into running container docker logs mycontainer # view stdout/stderr docker logs -f mycontainer # follow logs docker cp file.txt mycontainer:/tmp/ # copy file into container docker inspect mycontainer # full JSON config and state
Volumes
docker volume create mydata docker volume ls docker volume rm mydata docker run -v mydata:/app/data myapp # mount named volume
Cleanup
docker system prune # remove stopped containers, unused images, networks docker system prune -a # also remove unused images with no containers docker volume prune # remove unused volumes
Dockerfile Reference
FROM ubuntu:22.04 # base image WORKDIR /app # set working directory COPY requirements.txt . # copy files from host RUN pip install -r requirements.txt # run during build COPY . . # copy application code ENV PORT=8080 # set environment variable EXPOSE 8080 # document the port (does not publish) CMD ["python3", "app.py"] # default command when container starts
Layer caching
Each RUN/COPY/ADD instruction creates a layer. Docker caches layers and only rebuilds from the first changed instruction. Put frequently-changing instructions (COPY . .) last to maximise cache reuse.
Security Context
See Container Escape for breakout techniques. Key security considerations:
# Never run containers as root if avoidable docker run --user 1000:1000 myapp # Read-only filesystem — prevents writes outside volumes docker run --read-only myapp # Drop capabilities docker run --cap-drop ALL --cap-add NET_BIND_SERVICE myapp # Check what user a running container is using docker exec mycontainer whoami docker inspect mycontainer | grep -i user
Tips
- Use specific image tags rather than
:latest—:latestcan change unexpectedly and break builds .dockerignoreworks like.gitignore— use it to excludenode_modules,.git, and secrets from the build context- Multi-stage builds keep final images small — build in one stage, copy only the output to a clean final stage
docker compose(lowercase, no hyphen) is the current CLI;docker-composeis the older standalone tool — they are mostly compatible but the newer integrated version is preferred- Never store secrets in a Dockerfile — they persist in the image layer history even if overwritten in a later layer