✨Ultimate Docker CheatSheet For Devops✨

✨Ultimate Docker CheatSheet For Devops✨

Hello Everyone! Hope your'e doing good this CheatSheet includes all the possible syntaxs used in day-to-day for Devops Engineer. The Syntaxsare in the systemactical manner from creating > building > running > Managing > Monitoring .

Docker Images

1. docker images

docker images : This command lists all locally available Docker images along with their details such as repository, tag, image ID, creation time, and size.

2.docker pull

docker pull <image_name>:<tag> : This command downloads a Docker image from a registry. If the tag is not specified, it defaults to the "latest" tag.

docker search <image_name> : This command searches the Docker Hub for images matching the specified name.

4.docker build

docker build . -t <image_name> : This command builds a Docker image from a Dockerfile in the specified directory.

5.docker rmi

docker rmi <image_name/id> : This command removes a specific Docker image. If the tag is not specified, it removes all tags for that image.

docker image rm <image-name/id> can also use this command.

6.docker prune

docker image prune -a : This command removes all dangling (untagged) images as well as unused images.

7. dokcer image inspect

docker image inspect <image_name> : This command provides detailed information about a specific Docker image, including its configuration and layers.

Running a New Container

8. docker run

Run a Containerdocker run <image-name> : This command starts a container based on the official Ubuntu image and runs the default command (usually a shell) docker run ubuntu

9.docker run -d

Run Container in the Backgrounddocker run -d <image-name> : This starts an Nginx container in the background (detached mode). docker run -d nginx

10.docker run -p

Map portdocker run -p <port-number> <image-name> : This maps port 8080 on the host to port 80 on the Nginx container. docker run -p 8080:80 nginx

11.docker run -v

Mount a Volumedocker run -v <path> <image-name> : This mounts a host directory into the Nginx container. docker run -v /host/path:/container/path nginx

12.docker run -e

Set Environment Variablesdocker run -e MYSQL_ROOT_PASSWORD=mysecretpassword mysql :This sets the environment variable for the MySQL root password.

13. docker run --hostname

docker run --hostname my_container_host -d nginx : In this example, a container based on the Nginx image is started in detached mode (-d), and the --hostname option sets the custom hostname to "my_container_host."

Managing a Container

14.docker start

Start a Containerdocker start [container_name or container_id] : This command starts a stopped container.

15.docker stop

Stop a Running Containerdocker stop [container_name or container_id] : This command stops a running container gracefully.

16.docker kill

Forcefully Stop a Running Containerdocker kill [container_name or container_id

This command forcefully stops a running container.

17. docker restart

Restart a Containerdocker restart [container_name or container_id] :This command stops and then starts a container.

18.docker inspect

Inspect Container Detailsdocker inspect [container_name or container_id] : This command provides detailed information about a container, including its configuration, network settings, and more.

19. docker ps

List Running Containersdocker ps: This command lists all running containers.

20. docker rm <container id/name>

Remove a Containerdocker rm [container_name or container_id] : This command removes a stopped container. Add the -f option to force removal of a running container.

Remove All Stopped Containersdocker container prune : This command removes all stopped containers.

Info &Stats about the Container

21. docker logs <container-name/id>

Logs of a Containerdocker logs [container_name or container_id] : This command displays the logs of a container.

22.docker inspect

To get container detailsdocker inspect <container-name>

23. docker diff

It shows the difference between the container's filesystem and the image it was created from docker diff [container_name or container_id]

💡Thedocker diffcommand is useful for understanding the changes made within a running container. It's often used during development or troubleshooting to inspect the impact of commands and modifications.

The changes displayed bydocker diffare based on the difference between the container's current state and the image it was created from. It doesn't show changes made outside the container.

To see changes in real-time as they happen, you can use thedocker eventscommand along withdocker diff.

Docker Volume

24.docker volume create

To create a docker volume docker volume create <volume_name>

25.docker volume ls

To see all created volumes docker volume ls

26. docker volume inspect

To see all created volumes docker volume inspect <volume-name>

27. docker volume prune

To remove all unused docker volumes docker volume prune

Container Orchestration

The primary goal of container orchestration is to simplify the deployment and management of containerized applications by automating complex tasks and providing tools for efficient scaling, networking, and high availability.

28.docker service create

docker service create -–replicas=100 nodejs :
The command will create a Docker service with 100 replicas using the nodejs image.

Docker compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define a multi-container environment in a single file, typically named docker-compose.yml .It is created in yaml format ( .yml )

version: '3'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

  database:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: examplepassword
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myuser
      MYSQL_PASSWORD: myuserpassword

29. docker-composes up

docker-compose up: Create and start containers as per the configuration.

30.docker-compose down

docker-compose down: Stop and remove containers, networks, and volumes

31.docker-compose ps

docker-compose ps: List running containers.

Thanks for reading till the end of this article. Feel free to share your views and comments about this Blog !

🚀 Let's connect on !

Linkedin

Twitter.