Skip to main content

Command Palette

Search for a command to run...

Docker Volume and Docker Network

Day19 #90daysofdevops challenge

Published
5 min read
Docker Volume and Docker Network
Y

Hi there! My name is Yashswini and I'm a Linux Engineer with 3 years of experience in automation, continuous integration, and deployment. My expertise lies in DevOps and Cloud Computing with a focus on AWS. I have hands-on experience in various tools and technologies related to infrastructure automation, managed services, containerization, and monitoring and logging. I have a passion for troubleshooting and resolving issues and I'm always eager to learn and explore new technologies

1. Docker Volumes

Docker volumes are file systems mounted on Docker containers 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 /var/lib/docker/volume/ path. 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.

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

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

You can check your container logs of a particular service.

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.

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.

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].

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.

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