Use Docker to build NFS for sharing files between containers

Source: Internet
Author: User
Tags docker run

Let's start with today's two main characters: NFS and Docker

What is NFS?

The NFS (Network File system), which is one of the file systems supported by FreeBSD, allows computers in the network to share resources across TCP/IP networks. In an NFS application, a local NFS client application can transparently read and write to files located on the remote NFS server, just as you would access a local file. Excerpt from Baidu Encyclopedia

What is Docker?

This is not much to say, nearly two years a very popular thing oh.

After the introduction of the protagonist, then to say why to use Docker to build NFS, in fact, the title of this article has been explained, the main purpose is to achieve the sharing of files between containers. All you need to know about Docker is that Docker can support the container directory to mount to the host. With NFS, you can share the directory implementation between containers. If you have more than one container needs to share files, this will be an attempt to the scenario, the specific use of the scene to see the imagination, the main practice here today how to implement this function.

The knowledge involved
    • NFS Installation
    • Docker Inter-container communication
    • Docker Privileged
    • Dockerfile
    • Docker image
Writing Dockerfile
FROM ubuntuENV DEBIAN_FRONTEND noninteractiveRUN apt-get update -qq && apt-get install -y nfs-kernel-server runit inotify-tools -qqRUN mkdir -p /exportsVOLUME /exportsEXPOSE 111/udp 2049/tcp
Making a Docker image
 docker build -t=scottkiss/nfs .

Running the command takes a while to execute after success

docker images

You'll see a local Docker image that just created NFS

Configure and run Server for NFS

Perform

docker run -it --name nfs-server --privileged scottkiss/nfs

Enter the container terminal after execution

Modify Configuration

vi /etc/exports

Join at the end

/exports *(rw,sync,no_subtree_check,fsid=0,no_root_squash)

And then execute

exportfs -r

Then start the Rpcbind service

service rpcbind start

Finally start the NFS service

service nfs-kernel-server start

At this point, no accident, has successfully started the NFS server, and the/exports directory is shared out. Here, the author at the beginning of the encounter a pit, is the start time need to add –privileged parameters, or start the NFS service will prompt insufficient rights error. The main thing is that the mount operation is involved in this process, and the use of this parameter allows root within the container to have a real root authority, so that no error is made.

Configure and start the client

The client is much simpler and does not need to be configured like a server. Perform

docker run -it --link nfs-server:nfs  --privileged scottkiss/nfs

Enter the container terminal. And then execute

service rpcbind start

Then execute the remote mount command

mount -t nfs -o proto=tcp,port=2049 $NFS_PORT_2049_TCP_ADDR:/exports /home

In this way, the server's exports directory is attached to the client's home directory.

Can not wait to try, such as in the server terminal/exports directory to add a file, in the client's/home will also be added synchronously.

The main concern here is the –link parameter, which tells the Docker container to use the Nfs-server container and name its alias NFS. In this way, you can use $NFS_PORT_2049_TCP_ADDR to get the server IP.

Summarize

All of the above is a rough solution for testing, and there are many improvements, including dockerfile, which are used in a simple and crude way to facilitate experimentation.

# #文档信息

    • Copyright Disclaimer: Free Reprint-Non-commercial-non-derivative-retain attribution | Creative Commons by-nc-nd 3.0
    • Original URL: http://www.cocosk.com/articles/2016⁄2/16/docker-nfs.html
    • Lying Snow Sirk

Use Docker to build NFS for sharing files between containers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.