🚢Day 19 - Exploring Docker Volume & Docker Network

🚢Day 19 - Exploring Docker Volume & Docker Network

Docker Volume

Docker allows you to create something called volumes. Volumes are stored outside the container's filesystem, which allows them to persist even if the container is stopped or deleted. Volumes are often used to share data between containers or to persist data across container restarts.

You can create a volume using the docker volume create command:

docker volume create myvolume

Docker Network

In simple terms, a Docker network is like a virtual bridge that allows Docker containers to communicate with each other and with the outside world. It provides a way for containers to find and talk to each other, even if they are running on different machines or hosts. Think of it as a private network just for your Docker containers, where they can share data and services.

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

It's simple example of a multi-container Docker Compose file that includes an application and a database container. This example uses a Python Flask application with a PostgreSQL database.

version: '3.8'

services:
  web:
    image: python:3.9-slim
    volumes:
      - ./app:/app
    working_dir: /app
    command: ["python", "app.py"]
    ports:
      - "5000:5000"
    depends_on:
      - db

  db:
    image: postgres:13
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    ports:
      - "5432:5432"

To bring up the containers, run:

docker-compose up -d

To bring down the containers, run:

docker-compose down

This will stop and remove all containers, networks, and volumes associated with the application defined in the docker-compose.yml file.

Task-2: Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

To demonstrate using Docker Volumes and Named Volumes to share files between multiple containers, we'll create two containers that read and write data to the same volume.

Create a Docker Volume:

docker volume create myvolume

Create a file inside the volume:

echo "Hello, Docker Volumes!" | docker run -i --mount source=myvolume,target=/data alpine sh -c 'cat > /data/file.txt'

Verify that the file exists in the volume:

docker run --rm --mount source=myvolume,target=/data alpine ls /data

Create a second container and verify that it can read the file:

docker run --rm --mount source=myvolume,target=/data alpine cat /data/file.txt

You can use the docker exec command to run commands inside each container and verify that the data is the same in both containers. For example:

docker exec <container_id> cat /data/file.txt #Remember to replace <container_id> with the actual ID or name of the container.

Conclusion

In this task, we learned how to use Docker Volumes and Named Volumes to share files and directories between multiple containers. We created a Docker volume named myvolume, wrote data to a file inside the volume from one container, and then read the same file from another container. This demonstrates how Docker Volumes can be used to share data between containers, allowing them to access and modify the same files.

I'm confident that this blog will prove to be valuable, helping you discover new insights and learn something enriching .🙏

😊Happy Learning : )

Did you find this article valuable?

Support Pratik R. Mandge by becoming a sponsor. Any amount is appreciated!