Docker Commands Cheat-Sheet:
Docker Image Management:
docker pull <image_name>
: Pull an image from Docker Hub.docker build -t <image_name> .
: Build a Docker image from a Dockerfile in the current directory.docker images
: List all available Docker images.docker rmi <image_id>
: Remove a Docker image.
Container Management:
docker run -d --name <container_name> <image_name>
: Run a Docker container in the background.docker ps
: List all running containers.docker ps -a
: List all containers (including stopped ones).docker start <container_id>
: Start a stopped container.docker stop <container_id>
: Stop a running container.docker rm <container_id>
: Remove a stopped container.
Logs and Inspection:
docker logs <container_id>
: View container logs.docker inspect <container_id>
: Get detailed information about a container.
Interactive Shells:
docker exec -it <container_id> /bin/bash
: Start an interactive shell inside a running container.
Port Mapping:
docker run -p <host_port>:<container_port> <image_name>
: Map a container's port to a host port.
Volume Management:
docker volume create <volume_name>
: Create a Docker volume.docker run -v <volume_name>:<container_path> <image_name>
: Mount a volume to a container.docker volume ls
: List Docker volumes.docker volume create <volume>
: Create a named Docker volume.docker volume inspect <volume>
: Display detailed information about a volume.docker run -v <volume>:<container-path>
: Mount a volume into a container.
Networks:
docker network ls
: List Docker networks.docker network create <network_name>
: Create a custom Docker network.docker network connect <network_name> <container_id>
: Connect a container to a network.
Docker Compose:
Docker-Compose Commands Cheat-Sheet:
Compose Up and Down:
docker-compose up -d
: Start services defined in thedocker-compose.yml
file in detached mode.docker-compose down
: Stop and remove containers defined in thedocker-compose.yml
file.
Logs and Inspection:
docker-compose logs
: View logs for all services.docker-compose ps
: List the status of containers in the Compose project.docker-compose exec <service_name> /bin/bash
: Start an interactive shell in a service container.
Scaling Services:
docker-compose up -d --scale <service_name>=<number_of_instances>
: Scale a service to run multiple instances.
Building and Rebuilding:
docker-compose build
: Build or rebuild services as defined in thedocker-compose.yml
file.
Environment Variables:
- Define environment variables in the
docker-compose.yml
file under theenvironment
key for a service.
- Define environment variables in the
Volumes and Networks:
- Define volumes and custom networks in the
docker-compose.yml
file for services.
- Define volumes and custom networks in the
This cheat-sheet should help you work with Docker and Docker-Compose effectively and serve as a quick reference for various common tasks in container management and orchestration.