Day 19 Docker for DevOps Engineers

Day 19 Docker for DevOps Engineers

Β·

4 min read

Docker-Volume:πŸ“‘βš‘

Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having the same data.

There are several reasons why you might want to use a volume in a Dockerfile βˆ’

  • Persisting data: If you have data that needs to persist even if the container is stopped or removed, you can store it in a volume. This is useful for things like database files or application logs.

  • Sharing data between containers: If you have multiple containers that need to share data, you can use a volume to allow them to access the same data. This can be useful for things like storing shared configuration files or data used by multiple containers.

  • Easing data management: By separating data from the container itself, volumes can make it easier to manage the data. For example, you can use a volume to store data that is generated by the container, and then mount the volume on a host system to easily access the data.

Architecture of Docker Volume:πŸ›ž

What is Docker Volume

  • Docker volumes are a mechanism for managing and persisting data in Docker containers.

  • They allow you to separate the storage of data from the lifecycle of containers, providing a way to store, share, and manage data in a more controlled and flexible manner.

  • In Docker, containers are designed to be lightweight and ephemeral, meaning they can be started, stopped, and destroyed without affecting the underlying data.

  • However, some applications require persistent data that should survive container restarts, updates, or removals.

  • Docker volumes address this need by providing a way to manage data independently of the container instances.

    Docker Network:🌐🌏

  • Docker Networking is the way in which Docker containers connect to other containers on the same host or on different hosts and also to the outside world, i.e., the internet.

  • Docker’s networking subsystem is pluggable using drivers. Several drivers exist by default and provide core networking functionality.

    Docker Networking Drivers - Details and Use Cases | Docker Blog

    There are various kinds of Docker networks such as:

    1. Default Bridge Network

    2. Custom bridge/user-defined bridge

    3. Host Network

    4. Macvlan network

    5. None Network

    6. Overlay Network

    7. IPVLAN Network.

πŸ›žβš“Tasks:

  • 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 )

Create a Docker-compose file for multiple container for create application setup Container and create database Container.This example includes configurations for both bringing the containers up and bringing them down using a single command.

creat docker Docker compose file docker-compose.yaml and add some content in this file.

version : "3.3"
services:
  my-web-app:
    container-name:"django-todo-app"
    build:
    ports:
      - "8001-8005:8001"
    volumes:
      - my_django_todo-volume:/app
  my-db:
    container-name:"django-todo-app"
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123"
volumes:
  my_django_volume:
    external: true

Now after adding the content in the the compose file and add enviorment. now run the compose file.

docker-compose -d up

This command will start both the application and database containers in detached mode (-d), allowing them to run in the background.

To bring down the containers, use the following command:

docker-compose down

This command will stop and remove the containers, along with any associated networks and volumes.

Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

You can create a named volume using the docker volume create command. This volume will store the shared data that can be accessed by multiple containers.

To check the volume list

To create a docker volume:

sudo docker create --name new-task

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

docker run -d  -p docker run -d -v new-task:/app/data <image_id>

Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.

docker volume ls

To inspect volumes, use:

docker volume inspect <new-task_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>

To remove docker volume.

docker volume rm volume-name

πŸ“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.

So I encourage you to try this on your own and let me know in the comment section about your learning experience

Thank you for reading!

Happy Learning πŸ˜ŠπŸ™Œ

Thank You! Stay Connected β˜οΈπŸ‘©β€πŸ’»πŸŒˆ

Contact me at :

LinkedIn: Akash Singh

linkedin.com/in/akash-singh-48689a176

E-mail:

Did you find this article valuable?

Support Akash-DevOps by becoming a sponsor. Any amount is appreciated!

Β