Day 19 Docker Volume & Docker Network | Docker for DevOps

Mudit Mathur
6 min readAug 15, 2023


1. Docker Volumes πŸ“¦
2. Why Volumes over Bind Mount? πŸ”„
3. Commands related to Docker Volumes ⌨️
4. Docker Network 🌐
5. Commands related to Docker Network ⌨️
6. Tasks πŸ“‹
6.1 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) πŸš€
6.2 Task 2: Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers. πŸ“‚

Until now, I have learned how to create a docker-compose.yml file and push it to the repository. Let’s move forward and delve deeper into other Docker-compose.yml concepts like Docker Volume and Docker Network.

1. Docker Volumes πŸ“¦

Before I explain Docker Volumes, here is the link to the official documentation of Docker Volumes.

In simple terms, volumes are the preferred way to store and keep data that is created or used by Docker containers.

When you run containers with Docker, they can generate or rely on certain data, such as configuration files, databases, or application outputs. Volumes provide a convenient and reliable method to persist this data. By using volumes, it is ensured that the data remains available even if the containers are stopped, restarted, or removed. It acts as a separate storage space specifically for container data, making it easy to manage and share data between containers as needed.

There is a concept called Bind Mounts. Bind mounts are a way to access and share files or directories between the host machine and a Docker container. Unlike volumes, which are managed by Docker and stored within the Docker environment, bind mounts link specific locations on the host file system directly to the container.

2. Why Volumes over Bind Mount? πŸ”„

Volumes have several advantages over bind mounts:

  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers.
  • Volume drivers let you store volumes on remote hosts or cloud providers, encrypt the contents of volumes, or add other functionality.
  • New volumes can have their content pre-populated by a container.
  • Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.
  • Unlike a bind mount, you can create and manage volumes outside the scope of any container.

3. Commands related to Docker Volumes ⌨️

Create a volume:

docker volume create my-vol

List volumes:

docker volume ls

Inspect a volume:

docker volume inspect my-vol

Remove a volume:

docker volume rm my-vol

Attach a Volume to a Container:

docker run -v myvolume:/path/in/container myimage

Detach a Volume from a Container:

To detach a volume from a running container, you need to stop and remove the container. The volume will still exist and can be attached to other containers if needed.

docker container stop imagename
docker container rm imagename

4. Docker Network 🌐

Docker networking allows containers to communicate with each other and with external networks.

It provides a way for containers to establish connections, share information, and collaborate within a Docker environment.

5. Commands related to Docker Network ⌨️

Create a Network:

docker network create mynetwork

List Networks:

docker network ls

Inspect a Network:

docker network inspect mynetwork

Connect a Container to a Network:

docker run --network mynetwork myimage

Disconnect a Container from a Network:

docker network disconnect mynetwork mycontainer

Remove a Network:

docker network rm mynetwork

6. Tasks πŸ“‹

6.1 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) πŸš€

Let us set up a multi-container Docker Compose file that brings up an application container and a database container, allowing you to start and stop them together.

For that, the docker-compose needs to be installed. I have given a detailed explanation of the installation of docker-compose in the blog: Docker Compose Installation.

So let us start with the docker-compose.yml file:

version : "3.3"
image: nginx:latest
- "80:80"
- db
image: mysql
- "3306:3306"

In this example, I have defined two services: web and db. The web service represents the application container and the db service represents the database container.

Now using docker-compose up we can start both containers:

docker-compose up

Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in deployment file for auto-scaling.

docker-compose up --scale web=4

To scale down you can use:

docker-compose up --scale=2

To down the docker-compose:

docker compose down

6.2 Task 2: Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers. πŸ“‚

To create a docker volume:

sudo docker volume create --name myvol_1

To mount the volume in a container:

docker run -d -v myvol_1:/app/data <image_id>

For shared volumes, include it in the docker-compose.yaml file:

To list all the volumes, you can use:

docker volume ls

To inspect volumes, use:

docker volume inspect <vol_name>

By using the docker exec command, I have verified that the data in both the containers is same:

docker exec -it <cont_ID> <command_oryou_can_start_bash>

I have used ls for the listing of all directories.

Since I have completed my task, and no more need the docker volume I am going to delete the volumes:

docker volume rm volume_name

In conclusion, Docker volumes provide a way to persist and share data between containers in a Docker environment. They offer several advantages over bind mounts. When using Docker volumes, you can either create anonymous volumes or named volumes. Anonymous volumes are created automatically by Docker, while named volumes are explicitly defined in the Dockerfile or Docker Compose file.

In this blog, we explored the key features and common use cases of Docker Volumes. If you have any questions or would like to share your experiences, feel free to leave a comment below. Don’t forget to read my blogs and connect with me on LinkedIn and let’s have a conversations.

#Day19 #90daysofdevops