Skip to main content

Command Palette

Search for a command to run...

Docker Volume & Docker Network

Day 19 for 90DayOfDevOps

Published
4 min readView as Markdown
Docker Volume & Docker Network
S

Hello, I am sumit and currently pursing my final year of graduation with IT stream from JSPM BSIOTR Wagholi Pune. I am currently learning DevOps with TrainWIthShubham. I have prior knowledge of Java, JSP, Servlet, SQL and Data structure also.I am a hard worker, smart and quick learner.

Docker-Volume:

In Docker, a volume is a way to persist data generated by and used by Docker containers. Volumes provide a mechanism for sharing data between containers, as well as persisting data beyond the lifetime of a single container. They are particularly useful for storing databases, logs, and other data that should survive container restarts and removals.

Docker volumes have the following key characteristics:

  1. Data Persistence: Volumes allow data to persist even if the container that created them stops or is removed. This is important for scenarios where you want to retain data across container restarts or updates.

  2. Shared Data: Volumes can be shared among multiple containers, enabling data sharing and communication between them. This is useful for cases where multiple containers need access to the same data.

  3. Separation of Concerns: Volumes separate the concerns of data storage and the container itself. This makes it easier to manage and update containers without affecting the persistent data stored in volumes.

  4. Volume Types:

    • Named Volumes: These are volumes with a specific name and are managed by Docker. They can be easily referenced and shared between containers.

    • Anonymous Volumes: These are volumes that are created dynamically and are not given a specific name. They are typically used when you don't need to share the volume between containers or when the volume's name is not important.

  5. Volume Mounting: Containers can mount volumes, making the data within the volume accessible to the container. This is done by specifying the volume name or path in the docker run command or the docker-compose.yml file.

Here's a simple example of using a named volume in a docker-compose.yml file:

yamlCopy codeversion: '3'

services:
  web:
    image: nginx:latest
    volumes:
      - mydata:/usr/share/nginx/html

volumes:
  mydata:

In this example, a named volume named mydata is created and mounted to the /usr/share/nginx/html directory inside the Nginx container. This allows the Nginx container to serve content from the volume, and the data in the volume is persisted even if the container is stopped or removed.

To create and manage volumes, Docker provides commands like docker volume create, docker volume ls, and docker volume rm. Additionally, you can manage volumes through Docker Compose as demonstrated in the example above.

Docker Network:

In Docker, a network is a communication bridge between containers, allowing them to communicate with each other, share data, and access services. Docker provides various networking options to facilitate communication between containers and between containers and the host machine. These networking options help in creating isolated and secure environments for running containerized applications.

Here are some key concepts and features of Docker networking:

  1. Default Bridge Network: When Docker is installed, it creates a default bridge network named bridge. Containers connected to this network can communicate with each other using their container names, but they are isolated from the host machine and other external networks by default.

  2. User-Defined Bridge Networks: Docker allows you to create custom bridge networks, providing more control over container communication. Containers on the same user-defined bridge network can communicate with each other using container names, and you can also expose specific ports for external access.

    Example of creating a custom bridge network in a docker-compose.yml file:

     yamlCopy codeversion: '3'
    
     services:
       webapp1:
         image: nginx:latest
         networks:
           - mynetwork
    
       webapp2:
         image: nginx:latest
         networks:
           - mynetwork
    
     networks:
       mynetwork:
    
  3. Host Network: Containers can be attached to the host network, sharing the network namespace with the host. This means that the container has direct access to the host's network interfaces.

    Example of using the host network in a docker-compose.yml file:

     yamlCopy codeversion: '3'
    
     services:
       webapp:
         image: nginx:latest
         network_mode: host
    
  4. Overlay Network (Swarm Mode): Docker Swarm, Docker's native clustering and orchestration solution, supports overlay networks. Overlay networks enable communication between containers across multiple Docker hosts in a swarm.

    Example of creating an overlay network in a Docker Swarm environment:

     bashCopy codedocker network create --driver overlay myoverlaynetwork
    
  5. Macvlan Network: Docker supports Macvlan networks, which allow containers to have their own MAC addresses and appear as separate physical devices on the network.

    Example of creating a Macvlan network:

     bashCopy codedocker network create -d macvlan \
       --subnet=192.168.1.0/24 \
       --gateway=192.168.1.1 \
       -o parent=eth0 \
       mymacvlan
    

These are just a few examples of Docker networking options. Choosing the appropriate network type depends on the specific requirements of your application, such as isolation, communication patterns, and external access. Docker provides a flexible and powerful networking model to meet various use cases in containerized environments.