This tutorial is a part of our Free DevOps Tutorial Series which describes docker management commands cheat sheet. If you are looking for DevOps Training in Kolkata you can contact us for detailed DevOps Course Modules and available DevOps Projects.
What is Docker?
Docker is a platform and toolset that allows you to develop, deploy, and run applications in lightweight, portable containers. These containers are isolated environments that package an application and its dependencies, including libraries and configuration files, into a single, consistent unit.
Here are some key aspects of Docker:
- Containerization: Docker uses containerization technology to create containers, which are instances of isolated, lightweight environments. These containers share the same OS kernel but run independently of one another. This makes it possible to run multiple applications or services on the same host without conflicts.
- Portability: Docker containers are highly portable and can run consistently across different environments, such as development laptops, testing servers, and production servers. This eliminates the “it works on my machine” problem and streamlines application deployment.
- Version Control: Docker uses images as a template for containers. Images are versioned and can be stored in repositories, similar to version control for source code. This enables teams to manage and distribute application images with ease.
- Efficiency: Containers are lightweight and have a minimal overhead compared to traditional virtual machines (VMs). They start quickly, use fewer resources, and allow for efficient resource utilization on the host system.
- Isolation: Containers provide process and file system isolation, which enhances security and ensures that applications and services do not interfere with each other.
- Orchestration: Docker can be used in conjunction with container orchestration tools like Kubernetes and Docker Swarm to manage and scale containerized applications across clusters of machines.
Docker has become a popular choice for DevOps and software development teams because it simplifies application deployment, improves collaboration between developers and operations teams, and enhances the overall development and deployment workflow. It has revolutionized the way software is packaged and delivered, making it easier to build, ship, and run applications in various computing environments.
Docker Management Cheat Sheet:
Docker Basics:
Command | Description | Example |
---|---|---|
docker --version |
Check Docker version | docker --version |
docker pull <image> |
Pull an image from Docker Hub | docker pull ubuntu:latest |
docker images |
List locally available images | docker images |
docker rmi <image> |
Remove an image | docker rmi my_image:1.0 |
docker run <options> <image> |
Create and start a container from an image | docker run -d --name my_container ubuntu:latest |
docker ps |
List running containers | docker ps |
docker ps -a |
List all containers (including stopped ones) | docker ps -a |
docker start <container> |
Start a stopped container | docker start my_container |
docker stop <container> |
Stop a running container | docker stop my_container |
docker restart <container> |
Restart a container | docker restart my_container |
docker rm <container> |
Remove a stopped container | docker rm my_container |
docker exec -it <container> <command> |
Execute a command in a running container | docker exec -it my_container bash |
docker logs <container> |
View container logs | docker logs my_container |
Image Management:
Command | Description | Example |
---|---|---|
docker build -t <tag> <path> |
Build an image from a Dockerfile | docker build -t my_image:1.0 ./my_app |
docker push <image> |
Push an image to Docker Hub | docker push my_image:1.0 |
docker tag <source-image> <target-image> |
Create a new tag for an image | docker tag my_image:1.0 my_image:latest |
docker login |
Log in to a Docker registry | docker login docker.io |
docker logout |
Log out from a Docker registry | docker logout docker.io |
Container Interaction:
Command | Description | Example |
---|---|---|
docker exec -it <container> <command> |
Execute a command in a running container | docker exec -it my_container bash |
docker logs <container> |
View container logs | docker logs my_container |
Network and Volume Management:
Command | Description | Example |
---|---|---|
docker network ls |
List Docker networks | docker network ls |
docker volume ls |
List Docker volumes | docker volume ls |
docker network create <network> |
Create a custom Docker network | docker network create my_network |
docker volume create <volume> |
Create a named Docker volume | docker volume create my_volume |
Docker Compose:
Command | Description | Example |
---|---|---|
docker-compose --version |
Check Docker Compose version | docker-compose --version |
docker-compose up |
Start services defined in a Compose file | docker-compose up -d |
docker-compose down |
Stop and remove services defined in a Compose file | docker-compose down |
docker-compose logs <service> |
View logs for a specific service in Compose file | docker-compose logs web_service |
Docker Swarm:
Command | Description | Example |
---|---|---|
docker swarm init |
Initialize a Docker Swarm | docker swarm init |
docker swarm join-token <worker/manager> |
Generate token for worker/manager to join Swarm | docker swarm join-token worker |
docker service ls |
List services in a Docker Swarm | docker service ls |
docker service create <options> <image> |
Create a service in a Docker Swarm | docker service create --replicas 3 --name my_service my_image:1.0 |
docker service scale <service>=<replicas> |
Scale a service in a Docker Swarm | docker service scale my_service=5 |
docker stack deploy -c <compose-file> |
Deploy a stack in a Docker Swarm | docker stack deploy -c my_stack.yml my_stack |
docker stack ls |
List stacks in a Docker Swarm | docker stack ls |
docker stack rm <stack> |
Remove a stack in a Docker Swarm | docker stack rm my_stack |
System Maintenance:
Command | Description | Example |
---|---|---|
docker info |
Display Docker system information | docker info |
docker system prune |
Remove all stopped containers and unused data | docker system prune -a |
docker-compose -f <file> config |
Validate a Compose file | docker-compose -f my-compose.yml config |
Feel free to use these sections for easier reference to Docker commands and their purposes.