Docker Basics Cheat Sheet
"Passionate about simplifying the complexities of DevOps, I bring 5 years of hands-on experience in supporting development and operations teams to achieve faster, more reliable software delivery. I thrive on optimizing CI/CD pipelines, automating workflows, and troubleshooting challenges across diverse cloud environments. My expertise spans across infrastructure management, continuous integration, and performance monitoring—ensuring stability and efficiency at every stage of the software lifecycle. Always eager to learn, collaborate, and innovate, I’m committed to driving impactful change in the DevOps space."
1. Core Concepts
Docker: A platform to develop, ship, and run applications in isolated containers.
Container: A lightweight, portable package that includes an application and its dependencies, ensuring consistent performance regardless of the environment.
Image: A read-only template used to create containers. Images are built from a series of instructions in a
Dockerfile.Dockerfile: A file with a list of instructions on how to build a Docker image.
Registry: A repository to store Docker images (e.g., Docker Hub, Google Container Registry).
Volume: A method to persist data generated by Docker containers outside of the container’s file system.
2. Basic Commands
Docker Installation & Setup
Install Docker on your OS by following the official Docker installation guide.
Start Docker service:
bashCopy codesudo systemctl start docker
Docker Version and Info
Check Docker version:
bashCopy codedocker --versionDisplay detailed Docker information:
bashCopy codedocker info
3. Working with Images
Search and Pull an Image
Search for images on Docker Hub:
bashCopy codedocker search <image-name>Download (pull) an image:
bashCopy codedocker pull <image-name>:<tag>Example:
docker pull nginx:latest
List Images
List all downloaded images:
bashCopy codedocker images
Remove an Image
Delete an image:
bashCopy codedocker rmi <image-id>
4. Running Containers
Start a Container
Run a container from an image:
bashCopy codedocker run <image-name>Run in detached mode (in the background):
bashCopy codedocker run -d <image-name>Run with a custom name:
bashCopy codedocker run --name <container-name> <image-name>Expose container port to host:
bashCopy codedocker run -p <host-port>:<container-port> <image-name>
List and Stop Containers
List all running containers:
bashCopy codedocker psList all containers (including stopped):
bashCopy codedocker ps -aStop a running container:
bashCopy codedocker stop <container-id>Start a stopped container:
bashCopy codedocker start <container-id>
Remove Containers
Remove a specific container:
bashCopy codedocker rm <container-id>Remove all stopped containers:
bashCopy codedocker container prune
5. Managing Volumes
Creating and Using Volumes
Create a volume:
bashCopy codedocker volume create <volume-name>Mount a volume when running a container:
bashCopy codedocker run -v <volume-name>:<container-path> <image-name>List all volumes:
bashCopy codedocker volume lsInspect a volume:
bashCopy codedocker volume inspect <volume-name>
6. Building Images with Dockerfile
Dockerfile Commands
FROM: Sets the base image (e.g.,
FROM ubuntu:latest).COPY: Copies files/directories from the host to the container.
RUN: Executes commands in the container (e.g.,
RUN apt-get update).CMD: Sets default command for the container (e.g.,
CMD ["nginx"]).EXPOSE: Informs Docker the container will listen on a specified port.
ENV: Sets environment variables.
Build an Image from Dockerfile
Build command:
bashCopy codedocker build -t <image-name>:<tag> <path-to-dockerfile-directory>Example:
docker build -t myapp:latest .
7. Docker Networks
List Docker networks:
bashCopy codedocker network lsCreate a network:
bashCopy codedocker network create <network-name>Attach a container to a network:
bashCopy codedocker network connect <network-name> <container-name>Disconnect a container from a network:
bashCopy codedocker network disconnect <network-name> <container-name>
8. Docker Compose
- Docker Compose: A tool for defining and running multi-container Docker applications using a
docker-compose.ymlfile.
Basic Commands for Docker Compose
Start all services defined in
docker-compose.yml:bashCopy codedocker-compose upStart in detached mode:
bashCopy codedocker-compose up -dStop all services:
bashCopy codedocker-compose downView logs:
bashCopy codedocker-compose logs
9. Docker Logs and Debugging
Viewing Logs
See logs of a container:
bashCopy codedocker logs <container-id>Follow logs in real-time:
bashCopy codedocker logs -f <container-id>
Execute Commands in a Running Container
Access the terminal of a running container:
bashCopy codedocker exec -it <container-id> /bin/bashRun a command inside a running container:
bashCopy codedocker exec <container-id> <command>
10. Docker Best Practices
Use
.dockerignore: Exclude files/folders from the image to reduce size.Optimize
Dockerfilelayers: CombineRUNcommands when possible.Use specific tags: Avoid using
latesttag in production environments.Clean up unused images and containers:
bashCopy codedocker system prune
This cheat sheet should help you with the basics of Docker, from setting up and managing containers to building images and using Docker Compose. For more advanced commands and configurations, refer to the official Docker documentation.



