Mastering Docker: A Comprehensive Cheat Sheet for DevOps Success #90DaysOfDevops

Mastering Docker: A Comprehensive Cheat Sheet for DevOps Success #90DaysOfDevops

Registry & Repository

Install Docker on Linux:

sudo apt install docker.io (ubuntu) 
sudo yum install docker (centos)

docker login: To log in to a registry.

docker logout: To log out from a registry.

docker version: To know the version of a docker

docker search: It will search the registry for the image.

docker pull <image name: tag>: It will pull an image from the registry to the local machine.

🟒docker push <image name: tag>: It will push an image to the registry from the local machine.

Images

You can refer to images as the templates for the Docker containers. You can run the following commands to work with the images:

  • docker images: It will display all images.

  • docker images -q: Lists only the image_id of all the Docker images in the system.

  • docker image prune -a: Removes only the unused or dangling images and not the images used by a container. Gives you a prompt to proceed or not.

  • docker build: The docker build command is used to build the docker images with the help of Dockerfile. In the place of image_name use the name of the image you build with and give the t flag for tag and . “dot” represents the current directory.

docker build -t image_name:tag .
  • docker rmi <image name >: It will remove an image.

  • docker load -i < image name.tar>: It will load an image from STDIN a tar archive including images and tags.

  • docker save -o <image name> <imagename.tar>: It will save an image STDOUT to a tar archive stream with all parent layers, tags, & versions.

  • docker history <image name>: To display the history of an image

docker history <image name> -H
#Options of history command:
-H, --human=true|false
# Print sizes and dates in human readable format. The default is true.
 --no-trunc=true|false
#  Don't truncate output. The default is false.
 -q, --quiet=true|false
#  Only show numeric IDs. The default is false.
  • docker tag: It will tag the image to a name.

  • docker export my_container | gzip > my_container.tar.gz: It will export the container.

  • Container

Containers are the isolated Docker process that contains the code to be executed.

  • docker create: It will create a container without starting it.

  • docker rename: To rename the container.

  • docker run: It will create and start the container in one task. here the -d flag is detachable.

docker run -d <image name>
  • docker rm: It will delete a container.
docker rm <container ID>
  • docker update: It will update a container's resource limits.

  • docker commit: You can create an image from a container and temporarily pause it if it is running

docker commit container_name_or_id new_image_name:tag

Usually, a container will start and stop immediately if you run it without any option.

  • docker run -td container_id: It will keep the container running, -t will allocate a pseudo-TTY session, and -d will detach the container automatically.

  • docker rm -v: It will remove the volumes associated with the container.

  • docker start: It will start a container, so it is running.

  • docker stop: It will stop a running container.

  • docker restart: It stops and starts a container.

  • docker pause: It will pause a running container, "freezing" it in place.

  • docker unpause: It will unpause a running container.

  • docker wait: It will block until the running container stops.

  • docker kill: It sends a SIGKILL to a running container.

  • docker attach: It will connect to a running container.

  • docker ps: It will display the running containers.

  • docker logs: Provide the logs from the container.

  • docker inspect: It checks all the information on a container (including IP address).

  • docker events: It will get the events from the container.

  • docker port: It will display the public-facing port of the container.

  • docker top: It will display the running processes in the container.

  • docker stats: It will display the containers' resource usage statistics.

  • docker diff: It will display the changed files in the container's FS.

  • docker ps -a: It will display the running and stopped containers.

  • docker stats --all: It will display a list of all containers, default shows just running.

  • docker cp: It will copy the files or folders between a container and the local filesystem.

  • docker exec: To execute a command in a container.

  • Networks

  • Docker has a featured network, allowing the containers to connect. You can create three network interfaces with Docker, namely bridge, host, and none.By default, the new container is launched into the bridge network. To establish communication among several containers, you need a new network for launching containers in it. It lets the containers communicate while being isolated from other containers not connected to the network.docker network create: It will create a new network of bridge type by default.docker network rm: It will remove one or more networks specified by name and make sure that no containers are connected to the deleted network.docker network ls: It will list all the networks.docker network inspect: It will show detailed information on one or more networks.docker network connect: It will connect a container to a networkdocker network disconnect: It will disconnect a container from a network.

Volumes

  • Docker has volumes that are free-floating filesystems. So there is no need to be connected to a particular container. You can use volumes mounted from data-only containers for portability. As per Docker 1.9.0, it comes with the named volumes that replace data-only containers.docker volume create: to create volumes.docker volume rm: To remove volumes.docker volume ls: To list the volumes.docker volume inspect: To inspect the volumes.

Build

You can use the following commands to build the images from a Docker file.

  • Docker build -t myapp: will build an image from the Docker file and tag it.

  • Docker images- it will list all the images that are locally stored

  • Docker rmi alpine: 3.4 will delete an image from the Docker Store.

Cleanup

To optimize the usage of the resources, you need to clean up the resources frequently to maintain the performance. You can run the following commands to clean up resources.

  • Docker image prune: It will clean an unused/dangling image

  • Docker image prune -a: It will remove an image not used in a container.

  • Docker system prune: It will prune the entire system.

  • docker rm $(docker ps -a -q): It will delete all stopped containers

  • docker rmi $(docker images -q): It will delete all images.

Docker-compose Cheat Sheet

  • Compose is a tool that helps you to define and run multi-container Docker applications. With Compose, you get to work with a YAML file to configure your application services. With the help of the following commands, you can simply create and start all the services from your configuration.

  • docker-compose start: It will start the container.

  • docker-compose stop: It will stop the container.

  • docker-compose pause: It will pause the container.

  • docker-compose unpause: It will unpause the container.

  • docker-compose ps: It will list all the containers.

  • docker-compose up: It aggregates the output of each container (essentially running docker-compose logs --follow ).

  • Docker-compose down: It stops containers and removes containers, networks, volumes, and images created by up

  • .Docker-compose -f <docker-compose-file> up: It will start up your application.

  • docker-compose stop-run docker-compose in detached mode using the-d flag, and then you can stop it whenever needed.You can run them in the Docker CLI (Command-Line Interface) to manage containers, images, networks, and volumes in your Docker environment. For additional options and command variations, you can refer to the Docker documentation or use the docker --help command to see the available options for each command.

  • Thank you for reading this blog! 📖 Hope you have gained some value.If you found it helpful, please give it a like 👍, share your thoughts, and give me some valuable feedback. 😇 Don't forget to follow me for more such blogs on my LinkedIn. Happy DevOps Learning😊😊!!!