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

Small details of web front-end development

1 The select tag must be closed <select><...

Record a slow query event caused by a misjudgment of the online MySQL optimizer

Preface: I received crazy slow query and request ...

CSS example code to hide the scroll bar and scroll the content

Preface When the HTML structure of a page contain...

js array entries() Get iteration method

Table of contents 1. Detailed syntax of entires()...

How to write high-quality JavaScript code

Table of contents 1. Easy to read code 1. Unified...

A brief discussion on the role of the docker --privileged=true parameter

Around version 0.6, privileged was introduced to ...

Steps to purchase a cloud server and install the Pagoda Panel on Alibaba Cloud

Alibaba Cloud purchases servers Purchase a cloud ...

Mysql 5.7.18 Using MySQL proxies_priv to implement similar user group management

Use MySQL proxies_priv (simulated role) to implem...

MySql index detailed introduction and correct use method

MySql index detailed introduction and correct use...

Three ways to avoid duplicate insertion of data in MySql

Preface In the case of primary key conflict or un...

Application of anchor points in HTML

Set Anchor Point <a name="top"><...

How to uninstall MySQL 8.0 version under Linux

1. Shut down MySQL [root@localhost /]# service my...