Click any command to expand the explanation and examples.
๐ฆ Containers
docker run essential
Create and start a container.
docker run nginx # run in foreground docker run -d nginx # run in background (detached) docker run -d -p 8080:80 nginx # map port 8080 โ 80 docker run -d --name web nginx # give it a name docker run -it ubuntu bash # interactive terminal docker run --rm nginx # auto-remove when stopped docker run -v ./data:/app/data nginx # mount a volume docker run -e DB_HOST=localhost nginx # set environment variable docker run --env-file .env nginx # load env from file
docker ps daily
List running containers.
docker ps # running containers docker ps -a # all containers (including stopped) docker ps -q # only IDs (useful for scripting)
docker stop / start / restart daily
Control container lifecycle.
docker stop web # graceful stop docker start web # start a stopped container docker restart web # restart docker kill web # force stop docker stop $(docker ps -q) # stop ALL running containers
docker rm daily
Remove containers.
docker rm web # remove stopped container docker rm -f web # force remove (even if running) docker rm $(docker ps -aq) # remove ALL containers docker container prune # remove all stopped containers
docker logs daily
View container output.
docker logs web # all logs docker logs -f web # follow (like tail -f) docker logs --tail 50 web # last 50 lines docker logs --since 1h web # last hour
docker exec daily
Run a command inside a running container.
docker exec web ls /app # run a command docker exec -it web bash # open a shell docker exec -it web sh # if bash isn't available
๐ผ๏ธ Images
docker build essential
Build an image from a Dockerfile.
docker build . # build from current directory docker build -t myapp:latest . # tag the image docker build -t myapp:v1.0 . # version tag docker build -f Dockerfile.prod . # use specific Dockerfile docker build --no-cache . # rebuild without cache
docker images daily
List local images.
docker images # list all images docker images -q # only IDs docker image ls # same thing
docker pull / push useful
Download or upload images.
docker pull nginx # download from Docker Hub docker pull nginx:1.25 # specific version docker push myuser/myapp:latest # push to registry
docker rmi cleanup
Remove images.
docker rmi nginx # remove an image docker rmi $(docker images -q) # remove ALL images docker image prune # remove unused images docker image prune -a # remove ALL unused images
๐พ Volumes
docker volume data
Manage persistent data.
docker volume create mydata # create a volume docker volume ls # list volumes docker volume inspect mydata # details docker volume rm mydata # remove docker volume prune # remove unused volumesUse in run command
docker run -v mydata:/app/data nginx docker run -v $(pwd):/app nginx # bind mount current dir
๐ Docker Compose
docker compose up essential
Start all services defined in
docker-compose.yml.
docker compose up # start (foreground) docker compose up -d # start (background) docker compose up --build # rebuild images first docker compose up -d --force-recreate # recreate containers
docker compose down essential
Stop and remove containers.
docker compose down # stop and remove docker compose down -v # also remove volumes docker compose down --rmi all # also remove images
docker compose logs / exec / ps daily
Manage running services.
docker compose logs # all service logs docker compose logs -f web # follow specific service docker compose exec web bash # shell into a service docker compose ps # list running services docker compose restart web # restart a service
๐งน Cleanup
docker system prune cleanup
The nuclear option โ clean up everything unused.
docker system prune # remove unused containers, networks, images docker system prune -a # also remove unused images (not just dangling) docker system prune -a --volumes # remove EVERYTHING unused docker system df # see disk usageโ ๏ธ
prune -a --volumes will delete data. Make sure you don't need it.
Quick Reference
| What you want to do | Command |
|---|---|
| Run a container | docker run -d -p 8080:80 nginx |
| List running containers | docker ps |
| Stop a container | docker stop <name> |
| View logs | docker logs -f <name> |
| Shell into container | docker exec -it <name> bash |
| Build an image | docker build -t myapp . |
| Start Compose services | docker compose up -d |
| Stop Compose services | docker compose down |
| Clean up everything | docker system prune -a |