Skip to content

Docker CLI Deep Dive

Terminal window
docker run --name my-app nginx
# Without --name, Docker assigns random names like "vibrant_curie"
Terminal window
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
Terminal window
docker run -d nginx
# Returns container ID, terminal remains free
Terminal window
docker run -it ubuntu bash
# Opens a bash shell inside the container
# -i = interactive (keep STDIN open)
# -t = allocate a pseudo-TTY (terminal)
Terminal window
docker run --rm ubuntu echo "Hello World"
# Container is automatically deleted after the command finishes
Terminal window
docker run -v /host/path:/container/path nginx
docker run -v my-volume:/data postgres
Terminal window
docker run --network my-network nginx
Terminal window
docker run -e NODE_ENV=production node-app
docker run -e MYSQL_ROOT_PASSWORD=secret mysql

Terminal window
# Run MongoDB: named, detached, with port, env var, and volume
docker 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 stop
docker run \
--name my-api \
-d \
-p 3000:3000 \
-e NODE_ENV=production \
--rm \
my-api-image
# Run Ubuntu interactively and auto-remove
docker run -it --rm ubuntu bash