Cloud
Docker
Cammands_and_examples
Cammands

Docker & Microservices Cheat Sheet

Docker Image

  1. Build an image from a Dockerfile:

    docker build -t image_name path_to_dockerfile

    Example:
    docker build -t myapp .

  2. List all local images:

    docker images

    Example:
    docker image ls

  3. Pull an image from Docker Hub:

    docker pull image_name:tag

    Example:
    docker pull nginx:latest

  4. Remove a local image:

    docker rmi image_name:tag

    Example:
    docker rmi myapp:latest

  5. Tag an image:

    docker tag source_image:tag new_image:tag

    Example:
    docker tag myapp:latest myapp:v1

  6. Push an image to Docker Hub:

    docker push image_name:tag

    Example:
    docker push myapp:v1

  7. Inspect image details:

    docker inspect image_name:tag

    Example:
    docker inspect myapp:v1

  8. Save image to tar:

    docker save -o image_name.tar image_name:tag

    Example:
    docker save -o myapp.tar myapp:v1

  9. Load image from tar:

    docker load -i image_name.tar

    Example:
    docker load -i myapp.tar

  10. Prune unused images:

    docker image prune

Docker Container

  1. Run a container from image:

    docker run image_name

    Example:
    docker run myapp

  2. Run named container:

    docker run --name container_name image_name:tag

    Example:
    docker run --name my_container myapp:v1

  3. List running containers:

    docker ps

  4. List all containers:

    docker ps -a

  5. Stop container:

    docker stop container_name

    Example:
    docker stop my_container

  6. Start container:

    docker start container_name

    Example:
    docker start my_container

  7. Run in interactive mode:

    docker run -it container_name

    Example:
    docker run -it my_container

  8. Interactive shell mode:

    docker run -it container_name sh

    Example:
    docker run -it my_container sh

  9. Remove stopped container:

    docker rm container_name

    Example:
    docker rm my_container

  10. Force remove running container:

    docker rm -f container_name

    Example:
    docker rm -f my_container

  11. Inspect container:

    docker inspect container_name

    Example:
    docker inspect my_container

  12. View container logs:

    docker logs container_name

    Example:
    docker logs my_container

  13. Pause container:

    docker pause container_name

    Example:
    docker pause my_container

  14. Unpause container:

    docker unpause container_name

    Example:
    docker unpause my_container

Docker Volumes and Networks

  1. Create volume:

    docker volume create my_volume

  2. List volumes:

    docker volume ls

  3. Inspect volume:

    docker volume inspect my_volume

  4. Remove volume:

    docker volume rm my_volume

  5. Run container with volume:

    docker run --name my_container -v my_volume:/app/data myapp:v1

  6. Copy to container:

    docker cp data.txt my_container:/app/data

  7. List networks:

    docker network ls

  8. Port mapping:

    docker run --name my_container -p 8080:80 myapp

  9. Create bridge network:

    docker network create my_network

  10. Connect to network:

    docker network connect my_network my_container

  11. Inspect network:

    docker network inspect my_network

  12. Disconnect from network:

    docker network disconnect my_network my_container

Docker Compose

  1. Start containers:

    docker-compose up

  2. Stop and remove containers:

    docker-compose down

  3. Build services:

    docker-compose build

  4. List containers:

    docker-compose ps

  5. View logs:

    docker-compose logs

  6. Scale services:

    docker-compose up -d --scale web=3

  7. Run command in service:

    docker-compose run web npm install

  8. List volumes:

    docker volume ls

  9. Pause service:

    docker-compose pause web

  10. Unpause service:

    docker-compose unpause web

  11. Service info:

    docker-compose ps web