Docker CLI Deep Dive
7. Docker CLI Deep Dive
Section titled “7. Docker CLI Deep Dive”🔧 Essential Flags Explained
Section titled “🔧 Essential Flags Explained”--name — Give your container a name
Section titled “--name — Give your container a name”docker run --name my-app nginx# Without --name, Docker assigns random names like "vibrant_curie"-p — Port mapping (host:container)
Section titled “-p — Port mapping (host:container)”docker run -p 8080:80 nginx# Access nginx at localhost:8080, which maps to container port 80
docker run -p 3000:3000 node-app# Both host and container use port 3000-d — Detached mode (run in background)
Section titled “-d — Detached mode (run in background)”docker run -d nginx# Returns container ID, terminal remains free-it — Interactive terminal
Section titled “-it — Interactive terminal”docker run -it ubuntu bash# Opens a bash shell inside the container# -i = interactive (keep STDIN open)# -t = allocate a pseudo-TTY (terminal)--rm — Auto-remove after exit
Section titled “--rm — Auto-remove after exit”docker run --rm ubuntu echo "Hello World"# Container is automatically deleted after the command finishes-v — Volume mount
Section titled “-v — Volume mount”docker run -v /host/path:/container/path nginxdocker run -v my-volume:/data postgres--network — Connect to a network
Section titled “--network — Connect to a network”docker run --network my-network nginx-e — Set environment variable
Section titled “-e — Set environment variable”docker run -e NODE_ENV=production node-appdocker run -e MYSQL_ROOT_PASSWORD=secret mysql🛠️ Combining Flags — Real Examples
Section titled “🛠️ Combining Flags — Real Examples”# Run MongoDB: named, detached, with port, env var, and volumedocker run \ --name mongodb \ -d \ -p 27017:27017 \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=password \ -v mongo-data:/data/db \ mongo:6
# Run Node.js app: named, detached, with port, env, and remove on stopdocker run \ --name my-api \ -d \ -p 3000:3000 \ -e NODE_ENV=production \ --rm \ my-api-image
# Run Ubuntu interactively and auto-removedocker run -it --rm ubuntu bash