Day 20 - Docker Cheat Sheet

Day 20 - Docker Cheat Sheet

Day 20 of 90daysofdevops

Docker Basics

  • docker pull <image>: download a Docker image from a repository

  • docker images: list all Docker images on your system

  • docker ps: list all running Docker containers

  • docker ps -a: list all Docker containers, including stopped ones

  • docker run <image>: start a new Docker container from an image

  • docker stop <container>: stop a running Docker container

  • docker rm <container>: remove a stopped Docker container

  • docker rmi <image>: remove a Docker image from your system

Dockerfile

  • FROM: specify the base image to use in a Dockerfile

  • RUN: run a command during the build process of a Docker image

  • CMD: specify the default command to run when a container is started

  • EXPOSE: specify the port on which a container listens

  • COPY: copy files or directories from the build context to the container

  • WORKDIR: set the working directory for the container

  • ENV: set an environment variable for the container

Docker Images

  • docker images: list all Docker images on your system

  • docker image inspect <image>: inspect the details of a Docker image

  • docker image history <image>: show the history of a Docker image

  • docker image rm <image>: remove a Docker image from your system

  • docker image tag <source-image> <new-image-name>: tag a Docker image with a new name and/or tag

Docker Compose

  • docker-compose up: start the services defined in a docker-compose.yml file

  • docker-compose down: stop the services defined in a docker-compose.yml file

  • docker-compose build: build the Docker images defined in a docker-compose.yml file

  • docker-compose ps: list the running containers for services defined in a docker-compose.yml file

Docker Volumes

  • docker volume create <name>: create a new Docker volume

  • docker volume ls: list all Docker volumes on your system

  • docker volume inspect <name>: inspect the details of a Docker volume

  • docker run -v <name>:<path>: attach a Docker volume to a container

  • docker run -v <host-path>:<container-path>: mount a host directory as a Docker volume

Networking

  • docker network create <name>: create a new Docker network

  • docker network ls: list all Docker networks on your system

  • docker network inspect <name>: inspect the details of a Docker network

  • docker run --network <name>: attach a container to a Docker network

  • docker run -p <host-port>:<container-port>: publish a container's port to the host

  • docker run --expose <port>: expose a port from the container to other linked containers

Docker Swarm

  • docker swarm init: initialize a new Docker Swarm cluster

  • docker swarm join: join a Docker Swarm cluster

  • docker node ls: list all nodes in a Docker Swarm cluster

  • docker service create: create a new Docker service

  • docker service ls: list all Docker services in a Swarm cluster

  • docker service ps <service>: list the tasks for a Docker service

  • docker service scale <service>=<num-replicas>: scale a Docker service to a specified number of replicas

Docker Registry

  • docker login: log in to a Docker registry

  • docker logout: log out from a Docker registry

  • docker push <image>: push a Docker image to a registry

  • docker pull <image>: pull a Docker image from a registry

  • docker tag <source-image> <registry>/<repo>:<tag>: tag a Docker image for a registry

Docker Logs

  • docker logs <container>: display the logs of a running container

  • docker logs -f <container>: follow the logs of a running container in real time

  • docker logs --tail <number> <container>: display the last n lines of a container's logs

Docker Exec

  • docker exec -it <container> <command>: run a command inside a running container

  • docker exec -it <container> sh: open a shell session inside a running container

Docker Stats

  • docker stats: display live resource usage statistics of all running containers

Docker Commit

  • docker commit <container> <image-name>: create a new image from a running container's changes

Docker Build

  • docker build -t <image-name> .: build a Docker image from a Dockerfile in the current directory and tag it with the given name

Docker Clean-up

  • docker system prune: remove all stopped containers, unused networks, dangling images and caches

  • docker container prune: remove all stopped containers

  • docker image prune: remove all unused images

  • docker volume prune: remove all unused volumes

  • docker network prune: remove all unused networks

Docker Security

  • docker run --privileged: run a Docker container in privileged mode

  • docker run --cap-add=<capability>: add a Linux capability to a Docker container

  • docker run --security-opt <security-option>: add a security option to a Docker container

Docker System

  • docker system df: display information about disk usage and container counts

  • docker system prune: remove all unused containers, networks, images, and volumes

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #linux #docker #dockerproject