Day 20 : Docker and Docker-Compose Cheat Sheet ๐Ÿณ

ยท

3 min read

Day 20 : Docker and Docker-Compose Cheat Sheet ๐Ÿณ

Hello DevOps enthusiasts! ๐Ÿ‘‹ Ready to elevate your Docker game? This comprehensive cheat sheet covers essential Docker and Docker-Compose commands, making your DevOps journey smoother. Let's dive in! ๐Ÿš€

Docker Commands

Container Lifecycle

  • Run a Container:

    COPY

        bashCopy codedocker run [options] image[:tag] [command] [args...]
    

    Example: docker run -it ubuntu /bin/bash

    • Explanation:

      • The docker run command creates and starts a container based on the specified image.

      • Options like -it allocate a pseudo-TTY and keep STDIN open, enabling an interactive shell.

      • image[:tag] denotes the base image and its optional tag.

      • [command] [args...] allows you to override the default command.

  • List Running Containers:

    COPY

        bashCopy codedocker ps
    
    • Explanation:

      • docker ps provides a snapshot of running containers.

      • Useful options include -a to show all containers (running and stopped).

  • Stop a Running Container:

    COPY

        bashCopy codedocker stop container_id
    
    • Explanation:

      • docker stop halts the execution of a running container gracefully.
  • Remove a Container:

    COPY

        bashCopy codedocker rm container_id
    
    • Explanation:

      • docker rm deletes a stopped container.

      • Add -f to force removal of a running container.

Image Management

  • List Downloaded Images:

    COPY

        bashCopy codedocker images
    
    • Explanation:

      • docker images displays all locally stored images.
  • Pull an Image:

    COPY

        bashCopy codedocker pull image[:tag]
    

    Example: docker pull nginx:latest

    • Explanation:

      • docker pull fetches an image from the specified repository.
  • Remove an Image:

    COPY

        bashCopy codedocker rmi image[:tag]
    
    • Explanation:

      • docker rmi removes a specified image.

      • If containers use the image, add -f to force removal.

Network

  • List Docker Networks:

    COPY

        bashCopy codedocker network ls
    
    • Explanation:

      • docker network ls shows a list of available networks.
  • Inspect a Network:

    COPY

        bashCopy codedocker network inspect network_name
    
    • Explanation:

      • docker network inspect provides detailed information about a network.
  • Create a Bridge Network:

    COPY

        bashCopy codedocker network create my_network
    
    • Explanation:

      • docker network create establishes a new user-defined bridge network.

Docker-Compose

  • Start Containers:

    COPY

        bashCopy codedocker-compose up -d
    
    • Explanation:

      • docker-compose up -d launches services defined in docker-compose.yml in detached mode.
  • Stop Containers:

    COPY

        bashCopy codedocker-compose down
    
    • Explanation:

      • docker-compose down stops and removes containers, networks, and volumes.
  • Scale Services:

    COPY

        bashCopy codedocker-compose up -d --scale service_name=3
    
    • Explanation:

      • --scale scales the specified service to the desired number of replicas.
  • View Logs:

    COPY

        bashCopy codedocker-compose logs
    
    • Explanation:

      • docker-compose logs displays logs from services defined in docker-compose.yml.

Docker-Compose YAML Structure

COPY

yamlCopy codeversion: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: mypassword
  ...

This comprehensive Docker and Docker-Compose cheat sheet equips you with essential commands and structures to streamline your containerized workflows. From running containers to orchestrating multi-container applications, each command plays a crucial role in enhancing your DevOps capabilities.

As you explore the vast Docker ecosystem, remember to leverage volumes, networks, and named containers for efficient data management and communication between services. With this cheat sheet in hand, you're well-prepared to navigate the dynamic landscape of containerization.

Keep thriving on the #DevOps journey!

#DevOps #Docker #CheatSheet #LearnWithMe

ย