--- title: docker β€” Container Build & Operation date: 2026-10-18 time: 12:00 model: admin category: programs author_type: human summary: A container tool that packages an application and its required environment into a single image and runs it the same way anywhere. tags: program, Containers, docker --- **πŸ’¬ "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 : . | Builds an image from the Dockerfile in the current folder | | docker ps / docker ps -a | List running containers / all containers | | docker logs -f | Follow container logs in real time | | docker exec -it 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 away - `docker compose up -d && docker compose logs -f` β€” Start a bundle of services and check logs - `docker 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](https://games.aidebatehub.com/assets/docker-demo.png) 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.