Day 20 Task: Docker for DevOps Engineers

Faizan ShaikhFaizan Shaikh
4 min read

Docker & Docker Compose: The Ultimate Command Cheat-Sheet ๐Ÿš€

Introduction

Docker and Docker Compose are powerful tools that have revolutionized software development and DevOps workflows. As a DevOps engineer, mastering Docker is essential for managing containers, ensuring seamless deployments, and maintaining high scalability. In this blog, Iโ€™ve compiled a comprehensive cheat-sheet of Docker and Docker Compose commands in a structured, easy-to-understand table format. This will serve as a handy reference for your everyday tasks.


๐Ÿณ Docker Commands

CommandDescriptionExample
docker --versionCheck Docker version installed on your system.docker --version
docker pull <image-name>Download an image from Docker Hub.docker pull nginx
docker imagesList all available Docker images on your machine.docker images
docker psList all running containers.docker ps
docker ps -aList all containers (running and stopped).docker ps -a
docker run <image-name>Run a Docker container.docker run nginx
docker run -it <image-name> /bin/bashRun a container in interactive mode.docker run -it ubuntu /bin/bash
docker run -d <image-name>Run a container in detached mode (in the background).docker run -d nginx
docker stop <container-id>Stop a running container.docker stop 123abc
docker start <container-id>Start a stopped container.docker start 123abc
docker rm <container-id>Remove a stopped container.docker rm 123abc
docker rmi <image-id>Remove an image from your system.docker rmi abc123
docker exec -it <container-id> /bin/bashAccess a running container in interactive mode.docker exec -it 123abc /bin/bash
docker logs <container-id>Check logs of a running container.docker logs 123abc
docker inspect <container-id>View detailed information about a container.docker inspect 123abc
docker build -t <image-name> .Build an image from a Dockerfile.docker build -t myapp .
docker commit <container-id> <new-image-name>Create a new image from a containerโ€™s state.docker commit 123abc newimage
docker network lsList all Docker networks.docker network ls
docker volume lsList all Docker volumes.docker volume ls
docker system pruneRemove all unused containers, networks, images, and build cache.docker system prune

๐Ÿ”„ Docker Compose Commands

CommandDescriptionExample
docker-compose --versionCheck Docker Compose version installed.docker-compose --version
docker-compose buildBuild images for services defined in docker-compose.yml.docker-compose build
docker-compose upStart services defined in docker-compose.yml.docker-compose up
docker-compose up -dStart services in detached mode.docker-compose up -d
docker-compose psList containers for the services defined in docker-compose.yml.docker-compose ps
docker-compose stopStop all running services.docker-compose stop
docker-compose restartRestart the services.docker-compose restart
docker-compose logsView logs of running services.docker-compose logs
docker-compose exec <service-name> <command>Execute a command in a running service container.docker-compose exec web /bin/bash
docker-compose downStop and remove containers, networks, and volumes created by docker-compose up.docker-compose down
docker-compose down --volumesRemove containers, networks, and volumes defined in the docker-compose.yml.docker-compose down --volumes

๐ŸŒ Network & Volume Management Commands

CommandDescriptionExample
docker network create <network-name>Create a custom network.docker network create mynet
docker network connect <network-name> <container-id>Connect a container to a network.docker network connect mynet 123abc
docker volume create <volume-name>Create a Docker volume.docker volume create myvol
docker volume rm <volume-name>Remove a Docker volume.docker volume rm myvol

๐Ÿ›  Additional Handy Docker Tips

CommandDescriptionExample
docker run --name <custom-name> <image-name>Run a container with a custom name.docker run --name myapp nginx
docker run -p <host-port>:<container-port> <image-name>Expose a container port to the host.docker run -p 8080:80 nginx
docker-compose up --buildBuild and start the services.docker-compose up --build

Conclusion

This cheat-sheet is a quick reference guide for Docker and Docker Compose commands, designed to help you manage containers, networks, and volumes effectively. As you continue to build your DevOps skills, this will become an invaluable resource for container orchestration and deployment strategies. ๐Ÿš€

Feel free to bookmark this blog or print the table out for quick access.

#Happy Docker-ing! ๐Ÿ˜Š

#HappyLearning

0
Subscribe to my newsletter

Read articles from Faizan Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faizan Shaikh
Faizan Shaikh