Day 19-20 : Docker  for DevOps Engineer

Day 19-20 : Docker for DevOps Engineer

Β·

6 min read

Understanding Docker Volume: πŸ“¦

Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having the same data

πŸ”‘ Key Points about Docker Volumes:

  • Docker Volumes are a way to manage and persist data beyond the lifecycle of containers.

  • They provide a bridge between the host system and the container, allowing data to be shared and stored separately.

  • Volumes can be used to store databases, configuration files, logs, and any data that needs to persist even if the container is deleted.

πŸ› οΈ How to Use Docker Volumes:

  1. Creating a Volume: To create a Docker Volume, you can use the docker volume create command. For example:

     docker volume create my_data_volume
    
  2. Using Volumes in Containers: You can attach a volume to a container during its creation or by using the docker run command:

     docker run -d -v my_data_volume:/app/data my_app_container
    
  3. Inspecting Volumes: Use docker volume inspect to see details about a volume:

     docker volume inspect my_data_volume
    

    πŸš€ Here's a list of Docker volume commands:

    1. docker volume create <VOLUME_NAME>: Create a named volume.

    2. docker volume ls: List available volumes.

    3. docker volume inspect <VOLUME_NAME>: Display detailed volume information.

    4. docker volume rm <VOLUME_NAME>: Remove a named volume (must be unused).

    5. docker volume prune: Remove all unused volumes.

    6. docker run -v <VOLUME_NAME>:<CONTAINER_PATH>: Mount a named volume to a container.

    7. docker run -v <HOST_PATH>:<CONTAINER_PATH>: Mount a host directory to a container.

Navigating Docker Networking: 🌐

Docker allows you to create virtual spaces called networks, where you can connect multiple containers (small packages that hold all the necessary files for a specific application to run). This way, the containers can communicate with each other and with the host machine (the computer on which the Docker is installed). When we run a container, it has its own storage space that is only accessible by that specific container. If we want to share that storage space with other containers, we can't do that.

πŸ”‘ Key Points about Docker Networking:

  • Docker provides different networking options like Bridge, Host, Overlay, and MACVLAN networks.

  • The Bridge network is the default network and enables containers on the same host to communicate via internal IP addresses.

  • Overlay networks are used for communication between containers on different Docker hosts, enabling multi-host deployments.

πŸ› οΈ Working with Docker Networks:

  1. Creating a Network: To create a Docker network, you can use the docker network create command:

     docker network create my_app_network
    
  2. Running Containers on a Network: While running a container, you can specify the network using the --network flag:

     docker run -d --network my_app_network my_app_container
    
  3. Inspecting Networks: Check network details using the docker network inspect command:

     docker network inspect my_app_network
    

Here's a list of Docker network commands:

  1. docker network create <NETWORK_NAME>: Create a new user-defined network.

  2. docker network ls: List available networks.

  3. docker network inspect <NETWORK_NAME>: Display detailed network information.

  4. docker network rm <NETWORK_NAME>: Remove a user-defined network (must be unused).

  5. docker network prune: Remove all unused networks.

  6. docker run --network <NETWORK_NAME>: Specify a network for a container to join.

  7. docker network connect <NETWORK_NAME> <CONTAINER_NAME>: Connect a container to a user-defined network.

  8. docker network disconnect <NETWORK_NAME> <CONTAINER_NAME>: Disconnect a container from a user-defined network.

Tasks :

Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )

  • First, create a docker-compose.yml file for your project:

  • Use the docker-compose up command with the -d flag to start a multi-container application in detached mode.

  • Use the docker-compose ps command to view the status of all containers, and docker-compose logs to view the logs of a specific service.

  • Use the docker-compose down command to stop and remove all containers, networks, and volumes associated with the application.

CheatsheetπŸ˜„:

  1. docker run - run a container from an image

  2. docker pull - pull an image from a registry

  3. docker push - push an image to a registry

  4. docker build - build an image from a Dockerfile

  5. docker ps - list running containers

  6. docker stop - stop a running container

  7. docker start - start a stopped container

  8. docker restart - restart a container

  9. docker logs - show the logs of a container

  10. docker exec - execute a command inside a running container

  11. docker images - list available images

  12. docker rm - remove a container

  13. docker rmi - remove an image

  14. docker inspect - show information about a container

  15. docker network create - create a network for containers to communicate

  16. docker network connect - connect a container to a network

  17. docker network disconnect - disconnect a container from a network

  18. docker port - show the mapped ports of a container

  19. docker cp - copy files between a container and the host

  20. docker commit - create a new image from a container's changes

  21. docker login - log in to a registry

  22. docker logout - log out of a registry

  23. docker tag - tag an image with a new name

  24. docker export - export the contents of a container as a tar archive

  25. docker import - create a new image from a tar archive

  26. docker save - save an image as a tar archive

  27. docker load - load an image from a tar archive

  28. docker top - show the processes running inside a container

  29. docker stats - show resource usage statistics of containers

  30. docker diff - show the changes made to a container's filesystem

  31. docker events - show the events generated by Docker

  32. docker history - show the history of an image

  33. docker pause - pause a running container

  34. docker unpause - unpause a paused container

  35. docker kill - send a signal to a container to stop it abruptly

  36. docker wait - wait for a container to exit and return its exit code

  37. docker attach - attach to a running container's console

  38. docker buildx - build and push multi-platform images

  39. docker compose - manage multi-container applications with Docker Compose

  40. docker swarm - create and manage a cluster of Docker nodes

  41. docker volume create - create a named volume for persistent data storage

  42. docker volume ls - list available volumes

  43. docker volume rm - remove a named volume

  44. docker system prune - remove all unused objects from Docker

  45. docker system df - show the usage of Docker objects

  46. docker system events - show the events generated by Docker on the system

  47. docker system info - show the system-wide information about Docker

  48. docker system inspect - show detailed information about Docker objects

  49. docker system logs - show the system logs of Docker

  50. docker system version - show the version of Docker installed on the system

Conclusion🐳:

By mastering these aspects of Docker, DevOps teams can unlock a world of possibilities for rapid development, efficient testing, and seamless deployment. So go ahead, embrace the power of Docker and take your DevOps game to the next level! πŸš€πŸŒ

Β