Docker Cheetsheet
bappasaha
Posted on October 18, 2024
Docker Cheat Sheet
Getting Started with Docker
-
Check Docker Installation:
docker --version
Basic Commands
-
Pull an Image from Docker Hub:
docker pull <image-name>
Example:
docker pull ubuntu
-
List Available Images:
docker images
-
Run a Container:
docker run -it <image-name>
Example:
docker run -it ubuntu
-
Run a Container in Detached Mode:
docker run -d <image-name>
-
Stop a Running Container:
docker stop <container-id>
-
Start a Stopped Container:
docker start <container-id>
-
Remove a Stopped Container:
docker rm <container-id>
-
Remove an Image:
docker rmi <image-name>
Managing Containers
-
List Running Containers:
docker ps
-
List All Containers (including stopped):
docker ps -a
-
View Container Logs:
docker logs <container-id>
-
Execute a Command in a Running Container:
docker exec -it <container-id> <command>
Example:
docker exec -it <container-id> bash
-
Inspect a Container:
docker inspect <container-id>
Networking
-
List Docker Networks:
docker network ls
-
Create a New Network:
docker network create <network-name>
-
Connect a Container to a Network:
docker network connect <network-name> <container-id>
-
Disconnect a Container from a Network:
docker network disconnect <network-name> <container-id>
Volumes and Data Management
-
Create a Volume:
docker volume create <volume-name>
-
List Volumes:
docker volume ls
-
Remove a Volume:
docker volume rm <volume-name>
-
Mount a Volume to a Container:
docker run -v <volume-name>:<container-path> <image-name>
Dockerfile and Building Images
-
Create a Dockerfile:
Basic structure:
FROM <base-image> MAINTAINER <your-name> COPY <source> <destination> RUN <command> CMD ["<executable>"]
-
Build an Image from a Dockerfile:
docker build -t <image-name>:<tag> .
Example:
docker build -t myapp:latest .
-
List Built Images:
docker images
Advanced Commands
-
Tag an Image:
docker tag <image-id> <new-image-name>:<tag>
-
Push an Image to Docker Hub:
docker push <image-name>:<tag>
-
Save an Image to a Tar File:
docker save -o <path-to-output-file> <image-name>
-
Load an Image from a Tar File:
docker load -i <path-to-input-file>
-
Docker Compose:
-
Start Services:
docker-compose up
-
- **Stop Services**:
```bash
docker-compose down
```
-
Scale Services:
docker-compose up --scale <service-name>=<number>
-
View Running Docker Compose Services:
docker-compose ps
Useful Docker Commands
-
Remove All Stopped Containers:
docker container prune
-
Remove Unused Images:
docker image prune
-
Remove All Unused Data (containers, networks, images):
docker system prune
-
Get Docker System Information:
docker info
Posted on October 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.