Working with Images
docker images # list downloaded images
docker pull nginx # pull an image
docker rmi nginx # remove an image
docker image prune # remove unused images
Running Containers
docker run hello-world # run and stop
docker run -it ubuntu bash # interactive mode
docker run -d nginx # run in background (detached)
docker run -d -p 8080:80 nginx # with port mapping
docker run -d --name my-nginx nginx # with a name
docker run -d -e KEY=value nginx # with an environment variable
docker run --rm -it ubuntu bash # remove after stop
Flags:
- -d — detached mode (background)
- -it — interactive terminal
- -p host:container — port mapping
- -e KEY=value — environment variable
- --name — custom name
- --rm — auto-remove on stop
Managing Containers
docker ps # running containers
docker ps -a # all, including stopped
docker stop my-nginx # stop
docker start my-nginx # start an existing container
docker restart my-nginx # restart
docker rm my-nginx # remove (must be stopped)
docker rm -f my-nginx # force remove
docker container prune # remove all stopped containers
Logs and Debugging
docker logs my-nginx # all logs
docker logs -f my-nginx # follow logs in real time
docker logs --tail 50 my-nginx # last 50 lines
docker exec my-nginx ls /etc/nginx # run a command
docker exec -it my-nginx bash # get a shell inside
docker inspect my-nginx # full container info
docker stats # resource usage
docker stats --no-stream # resource snapshot
Cleanup
docker system prune # remove all unused resources
docker system prune -a # including images with no containers
docker system df # how much disk Docker is using
Lifecycle Cheat Sheet
docker pull → image downloaded
docker run → container created and started
docker stop → container stopped (data preserved)
docker start → container started again
docker rm → container removed
docker rmi → image removed
Useful Aliases
Add to ~/.zshrc or ~/.bashrc:
alias dps='docker ps'
alias dpsa='docker ps -a'
alias dlog='docker logs -f'
alias dex='docker exec -it'
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!