Implementation of mounting NFS shared directory in Docker container

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.htm introduced how to use Dockerfile to build Ubuntu 16.04 image and compile and execute Messy_Test project in the container. Here we introduce how to mount the shared directory on the NFS server in the container.

The Dockerfile content is as follows:

FROM ubuntu:16.04
LABEL maintainer="FengBingchun [email protected]" \
   version="1.0" \
   description="dockerfile test"
RUN dep_items='git g++-5 nfs-common make' \
  && apt-get update \
  && apt-get install -y $dep_items \
  && ln -s /usr/bin/g++-5 /usr/bin/g++ \
  && ln -s /usr/bin/gcc-5 /usr/bin/gcc \
  && mkdir -p /mnt/nfs \
  && rm -rf /var/lib/apt/lists/*

Build the image. After executing the following command, an image named fengbingchun/ubuntu:16.04 will be successfully generated:

docker build -t fengbingchun/ubuntu:16.04 .

By mounting the host directory, create a new container test and execute one of the following commands. The first one is recommended:

docker run --cap-add sys_admin -it -P --name test --mount type=bind,source=e:\GitCode\docker,target=/home/fengbingchun fengbingchun/ubuntu:16.04 /bin/bash
docker run --privileged=true -it -P --name test --mount type=bind,source=e:\GitCode\docker,target=/home/fengbingchun fengbingchun/ubuntu:16.04 /bin/bash

Execute the following commands in the container to mount, assuming that the nfs server IP is 10.107.2.1 and the shared directory is shared:

/etc/init.d/rpcbind start
mount -t nfs 10.107.2.1:/shared /mnt/nfs

Create a soft link and add the cmake executable file path to the environment variable, and execute the following commands in sequence:

ln -s /mnt/nfs/Ubuntu-16.04/ /usr/local/toolchains
echo "export PATH=/usr/local/toolchains/bin:$PATH" >> /etc/profile
source /etc/profile

Therefore, cmake was not installed when the image was created. After executing the source command, execute cmake --version to see the cmake version information, as shown in the following figure:

Then in the container, cd to the /home/fengbingchun directory, clone Messy_Test and execute the following command:

git clone https://github.com/fengbingchun/Messy_Test

Then cd to the Messy_Test/prj/linux_cmake_CppBaseTest directory and execute the following commands in sequence:

./build.sh
./build/CppBaseTest

The execution result is shown in the figure below, which shows that after the image built by Dockerfile is mounted in the container with the NFS shared directory, Messy_Test can be compiled and executed normally through cmake in the shared directory:

You can also view the directories shared by the NFS server in the container by executing the following command:

showmount -e 10.107.2.1

Save the image fengbingchun/ubuntu:16.04 to a tarball and execute the following command:

docker save -o ubuntu_16.04.tar fengbingchun/ubuntu:16.04

Copy ubuntu_16.04.tar to the Ubuntu system, load an image from the tarball, and execute the following command:

docker load -i ubuntu_16.04.tar

Then perform similar operations on Windows, compile and execute Messy_Test in the newly created container test, and execute the following commands in sequence:

docker run --privileged=true -it -P --name test --mount type=bind,source=/home/xxxx/Disk/GitHub/docker,target=/home/fengbingchun fengbingchun/ubuntu:16.04 /bin/bash
/etc/init.d/rpcbind start
mount -t nfs 10.107.2.1:/shared /mnt/nfs
ln -s /mnt/nfs/Ubuntu-16.04/ /usr/local/toolchains
echo "export PATH=/usr/local/toolchains/bin:$PATH" >> /etc/profile
source /etc/profile
cd /home/fengbingchun/
git clone https://github.com/fengbingchun/Messy_Test
cd Messy_Test/prj/linux_cmake_CppBaseTest/
./build.sh
./build/CppBaseTest

The execution result is shown in the figure below: It shows that after the image generated on Windows is packaged, it can be used normally after loading on Ubuntu:

Notice:

(1) On Ubuntu, use "--privileged=true" when creating a container, otherwise the error "mount.nfs: access denied by server while mounting 10.107.2.1:/shared" will be reported when mounting;

(2) On Windows, sometimes the container may get stuck. You can delete the container and then create a new one.

This is the end of this article about how to mount NFS shared directories in Docker containers. For more information about how to mount NFS shared directories in Docker containers, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker View the Mount Directory Operation of the Container
  • Docker - Summary of 3 ways to modify container mount directories
  • Docker mounts local directories and data volume container operations
  • How to mount the host directory in Docker container
  • How to use Docker to mount the container directory to the host

<<:  JavaScript to achieve lottery effect

>>:  HTML Tutorial: DOCTYPE Abbreviation

Recommend

Detailed explanation of Vue's custom event content distribution

1. This is a bit complicated to understand, I hop...

Nginx Location directive URI matching rules detailed summary

1. Introduction The location instruction is the c...

Solution to Tomcat server failing to open tomcat7w.exe

I encountered a little problem when configuring t...

In-depth understanding of React Native custom routing management

Table of contents 1. Custom routing 2. Tab naviga...

Details after setting the iframe's src to about:blank

After setting the iframe's src to 'about:b...

JavaScript function call, apply and bind method case study

Summarize 1. Similarities Both can change the int...

JavaScript canvas realizes the effect of nine-square grid cutting

This article shares the specific code of canvas t...

Solutions to browser interpretation differences in size and width and height in CSS

Let’s look at an example first Copy code The code ...

Tutorial on installing MySQL on Alibaba Cloud Centos 7.5

It seems that the mysql-sever file for installing...

MySQL query learning basic query operations

Preface MySQL is the most popular relational data...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

Node and Python two-way communication implementation code

Table of contents Process Communication Bidirecti...

Use of filter() array filter in JS

Table of contents 1. Introduction 2. Introduction...

mysql 5.7.23 winx64 decompression version installation tutorial

Detailed installation tutorial of mysql-5.7.23-wi...