Covered topics: Working with Images, Running Containers, Managing Containers, Logs and Debugging.
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!