Day 19 Task: Docker for DevOps Engineers

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 same data.

How to use volumes to share data between Docker containers | by Edouard  Courty | Medium

  • Docker Network

    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) together. 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.

Docker Networking - Basics, Network Types & Examples

Task-1

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

  • Create a directory day-19 in that again create directory docker-compose

  • After that use this command to compose up ⬇️ if you execute that command it will ask to create a volume and create it the after do compose up.
docker-compose up -d
docker volume create --name=my_django_volume

docker-compose up -d

Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in deployment file for auto-scaling.

  • when we are using docker-compose scale we need to down our container and use this command to scale our container.Which means we to kill and remove our containers.
docker-compose down

  • Now use this command to scale our file.
docker-compose up -d --scale web=5

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

  • As we did in docker-compose scale task

Task-2

Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

  • Create a new volume
 docker volume create ubuntu_volume
 docker volume ls
 docker volume inspect ubuntu_volume

Create two or more containers that read and write data to the same volume using the docker run --mount command.

  • Create two or more containers that need to read and write data. Use the below command to specify the same volume configuration for each container.
 docker run -d --name nginx_container_1 -v ubuntu_volume:/app nginx:latest
 docker run -d --name nginx_container_2 -v ubuntu_volume:/app nginx:latest
 docker ps

Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

  • Verify data consistency by executing commands inside each container using docker exec.
 docker exec -it <CONTAINER ID 1> bash
cd app
echo "File created in container 1" > volume.txt
ls
exit

I have created a new file called 'volume-test.txt' while connected to nginx_container_1. Let's connect to nginx_container_2 and verify the text file is there.

 docker exec -it nginx_container_2 bash
cd app
ls
cat volume.txt
exit

In this example, we have used the container ID and the container name with the docker exec command to verify the content of the volume

Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.

  • Utilize docker volume ls to list all volumes and docker volume rm to remove the volume once you have finished using it.
docker volume ls

  • When we want to remove a volume our container should stop and remove, then we can remove the volumes.

Stopping the containers:

docker ps
docker stop <NAMES>

Removing the volumes:

docker volume ls
docker volume rm <VOLUME NAME>

0
Subscribe to my newsletter

Read articles from Yuvraj Nath Chouhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yuvraj Nath Chouhan
Yuvraj Nath Chouhan

🚀 Aspiring DevOps & DevSecOps Engineer | Cloud DevOps (Azure & AWS) | Automation & Security Champion | Passionate Learner🌐 Hi, I’m Yuvraj! 🔥 I'm embarking on an exciting journey in the world of DevOps & DevSecOps, armed with hands-on expertise and a drive to excel. From automating tasks to building robust CI/CD pipelines, I’m dedicated to turning complex challenges into efficient and secure solutions. What I Bring to the Table: ●Linux & Shell Scripting 🖥️ Automating processes and mastering command-line operations. ●Git & Git Branching 🔀 Seamless version control for effective collaboration. ●Build Tools 🔧 (Maven & npm) Crafting stable builds for quality software. ●CI/CD Tools 🚄 (Jenkins, GitHub Actions, Azure DevOps, GitLab CI/CD) Ensuring smooth & automated deployments. ●Security in DevOps 🛡️ Embedding security at every stage to safeguard development processes. ●Artifact Management 📦 (Nexus & Azure Artifacts) Managing software versions efficiently. ●Containerization & Orchestration 🐳⎈ (Docker & Kubernetes) Deploying scalable, agile applications. ●Infrastructure as Code (IaC) 📜 (Terraform & Ansible) Automating environment provisioning. ●Monitoring & Troubleshooting 🔍 Keeping systems in check with proactive issue resolution. ●Real-world & Corporate Projects 🌟 Hands-on experience with practical implementations. ✨ Always eager to learn, collaborate, and innovate! ✨ I’m ready to transform my academic knowledge and project experiences into real-world impact. Let’s connect and explore how we can drive meaningful change together! 🤝