DEV Community

bappasaha
bappasaha

Posted on

Docker Cheetsheet

Docker Cheat Sheet

Getting Started with Docker

  1. Check Docker Installation:

    docker --version
    
    

Basic Commands

  1. Pull an Image from Docker Hub:

    docker pull <image-name>
    
    

    Example:

    docker pull ubuntu
    
    
  2. List Available Images:

    docker images
    
    
  3. Run a Container:

    docker run -it <image-name>
    
    

    Example:

    docker run -it ubuntu
    
    
  4. Run a Container in Detached Mode:

    docker run -d <image-name>
    
    
  5. Stop a Running Container:

    docker stop <container-id>
    
    
  6. Start a Stopped Container:

    docker start <container-id>
    
    
  7. Remove a Stopped Container:

    docker rm <container-id>
    
    
  8. Remove an Image:

    docker rmi <image-name>
    
    

Managing Containers

  1. List Running Containers:

    docker ps
    
    
  2. List All Containers (including stopped):

    docker ps -a
    
    
  3. View Container Logs:

    docker logs <container-id>
    
    
  4. Execute a Command in a Running Container:

    docker exec -it <container-id> <command>
    
    

    Example:

    docker exec -it <container-id> bash
    
    
  5. Inspect a Container:

    docker inspect <container-id>
    
    

Networking

  1. List Docker Networks:

    docker network ls
    
    
  2. Create a New Network:

    docker network create <network-name>
    
    
  3. Connect a Container to a Network:

    docker network connect <network-name> <container-id>
    
    
  4. Disconnect a Container from a Network:

    docker network disconnect <network-name> <container-id>
    
    

Volumes and Data Management

  1. Create a Volume:

    docker volume create <volume-name>
    
    
  2. List Volumes:

    docker volume ls
    
    
  3. Remove a Volume:

    docker volume rm <volume-name>
    
    
  4. Mount a Volume to a Container:

    docker run -v <volume-name>:<container-path> <image-name>
    
    

Dockerfile and Building Images

  1. Create a Dockerfile:
    Basic structure:

    FROM <base-image>
    MAINTAINER <your-name>
    COPY <source> <destination>
    RUN <command>
    CMD ["<executable>"]
    
    
  2. Build an Image from a Dockerfile:

    docker build -t <image-name>:<tag> .
    
    

    Example:

    docker build -t myapp:latest .
    
    
  3. List Built Images:

    docker images
    
    

Advanced Commands

  1. Tag an Image:

    docker tag <image-id> <new-image-name>:<tag>
    
    
  2. Push an Image to Docker Hub:

    docker push <image-name>:<tag>
    
    
  3. Save an Image to a Tar File:

    docker save -o <path-to-output-file> <image-name>
    
    
  4. Load an Image from a Tar File:

    docker load -i <path-to-input-file>
    
    
  5. Docker Compose:

    • Start Services:

      docker-compose up
      
      
- **Stop Services**:
Enter fullscreen mode Exit fullscreen mode
    ```bash
    docker-compose down

    ```
Enter fullscreen mode Exit fullscreen mode
  1. Scale Services:

    docker-compose up --scale <service-name>=<number>
    
    
  2. View Running Docker Compose Services:

    docker-compose ps
    
    

Useful Docker Commands

  1. Remove All Stopped Containers:

    docker container prune
    
    
  2. Remove Unused Images:

    docker image prune
    
    
  3. Remove All Unused Data (containers, networks, images):

    docker system prune
    
    
  4. Get Docker System Information:

    docker info
    
    

Top comments (0)