Docker volume and Docker Network

Docker volume and Docker Network

Two essential components of Docker are volumes and networks. In this blog, we will explore these two components and understand how they are used in a Docker project.

Docker Volume

Docker Volume is a persistent data storage mechanism that enables a container to store and share data with other containers or the host machine.

There are a few different types of Docker volumes: host, anonymous, and, named.

Host Volume

Host Volume (bind mount): Maps a host directory into a container, allowing the container to read and write to the host's filesystem. To create a host volume, run:

docker run -v /path/on/host:/path/in/container

Anonymous volumes

Anonymous Volume: Created and managed by Docker automatically, with no explicit name, used for temporary or disposable storage within a container.

docker run -v /path/in/container ...

Named Volume

Named Volume: Explicitly created and named by the user, can be shared and reused across multiple containers, with data stored in a location managed by Docker.

docker volume create somevolumename
docker run -v name:/path/in/container ...

Docker Network

Docker Networking is a feature that allows you to connect multiple Docker containers together, creating a network of containers that can communicate with each other. Docker Networking provides a way for containers to interact with each other without needing to expose their ports to the outside world.

There are several types of Docker networks, each designed for a specific use case.

  1. Bridge Network: The default network type that allows containers to communicate with each other using IP addresses.

  2. Host Network: Shares the network stack with the host system, allowing containers to use the host system's IP address.

  3. Overlay Network: Spans multiple Docker hosts, enabling containers to communicate with each other as if they were on the same network.

  4. Macvlan Network: Assigns a unique MAC address to each container, making it appear as a physical device on the network.

Docker Project

Let's understand Docker volume and Docker network concept using project.

version: '3.8'
services:
  db:   
    image: mysql:8.0.27
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
    networks:
      - my_network

  wordpress:
    image: wordpress:latest
    ports:
      - 8090:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
    networks:
      - my_network

  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d

    depends_on:
      - wordpress
    networks:
      - my_network
volumes:
  db_data:
  nginx_config:

networks:
  my_network:
    driver: bridge

There are three services defined in this yaml file:

  1. db - This service is responsible for running the MySQL database server in a container.

  2. wordpress - This service is responsible for running the WordPress application in a container.

  3. nginx - This service is responsible for running the Nginx web server in a container.

Network

The code defines a custom Docker network named "my_network" using the following section:

networks:
  my_network:
    driver: bridge

All three services (db, wordpress, and nginx) are connected to this network using the following section under each service definition:

networks:
  - my_network

By connecting the services to the same network, they can communicate with each other using their container names as hostnames. For example, the WordPress container can connect to the MySQL database container by using "db" as the hostname, which is the name of the MySQL service defined in the Compose file.

Volume

The code defines two Docker volumes:

  1. db_data - This volume is used to persist data for the MySQL database service. It is defined using the following section:

     volumes:
       - db_data:/var/lib/mysql
    

    This maps the "db_data" volume to the "/var/lib/mysql" directory inside the MySQL container. This ensures that the data stored by the MySQL container persists even after the container is destroyed.

  2. nginx_config - This volume is used to provide custom Nginx configuration files without having to rebuild the container. It is defined using the following section:

     volumes:
       - ./nginx/conf:/etc/nginx/conf.d
    

    This maps the "./nginx/conf" directory on the host machine to the "/etc/nginx/conf.d" directory inside the Nginx container. Any files placed in the "./nginx/conf" directory on the host machine will be available inside the Nginx container at the "/etc/nginx/conf.d" directory.

Run the following command to start the services defined in the Compose file:

docker-compose up -d

To check running containers :

docker-compose ps

To check wordpress service is running :

https://localhost:8090
or
https://instance-ip-address:8090

To stop the services:

docker compose down

To see the list of volumes :

docker volume ls

You can see "wordpress_db_data" and "wordpress_nginx_config" volumes we created using yaml file.

To see the created networks :

docker network ls

You can see "wordpress_my_network" network e created using yaml file.

Thank you for reading! Hope you find this article helpful.

~Kunal