Unlocking the Power of Docker Volumes and Networks

Unlocking the Power of Docker Volumes and Networks

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.

List all volumes and remove the shared volume:

 docker volume ls
 docker volume rm my_shared_volume

Conclusion:

Docker volumes and networks are essential tools for managing data and communication in containerized environments. Volumes provide a reliable and efficient way to handle data storage, ensuring easy backup, portability, and performance. Docker networks, on the other hand, facilitate seamless communication between containers and the outside world, offering various configurations to suit different needs. By understanding and utilizing these features, you can create robust, scalable, and efficient containerized applications.