# Docker Volume and Docker Network

### 1\. Docker Volumes

Docker volumes are file systems mounted on [Docker containers](https://phoenixnap.com/kb/docker-image-vs-container) to preserve data generated by the running container.

The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.

Docker has two options for containers to store files in the host machine so that the files are persisted even after the container stops:

1. **Volumes** are stored in a part of the host filesystem, which is managed by
    
2. **Bind mounts** may be stored *anywhere* on the host system.
    

### 2\. How to Create a Docker Volume

To create a Docker Volume use the command:

`docker volume create [volume_name]`

Docker automatically creates a directory for the volume on the host under the **<mark>/var/lib/docker/volume/</mark>** <mark> path</mark>. You can now mount this volume on a container, ensuring data persistence and data sharing among multiple containers.

For example, to create a volume under the name **data**, you would run the command:

`docker volume create data`

To verify you have successfully created a Docker volume, prompt Docker to list all available volumes with

`docker volume list`

To see more information about a Docker volume, use the `inspect` command

`docker volume inspect [volume_name]`

### 3\. Mounting a Data Volume

To mount a data volume to a container add the `--mount` flag to the `docker run` command. It adds the volume to the specified container, where it stores the data produced inside the virtual environment.

To run a container and mount a data volume to it, follow the basic syntax:

`docker run --mount source=[volume_name],destination=[path_in_container] [docker_image]`

Replace `[path_in_container]` with the path where you want to place the data volume in the container. Everything stored in that directory automatically gets saved on the data volume on the host as well. For ex-

`docker run -it --name=example1 --mount source=data,destination=/data ubuntu`

The command instructs Docker to run a container in interactive mode (`-it`) from the Ubuntu image, under the name `example1`, while mounting the volume data in the `/data` the directory inside the container.

Then, check to verify the volume was successfully mounted by listing the content of the container: `ls`

### 4\. Copying Files Between Containers From a Shared Volume

Let’s see how Docker volumes allow you to share files between containers.

To do so, we use the volume and container created in the previous section. This included running the commands:

`docker volume create data`

`docker run -it --name=example1 --mount source=data,destination=/data ubuntu`

1\. Once you have switched to the container command prompt, move to the data volume directory:

`cd data`

2\. Create an empty sample file using the touch command:

`touch sample1.txt`

3\. Now, exit the container:

`exit`

4\. Then, launch a new container **example2** with the same data volume:

`docker run -it --name=example2 --mount source=data,destination=/data ubuntu`

5\. List the content of the container. You should find the data directory, as in the **example1**:

`ls`

6\. Move to the data directory and list its content of it:

`cd data`

`ls`

The basic syntax for removing a Docker volume in the command line is:

`docker volume rm [volume_name]`

### 5\. **What is Docker Networking?**

Docker networking enables a user to link a Docker container to as many networks as he/she requires. Docker Networks are used to provide complete isolation for Docker containers.

Note: A user can add containers to more than one network.

Let’s move forward and look at the Advantages of networking.

When Docker is installed, a default bridge network named  `docker0` is created. Each new Docker container is automatically attached to this network, unless a custom network is specified.

### 6\. Advantages of Docker Networking

Some of the major benefits of using Docker Networking are:

* They share a single operating system and maintain containers in an isolated environment.
    
* It requires fewer OS instances to run the workload.
    
* It helps in the fast delivery of software.
    
* It helps in application portability.
    

### 7\. **Docker Network Types**

1. **Bridge Network:**
    
    Bridge networking is the most common network type. It is limited to containers within a single host running the Docker engine. Bridge networks are easy to create, manage and troubleshoot.
    
    For the containers on the bridge network to communicate or be reachable from the outside world, port mapping needs to be configured. As an example, consider you can have a Docker container running a web service on port  **80**. Because this container is attached to the bridge network on a private subnet, a port on the host system like  **8000**  needs to be mapped to port  80 on the container for outside traffic to reach the web service.
    
    `docker network create -d bridge mynetwork [network name]`
    
2. **Overlays Network :**
    
    An overlay network uses software virtualization to create additional layers of network abstraction running on top of a physical network. In Docker, an overlay network driver is used for multi-host network communication
    
    To create an overlay network named `my-overlay-net`, you’ll also need the `--subnet` parameter to specify the network block that Docker will use to assign IP addresses to the containers:
    
    `docker network create -d overlay --subnet=192.168.10.0/24 my-overlay-net`
    

### Task 1: **Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container ).**

By using the `docker-compose -d up` command to start the multi-container application in detached mode.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681830824076/1f597bd8-feb8-434d-b7f2-9aa4ea37fbfc.png?auto=compress,format&format=webp align="left")

Using the `docker-compose scale` command you can scale your container service.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681831048577/80b1e1ec-2f37-423f-9785-111bd4064ceb.png?auto=compress,format&format=webp align="left")

By using the `docker-compose ps` command you can check your running container status.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681831167363/78791c3a-7d3e-4951-94ca-ee611d0eae8b.png?auto=compress,format&format=webp align="left")

You can check your container logs of a particular service.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681831293483/11eea023-ba7e-4713-994f-d933d8d67cae.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681831375315/e6c9e49f-57c1-4cf3-87f0-f8398da9129b.png?auto=compress,format&format=webp align="left")

### Task 2 :

**Use Docker Volumes and Named Volumes to share files and directories between multiple containers.**

Create a volume as per your choice and then list the volume list.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681831886841/fb012420-0cd9-455b-b17f-52f315ccd491.png?auto=compress,format&format=webp align="left")

**Create two or more containers that read and write data to the same volume using the** `docker run --mount` **command.**

Create two containers and attached the same name volume to them by using the --mount flag.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681831986425/88044d3d-ee35-4e0f-8d6b-ba94f5377f0e.png?auto=compress,format&format=webp align="left")

**Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.**

Here you can see two containers are using the same data \[data1\].

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681832162224/1d58f093-2955-4938-be5a-1f72197442ce.png?auto=compress,format&format=webp align="left")

If you want to remove volume, first you have to stop both of the containers and remove them completely. Then you can remove the volume.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681832631541/202e1aa3-ebb8-44ed-8570-2c95c8398e39.png?auto=compress,format&format=webp align="left")

First, check the volume list by using the `docker volume ls` command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681832396553/3315358c-a1a2-4233-bb17-02bb5d383aaa.png?auto=compress,format&format=webp align="left")
