Last Updated On
Docker provides the ability to package and run an application in a loosely isolated environment called a container.
This cheatsheet provides a comprehensive and practical reference for common Docker commands. It covers images, containers, volumes, networks, Docker Compose, command combos, and more.
Table of Contents
Docker Basics
Below are the basic commands used for Docker.
1.To get installed docker version
docker --version
2. Log in to Docker Hub or a registry
docker login
3. Log out from a registry
docker logout
4. Start the docker daemon
docker -d
5. Display system-wide information
docker info
Image Management
Docker images are a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings
1.List local images
docker images
2. Build an Image from a Dockerfile
docker build -t [image_name] .
3. Build an Image from a Dockerfile without the cache
docker build -t [image_name] . –no-cache
4. Download an image from Docker Hub/registry
docker pull [image]
5. Tag an image
docker tag [image] [new_image]
6. Delete an Image
docker rmi [image_id]
7. Remove all unused images
docker image prune
Container Management
A container is a runtime instance of a docker image. A container will always run the same, regardless of the infrastructure.
Commands for running, stopping, inspecting, and managing containers.
1.List running containers
docker ps
2. List all containers (including stopped ones)
docker ps -a
3. Run a container from an image
docker run [image]
4. List all containers (including stopped ones)
docker ps -a
5. Run a container from an image
docker run [image]
6. Create and run a container from an image, with a custom name
docker run --name [container_name] [image_name]
7. Run a container in detached mode
docker run -d --name [container_name] [image_name]
8. Assign a name to the container
docker run --name [name] [image]
9. Open a shell inside a running container
docker exec -it [container_name] sh
10. Stop a running container
docker stop [container_name] (or [container-id])
11. Start a stopped container
docker start [container_name] (or [container-id])
12. Restart a container
docker restart [container_name] (or [container-id])
13. Remove a stopped container
docker rm [container_name]
14. View container logs
docker logs [container_name] (or [container-id])
15. To inspect a running container
docker inspect [container_name] (or [container-id])
16. Copy files from container to host
docker cp [container]:/src /dest
Docker Compose
Commands for managing multi-container applications with Docker Compose.
1.Start services defined in docker-compose.yml
docker-compose up
2. Start services defined in docker-compose.yml in detached mode
docker-compose up -d
3. Stop and remove containers, networks, images, and volumes
docker-compose down
4. Build or rebuild services
docker-compose build
5. View output from services
docker-compose logs
6. Run a command in a running service container
docker-compose exec [service] bash
Volume
Commands for creating, inspecting, and mounting Docker volumes.
1.List all volumes
docker volume ls
2. Create a new volume
docker volume create [name]
3. View details of a volume
docker volume inspect [name]
4. Remove a volume
docker volume rm [name]
5. Mount a volume in a container
docker run -v [vol]:/data [image]
Networks
Commands for creating, inspecting, and managing Docker networks.
1.List all networks
docker network ls
2. Create a new network
docker network create [name]
3. View details of a network
docker network inspect [name]
4. Remove a network
docker network rm [name]
5. Connect a container to a network
docker run --network [name] [image]
