πŸ›³οΈDay 23 in DevOps: Understanding Docker Volume and Network

πŸ›³οΈDay 23 in DevOps: Understanding Docker Volume and Network

Β·

3 min read

✨ Docker Volume :

Docker volumes are like special storage areas managed by Docker for your containers. They're better than using regular folders from your computer because:

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

  1. πŸ›‘οΈ Easy to Backup and Move: Volumes make it easy to save or move data.

  2. πŸ”§ Simple to Manage: Docker gives you easy commands to control volumes.

  3. 🌍 Works on Any System: Volumes work on both Linux and Windows.

  4. πŸ”’ Safe Sharing: You can safely share data between multiple containers.

  5. ✨ Extra Features: You can do cool things like store data on other computers or encrypt it.

  6. πŸ“‚ Start with Data: You can start a volume with some data already in it.

  7. πŸš€ Better Performance: Volumes work faster than regular folders, especially on Mac and Windows.

Think of volumes as a special storage area managed by Docker, making it easy and safe to handle your container data.

✨ Docker Network :

Docker helps you run applications in isolated environments called containers. To manage communication between these containers and the outside world, Docker uses different types of networks:

Understanding of Docker Networking using Hands-on Labs

  1. πŸŒ‰ Bridge Network:

    • Connects containers to each other and your host.

    • Containers get their own IP address.

    • They can communicate with each other but are isolated from other networks.

  2. 🏠 Host Network:

    • Containers use the host’s network directly.

    • No separate IP addresses for containers.

    • Ports used by containers are the same as the host’s ports.

  3. 🌐 Overlay Network:

    • Connects containers across multiple Docker hosts.

    • Useful for clusters of Docker hosts (like Docker Swarm).

  4. πŸ“‘ IPvLAN Network:

    • Gives precise control over IP addresses.

    • Useful for connecting containers to existing networks.

  5. πŸ”Œ Macvlan Network:

    • Makes containers appear as physical devices on your network.

    • Each container gets its own MAC address.

βœ…Task-1: Create a Multi-Container Docker-Compose File

πŸ“ Steps:

  1. Create a docker-compose.yml file with the following content:

      version: '3.8'
    
      services:
        web:
          image: nginx:latest
          ports:
            - "80:80"
    
        db:
          image: mysql:latest
          environment:
            MYSQL_ROOT_PASSWORD: example
            MYSQL_DATABASE: sample_db
          volumes:
            - db_data:/var/lib/mysql
    
      volumes:
        db_data:
    
  2. Run the following commands:

    • πŸš€ Start containers: docker-compose up -d

    • πŸ” Scale web service to 3 instances: docker-compose scale web=3

    • πŸ“‹ Check container status: docker-compose ps

    • πŸ“„ View logs for web service: docker-compose logs web

    • πŸ›‘ Stop and remove containers: docker-compose down

βœ…Task-2: Use Docker Volumes to Share Data Between Containers

πŸ“ Steps:

  1. πŸ“¦ Create a named volume:

      docker volume create my_shared_volume
    
  2. 🐳 Run two containers with the shared volume:

      docker run -d --name container1 --mount source=my_shared_volume,target=/shared_volume alpine tail -f /dev/null
      docker run -d --name container2 --mount source=my_shared_volume,target=/shared_volume alpine tail -f /dev/null
    
  3. ✍️ Write data from one container and check it from another:

      docker exec container1 sh -c "echo 'Hello from container1' > /shared_volume/data.txt"
      docker exec container2 cat /shared_volume/data.txt
    

    You should see "Hello from container1" when you check from the second container.

  4. πŸ“œ List all volumes and remove the shared volume:

      docker volume ls
      docker volume rm my_shared_volume
    

In summary, Docker volumes are great for sharing data between containers, making it easy to manage and keep your data safe.

Happy Learning! 😊

Β