Wednesday, June 26, 2024

How to Mount a Docker Container with a Volume on Different Servers

How to Mount a Docker Container with a Volume on Different Servers





 Mounting a Docker container with a volume that resides on a different server involves setting up network connectivity and configuring network file systems like NFS. This guide will help you set up an NFS to share a Docker volume across different servers. let my volume be available on Redhat and the container is running on Ubuntu


Step-by-Step Guide to Setting Up NFS


1. Install NFS Server on Red Hat

First, install and configure the NFS server on your Red Hat server:
commands:
sudo yum install nfs-utils
sudo systemctl enable nfs-server 
sudo systemctl start nfs-server

2. Export the Docker Volume Directory

Edit the NFS export configuration to share the Docker volume directory:
command:
sudo vi /etc/exports
add the docker volume path with the IP of the server that will access this (Ubuntu)
example : 
/var/lib/docker/volumes/testData/_data ip_of_ubuntu(rw,sync,no_subtree_check)



3. Restart NFS Service

Restart the NFS service to apply the changes:
command: sudo systemctl restart nfs-server

Mounting the NFS Volume on Ubuntu


4. Install NFS Client on Ubuntu

Install the NFS client utilities on your Ubuntu server:
commands:
sudo apt-get update 
sudo apt-get install nfs-common

5. Mount the NFS Share on Ubuntu

Create a mount point and mount the NFS share:
commands: 
sudo mkdir -p /mnt/tetData  #this is the place on ubunte where volume will be mount
sudo mount -t nfs 192.168.67.9:/var/lib/docker/volumes/tetData/_data /mnt/tetData
Replace 192.168.67.9 with the IP address of your Red Hat server.

Using the NFS Volume in Docker

6. Update Docker Compose Configuration

Modify your docker-compose.yml to use the NFS-mounted directory:
compose file syntax


Additional Considerations

  • NFS Security: Secure your NFS configuration based on your network and security requirements.
  • Persistent Mount: To make the NFS mount persistent across reboots, add an entry to /etc/fstab on your Ubuntu server.
  • command:  echo '192.168.67.9:/var/lib/docker/volumes/tetData/_data /mnt/tetData nfs defaults 0 0' | sudo tee -a /etc/fstab

Permission Issues

1. Verify and Adjust Permissions on the Red Hat Server:
   

   Ensure that the directory and its contents on the Red Hat server have the appropriate permissions. You need to set the ownership and permissions so that the Docker container running on Ubuntu can access it.


 commands:  

   sudo chown -R 999:999 /var/lib/docker/volumes/tetData/_data

   sudo chmod -R 755 /var/lib/docker/volumes/tetData/_data


 Here, `999:999` corresponds to the `mongodb` user and group ID used by the official MongoDB Docker image.

2. Check and Adjust Local Directory Permissions on Ubuntu


   Ensure the local mount point directory on Ubuntu has the correct permissions:


   commands

   sudo chown -R 999:999 /mnt/tetData

   sudo chmod -R 755 /mnt/tetData


3. Restart Services:


Restart the NFS server on the Red Hat machine to apply changes:

commands: 

sudo systemctl restart nfs-server

   


By following these steps, you can mount a Docker container on one server to a volume residing on another server, leveraging the NFS for seamless data sharing. Adjust configurations and permissions according to your specific setup and requirements.




Previous Post
Next Post

post written by:

0 comments: