Docker container's sshfs mount freezes, but only when mounted by Python

I am assuming you want to mount some server's directory to container's filesystem using SSHFS. You could add that instruction to the Dockerfile:

FROM ubuntu:18.04
RUN apt-get update && apt-get -y install stuff
COPY sshprivkey .ssh/sshprivkey
RUN mkdir /mnt/test
RUN sshfs username@server-worker01:/test_project/ /mnt/test -o IdentityFile=.ssh/sshprivkey,auto_cache,reconnect,transform_symlinks,follow_symlinks

You can add other stuff that you have in your Dockerfile below this. You should add sshprivkey file to your current directory.

Now if you build it:

docker build -t your_desired_image_name .

And then run a container using that image and check whether it worked:

docker run --privileged -it your_desired_image_name 

root@container_id$ ls /mnt/test

Remember to add that --privileged flag for it to work. I found it out from here.


Server Directory/Directories can be mounted directly to container's filesystem using SSHFS utility.

Dockerfile

FROM ubuntu:18.04
RUN apt-get update
COPY sshprivkey .ssh/sshprivkey
RUN mkdir /mnt/test
RUN sshfs username@server-worker01:/test_project/ /mnt/test -o IdentityFile=.ssh/sshprivkey,auto_cache,reconnect,transform_symlinks,follow_symlinks

Container is created using ubuntu:18.04 base image.

FROM ubuntu:18.04

Update package list. This command will be changed according to base image. here command can be updated to install required packages

RUN apt-get update

Copied sshprivkey to use with sshfs for permissions to mount server directly to container

COPY sshprivkey .ssh/sshprivkey

Where server directory should be mounted for that test directory is created

RUN mkdir /mnt/test

Run sshfs command with sshprivkey to mount server directory in previous created test directory

RUN sshfs username@server-worker01:/test_project/ /mnt/test -o IdentityFile=.ssh/sshprivkey,auto_cache,reconnect,transform_symlinks,follow_symlinks

To Build image from Dockerfile in current directory following command is used:

docker build -t your_desired_image_name .

Running interactive session container from previously created image following command it used:

docker run --cap-add SYS_ADMIN -it your_desired_image_name 

After container interactive session starts we can check that if it worked:

root@container_id$ ls /mnt/test

Here i have used --cap-add to give container permission to mount directory

Note: It is good to add limited permission as required using --cap-add and not using --privileged until nothing else works for security purpose has it is not good to give too much permissions to that container

You can read more about docker permissions here