Day 19 Task: Docker for DevOps Engineers(Part-3)

Day 19 Task: Docker for DevOps Engineers(Part-3)

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.

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

  • 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.

💼Task : 1

Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot.

1️⃣Step : 1 We have compose a docker compose file

version: '3'
services:

  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql   # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
      - mysql-data:/var/lib/mysql  # Mount the volume for MySQL data storage

volumes:
  mysql-data:

2️⃣Step : 2

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

docker-compose up -d

3️⃣Step : 3

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.

docker-compose up -d --scale backend=3

4️⃣Step : 4

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.

docker-compose ps
docker-compose logs

5️⃣Step : 5

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

docker-compose down

💼Task : 2

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

docker volume ls
docker volume create test_volume

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

# Container 1
docker run -d --name container1 --mount source=shared_data,target=/data alpine tail -f /dev/null

# Container 2
docker run -d --name container2 --mount source=shared_data,target=/data alpine tail -f /dev/null

In the above commands:

  • --name: Assigns a name to the container.

  • --mount: Attaches the "shared_data" named volume to the container and mounts it at the "/data" directory.

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

# Add data from container1
docker exec container1 sh -c "echo 'Data from Container 1' > /data/test.txt"

# Verify data from container2
docker exec container2 cat /data/test.txt

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

docker volume ls
docker volume rm

📍Conclusion :

On this blog we have covered Docker-Volume & Docker-Network we have completed few task related to this.

Thank you for reading!

Contact me on : Linkedin 🤝

Check out my GitHub for more resources 📚