Docker Volume And Docker Network

Docker Volume And Docker Network

Day 19 : #90DaysOfDevOps Challenge

What is Docker Volume..?

Docker-Volume is a feature in the Docker containerization platform that enables data to persist between containers and to be shared among them. When you create a Docker container, any data that is generated or used by the container is stored inside the container itself. However, when the container is deleted, the data is also deleted.

To solve this problem, Docker-Volume was introduced. It allows you to store the data in a separate location outside the container, making it independent of the container’s lifecycle. This way, even if the container is deleted, the data remains accessible and can be used by other containers as well.

Docker-Volume can be used to manage the storage requirements of your containers. It enables you to easily manage the data for your applications, such as databases, log files, and other persistent data. Docker-Volume can also be used to store configuration files, templates, and other files that are required by the container.

Overall, Docker-Volume is a powerful feature that allows for flexible and scalable data management in Docker containers.

How to use volumes to share data between Docker containers | by Edouard  Courty | Medium

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.

Volumes on the Docker host

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

What is Docker Network..?

Docker network is a feature in the Docker containerization platform that enables the communication between containers running on the same host or across multiple hosts. It provides a virtual network that connects containers to each other and to external networks, allowing them to communicate securely and efficiently.

When you create a Docker container, it is isolated from the host system and other containers by default. To enable communication between containers, you can create a Docker network and attach containers to it. Once the containers are attached to the same network, they can communicate with each other by their container names or IP addresses.

Docker network provides several types of network drivers, including Bridge, Host, Overlay, Macvlan, and none. The bridge is the default network driver and allows containers to communicate with each other on the same host. Host allows containers to use the host network stack, while Overlay enables the communication between containers on different hosts. Macvlan allows containers to have a unique MAC address and appear as physical devices on the network, while none disables networking for the container.

Docker network also supports network segmentation and isolation, allowing you to create multiple networks and assign containers to specific networks based on their function or security requirements. This helps to improve the security and performance of your Docker environment.

Overall, the Docker network is a powerful feature that enables communication between containers and provides flexible and secure networking options for your Docker environment.

what are the different types of docker networking drivers | Edureka  Community

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

Tasks

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 my previous blog.

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

version : "3.3"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123"

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 the 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

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>

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

Thank you for reading!!!
~Bhaarat