docker mount SMB/CIFS folder with user credentials into container

Viewed 28

How to mount a network folder into a docker container? I have a Python-based container that should manipulate files in a shared network drive. How to properly access this drive in/from a container?

In non-containerized code, I would simply use samba to access the shared network drive: smb://<username>:<password>@//server/path/to/folder But this is not the ideal way (neither in maintainability nor the docker-way. It will actually be suppressed by docker.) Note that <username> and <password> do not necessary belong to the user that hosts the docker server.

To my knowledge, there are different options to do this.

  1. Try mounting the file in the Python code with samba and wrap the code to a docker-container. However, I'm actually not sure is this is possible at all since docker is supposed to block this for security reasons.
  2. Mount it as a virtual volume via docker(-compose) with the samba-path
version: "3.8"

services:
  my-service:
    image: my-service  # FROM python:3.9.14-slim-bullseye docker image
    volumes:
      - shared-networkdrive:/media/shared-networkdrive
volumes:
  shared-networkdrive:
    driver_opts:
      type: cifs
      device: smb://<username>:<password>@//server/path/to/folder

provokes the following error:

Attaching to climate-factor-update Error response from daemon: error while mounting volume '/var/lib/docker/volumes/shared-networkdrive/_data': failed to mount local volume: mount :@//server:/var/lib/docker/volumes/shared-networkdrive/_data: invalid argument

  1. Mount it as a virtual volume via docker(-compose) with driver options
version: "3.8"

services:
  my-service:
    image: my-service  # FROM python:3.9.14-slim-bullseye docker image
    volumes:
      - shared-networkdrive:/media/shared-networkdrive

volumes:
  shared-networkdrive:
    driver_opts:
      type: cifs
      o: username=<username>,password=<password>
      device: //server/path/to/folder

provokes this error

Attaching to climate-factor-update Error response from daemon: error while mounting volume '/var/lib/docker/volumes/shared-networkdrive/_data': failed to mount local volume: mount :@//euro1/MUC:/var/lib/docker/volumes/shared-networkdrive/_data: invalid argument

What is the right, "dockernic" way and what am I missing here? There is a question on this, which is rather abstract for me.

0 Answers
Related