- codingchefs
- Posted on
- No Comments
Docker Commands Cheat Sheet: A Quick Reference Guide
Docker has revolutionized software development by enabling faster builds, tests, and deployments in lightweight, portable containers. This cheat sheet will guide you through essential Docker commands, offering quick copy-and-paste solutions to streamline your Docker workflow.
Docker Concepts
Images
- Docker images are read-only templates that create containers.
- Images are created with
docker build
and stored in a Docker registry (e.g., DockerHub). - Images consist of layers from other images, optimizing resource usage.
Containers
- Containers are instances of images, providing an isolated environment to run applications.
- Containers include all dependencies needed to execute the application.
Registries & Repositories
- A Docker registry stores images; DockerHub is the default public registry.
- A repository is a collection of Docker images under the same name, differentiated by tags representing different versions.
Essential Docker Commands
Prune Unused Objects
Remove unused containers, networks, images, and volumes:
docker system prune
List and Stop Containers
List all running containers:
docker ps
Stop all running containers:
docker stop $(docker ps -a -q)Stop a specific container by ID:
docker stop {containerId}
List Docker Images
Display all local images:
docker images
Run a Container
Run a simple container in detached mode:
docker run -d busybox:1.24 sleep 1000
Run interactively with pseudo-TTY:
docker run -it busybox:1.24Remove container after execution:
docker run --rm busybox:1.24 sleep 1Run with a specific container name:
docker run --name hello_world busybox:1.24
Inspect a Container
View container details:
docker inspect {containerId}
Port Mapping and Logs
Run with port mapping:
docker run -d -it -p 8888:8080 tomcat:8.0
View container logs:
docker logs {containerId}
Image Layers and History
Display the history of an image’s layers:
docker history busybox:1.24
Building Docker Images
Build a Docker Image from a Container
Start a base image:
docker run -it debian:jessie
Make changes (e.g., install Git):
apt-get update && apt-get install -y gitCommit changes to create a new image:
docker commit {containerId} your_image_name:1.0
Building with Dockerfile
Create a Dockerfile
:
touch Dockerfile
Write Dockerfile content:
FROM debian:jessie
RUN apt-get update && apt-get install -y git vim
Build the image:
docker build -t your_image_name:1.0 .
Caching Layers
Avoid rebuilding unchanged layers:
docker build -t your_image_name:1.0 --no-cache=true .
Push to DockerHub
Tag and push the image:
docker tag {containerId} {username}/your_image_name:1.0
docker login --username={username}
docker push {username}/your_image_name:1.0
Networking
List Networks
docker network ls
Create and Connect to Bridge Network
docker network create --driver bridge my_bridge_network
docker run -d --net my_bridge_network busybox sleep 1000
Host Network Mode
Run with host network mode for better performance:
docker run --net host busybox sleep 1000
Docker-Compose Commands
Start Services
Start all services defined in docker-compose.yml
:
docker-compose up
Run in detached mode:
docker-compose up -d
View Logs
Show logs for all services:
docker-compose logs
Follow logs in real-time:
docker-compose logs -f
Stop and Remove Containers
Stop all services:
docker-compose stop
Remove stopped services:
docker-compose rm
Rebuild Services
Rebuild containers after changes:
docker-compose build
Conclusion
Docker is an indispensable tool for modern development, allowing faster and more efficient application deployment. This cheat sheet covers the essential commands to get you up and running, whether you’re building, testing, or managing containers. Save it as a reference to keep your Docker workflow smooth and productive. Happy Dockerizing!