Docker Containers Share Data

Introduction

In this blog, we will explore various methods of data sharing in Docker containers and how they can be effectively used in your projects. Now, let go Docker Containers Share Data.

Docker containers can share data using various mechanisms provided by Docker. Here are a few ways to share data between Docker containers.

I will deploy 2 containers using docker to share data between containers.

Prerequisites

  • Host OS: Ubuntu 21.04
  • Installed Docker

Create a Volume for Docker containers share data

Volumes are a fundamental feature in Docker that enable data persistence and sharing between containers and the host system.

When you create a volume, it acts as a dedicated storage location that can be mounted into one or more containers. The data stored in a volume persists even if the container using it is removed.

We will create a Volume to save our data as command below:

docker volume create --name persistent-data-devops
Create a Volume for Docker containers share data

The volume is created in the /var/lib/docker/volumes directory.

vagrant@devopsroles:~$ sudo ls /var/lib/docker/volumes/persistent-data-devops/_data/test.txt
/var/lib/docker/volumes/persistent-data-devops/_data/test.txt

For example, We will deploy the first container which will use the persistent volume.

docker run -it --name=container1 -v persistent-data-devops:/data ubuntu:latest
deploy the first container
  • The container named: container1
  • mount the persistent-data-devops volume into the /data directory within the new container

Login new container and create a new file

echo "Hello, www.devopsroles.com" >> /data/test.txt
Login new container and create a new file

We’ll now deploy a second container as command below

docker run -it --name=container2 -v persistent-data-devops:/data ubuntu:latest

Login second container and display content

cat /data/test.txt

Edit /data/test.txt file. I use the vim command line.

Add the following at the bottom of the file:

This data share between containers 1 and 2

The output terminal as below

vagrant@devopsroles:~$ docker run -it --name=container2 -v persistent-data-devops:/data ubuntu:latest

root@ed16b6be95f8:/# cat /data/test.txt
Hello, www.devopsroles.com

root@ed16b6be95f8:/# vim /data/test.txt
root@ed16b6be95f8:/# cat /data/test.txt
Hello, www.devopsroles.com
This data share between containers 1 and 2

Exit the running container with the exit command. You can stop and remove them with the commands:

docker stop ID
docker rm ID
Docker Containers Share Data

After stopping and removing docker container1, we will deploy again, But data will remain data.

How to Share Data Between Docker Containers

Youtube: Docker Containers Share Data

Conclusion

You have to Docker Containers Share Data. These are some of the common methods to share data between Docker containers. Choose the one that best suits your requirements based on the nature of the data and the use case you have in mind.

Data sharing is a crucial aspect of Docker containerization. By utilizing volumes, bind mounts, named volumes, Docker Compose, and Docker networks, you can effectively share data between containers and the host system. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.