Docker
Run a Container
Run, list, stop, and remove Docker containers.
By EZ4Code Team
containerrunbasics
Code
# Run an image in foreground with port mapping
docker run -p 8080:80 --name web nginx:alpine
# Run in detached mode with env vars and volume
docker run -d \
--name app \
-p 3000:3000 \
-e NODE_ENV=production \
-v $(pwd)/data:/app/data \
node:20-alpine
# List running / all containers
docker ps
docker ps -a
# Stop and remove
docker stop web
docker rm web
docker rm -f $(docker ps -aq) # remove all containersExplanation
docker run creates a writable layer over the image and starts a process. Use -d for detached mode, -p to publish ports, -e for environment variables, and -v to mount host paths. docker ps lists containers, while stop/rm manage their lifecycle.
More Docker Snippets
Writing a Dockerfile
Build an image from a Dockerfile with multi-instruction layers.
Image Management
Pull, list, tag, push, and prune images.
Volumes & Bind Mounts
Persist data with named volumes, anonymous volumes, and bind mounts.
Networking
Create networks, attach containers, and expose ports.
Docker Compose
Define and run multi-container apps with compose.
Multi-Stage Build
Build artifacts in one stage and copy them into a slim final image.