docker — Container Build & Operation
💬 "Docker commands"
Docker bundles a program and the libraries and settings needed to run it into a read-only package called an image, then runs it as a container that behaves the same anywhere.
It is a go-to tool for reducing the “works on my machine but not on the server” problem, and it also lets you quickly spin up supporting services such as log collectors, databases, and web servers as containers.
- Image: a read-only template containing the runtime environment and code
- Container: a running instance that actually launches an image
- Dockerfile: a recipe for building an image (install, copy, and run commands)
- Compose: defines and starts multiple containers together in a single YAML file
Commonly used commands
| Command | Description |
|---|---|
| docker build -t <name>:<tag> . | Builds an image from the Dockerfile in the current folder |
| docker ps / docker ps -a | List running containers / all containers |
| docker logs -f <container> | Follow container logs in real time |
| docker exec -it <container> sh | Enter a running container |
| docker compose up -d | Start in the background according to the Compose definition |
| docker compose down | Clean up containers and networks started with Compose |
Three essential command combinations
docker build -t app:1.0 . && docker run -p 8080:80 app:1.0— Build, then verify locally right awaydocker compose up -d && docker compose logs -f— Start a bundle of services and check logsdocker images && docker system df— Check remaining images and disk usage
💬 "Create an example first response for when you get an incident alert"
Below is a step-by-step summary of situations you often encounter on production servers.
Check a web server with a container
Pull the official image and run it on port 80.
docker run --rm -d --name web -p 8080:80 nginx:alpine
docker ps
Logs and container access
Check the logs to see if responses are coming in, then enter the container.
docker logs web
docker exec -it web sh
Cleanup
When you are done, clean up the container and unused images.
docker stop web
docker image prune -f
How it's used in practice
1. Reproduce the server environment locally
Situation — You can't test locally because of server-only libraries.
docker run --rm -it -v "$PWD":/work -w /work ubuntu:24.04 bash
Result — You can run commands inside an ubuntu:24.04 container with the current folder mounted.
Key point — If you connect a folder with -v, files remain even after the container is deleted.
2. Temporarily start a database in a container
Situation — You need a test database for a short time without installing it on the server.
docker run --rm -d --name testdb -e MYSQL_ROOT_PASSWORD=dev mysql:8
docker logs -f testdb
Result — Even without a Compose file, you can start a database with environment variables and check its readiness through logs.
Key point — --rm automatically removes the container when it stops, making it convenient for temporary work.
3. Clean up when disk usage explodes
Situation — After repeated builds, disk usage has grown, so you need to clean up images.
docker system df
docker system prune -f
Result — Stopped containers, unused networks, and dangling images are cleaned up.
Key point — On a server, before pruning, check with docker volume ls whether there are any volumes you still need.
Important notes
- For image tags, specify a version instead of latest so you can tell what it contains.
- When passing secrets into a container, use environment variables or secret files rather than baking them into the image.
- Host networking (--network host) can cause port conflicts and firewall bypass issues, so use it only when necessary.
Actual execution screen
This is an illustrative example of checking the container list and disk usage on a server (not actual command output).
!Example of Docker container list and system disk usage check
Production server
Source (HTML): https://games.aidebatehub.com/programs/dev/container/docker.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments