Share Data Between Machines

When you deploy a fault tolerance application you need to give permission to the multiple replicas of the same service to the same files for that you can use some services like S3, NFS, and so on as shown in the following figure.

Use a volume driver

You can create the docker volumes by using the following command before creating a container. If you want to create a volume while creating the container you achieve that by using the volume drivers in the docker. You can create a volume by using a volume driver as shown in the following.

 docker volume create --driver vieux/sshfs <name of the vlome you want to create> 

Create a Service Which Creates an NFS Volume

You can create an NFS volume while creating the service as mentioned below.

version: '3'
services:
nfs-server:
image: tomact:latest
container_name: nfs_container
volumes:
- /data:/nfs_share

nfs-client:
image: busybox
container_name: nfs_client
command: tail -f /dev/null
volumes:
- nfs-data:/data

volumes:
nfs-data:
driver_opts:
type: nfs
  • NFS Server:
    • Image: Uses an image named “tomact” (probably a typo for “tomcat”) with the latest version.
    • Container Name: Sets the name of the container running the NFS server to “nfs_container”.
    • Volumes: Maps the host directory “/data” to the container directory “/nfs_share”, allowing data to be shared between the host and the container.
  • NFS Client:
    • Image: Uses the “busybox” image, which provides a minimal Linux environment.
    • Container Name: Sets the name of the container running the NFS client to “nfs_client”.
    • Command: Specifies a command to keep the container running (“tail -f /dev/null”).
    • Volumes: Creates a named volume named “nfs-data” and mounts it to the “/data” directory inside the container. This volume uses the NFS driver for sharing data with the NFS server.
  • Volumes:
    • nfs-data: Defines a named volume named “nfs-data”.
    • Driver Options: Specifies options for the volume driver, setting the type to “nfs”. This indicates that the volume will be managed by the NFS (Network File System) driver, allowing the container to access files stored remotely on an NFS server.

Mounting a Volume Inside Docker Container

When you are working on a micro-service architecture using Docker containers, you create multiple Docker containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the containers separately, it might lead to an unnecessary increase in the image size, and also, changing a file in one container will not create the corresponding change in the same file in other containers.

Hence, you required a shared directory or volume that you can mount on multiple Docker Containers, and all of them have shared access to a particular file or directory. Docker allows you to mount shared volumes in multiple containers. In this article, we will mount a volume to different containers and check whether the changes in the file are shared among all the containers or not.

Similar Reads

Volumes

Volumes in docker are the preferred way to deploy a stateful set application in the form of containers. You can manage and persist the docker data outside of the docker life cycle. Volumes are completely managed by docker, unlike the bind mounts. Docker volumes will allow you to share the data between the docker containers and the docker host. They are two types of volumes in the docker. You store the data in Docker....

Create and manage volumes | Step By Step Instructions

To know more about Docker commands refer to the Docker – Instruction Commands....

Populate a Volume Using a Container

You can populate the volume by following the below steps....

Share Data Between Machines

When you deploy a fault tolerance application you need to give permission to the multiple replicas of the same service to the same files for that you can use some services like S3, NFS, and so on as shown in the following figure....

How to Use Docker Volumes to Persist Changes

Using Docker volumes is a fundamental technique to persist changes made within containers. Here’s how you can do it:...

Conclusion

To conclude, in this article, we discussed how to create and inspect a Volume and mount it to multiple Docker Containers. This proves to be very helpful when you want shared access to files and directories among multiple Docker Containers....

Docker Containers – FAQs

How to mount a volume in docker?...