docker run Common Parameters and Scenario Examples
The purpose of docker run can be simply understood as: based on an image, create and start a new container.
Minimal syntax:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
This page does not aim to list every parameter. It only keeps the ones I use most often and the combinations I most easily forget.
High-Frequency Parameter Quick Reference
| Parameter | Purpose | Common Example |
|---|---|---|
-d | Run in background | docker run -d nginx:stable |
-it | Interactive terminal | docker run -it ubuntu:24.04 bash |
--rm | Auto-delete container on exit | docker run --rm ubuntu:24.04 bash |
--name | Specify container name | docker run --name web nginx:stable |
-p | Publish port | docker run -p 8080:80 nginx:stable |
-e | Set environment variable | docker run -e APP_ENV=prod my-app:latest |
--env-file | Read environment variables from file | docker run --env-file .env my-app:latest |
-v / --mount | Mount data volume or host directory | docker run -v data:/data redis:7 |
-w | Specify working directory | docker run -w /app node:22 npm test |
--restart | Restart policy after container exits | docker run --restart unless-stopped ... |
--network | Specify network | docker run --network my-net redis:7 |
--cpus | Limit CPU | docker run --cpus 2 ... |
--memory | Limit memory | docker run --memory 2g ... |
--gpus all | Expose GPU to container | docker run --gpus all ... |
-u | Specify run user | docker run -u 1000:1000 ... |
Default Habits I Recommend
- For temporary debugging containers, prefer
--rm - For long-running services, prefer adding
--name - For mounts, prefer
--mountin complex scenarios - When exposing services externally, explicitly write
-prather than guessing - Long-running services typically add
--restart unless-stopped
Scenario 1: Open a Temporary Interactive Shell
docker run --rm -it ubuntu:24.04 bash
Suitable for:
- Trying commands
- Temporarily installing packages for verification
- Quickly inspecting the file structure inside an image
If the image does not have bash:
docker run --rm -it alpine:3.21 sh