Mount an SMB share to a Docker container in two ways: using the Netshare plugin and by means of the local driver

Comment: the version of Docker - 19.03.11, Docker Compose - 1.26.0, Netshare - 0.36


Basic configuration


install Docker Engine — Community and Docker Compose as discussed here

create the /etc/docker/test folder for storing deployment settings of a test container

mkdir /etc/docker/test

create the /etc/docker/test/docker-compose.yml file with the deployment configuration of the container

version: '3.8'

networks:
  net:
    ipam:
      config:
        - subnet: 192.168.101.0/24

services:
  app:
    container_name: test
    image: alpine
    networks:
      net:
    stdin_open: true
    tty: true
    entrypoint: /bin/sh

Netshare plugin


download the plugin

curl -L https://github.com/ContainX/docker-volume-netshare/releases/download/v0.36/docker-volume-netshare_0.36_linux_amd64-bin -o /usr/local/sbin/docker-volume-netshare
chmod 700 /usr/local/sbin/docker-volume-netshare

create the /usr/lib/systemd/system/docker-volume-netshare.service unit file

[Unit]
Description=Docker Netshare Plugin
After=network.target
 
[Service]
Type=simple
ExecStart=/usr/local/sbin/docker-volume-netshare cifs
 
[Install]
WantedBy=multi-user.target

reload the systemd configuration

systemctl daemon-reload

start the service

systemctl start docker-volume-netshare

replace the content of the /etc/docker/test/docker-compose.yml file with the following

version: '3.8'

networks:
  net:
    ipam:
      config:
        - subnet: 192.168.101.0/24

volumes:
  vol:
    name: server.domain.example/share/
    driver: cifs
    driver_opts:
      cifsopts: username=usr,password=pwd

services:
  app:
    container_name: test
    image: alpine
    networks:
      net:
    volumes:
      - vol:/mount
    stdin_open: true
    tty: true
    entrypoint: /bin/sh

connect to the container and check to see if the share was successfully mounted

docker exec -t -i test /bin/sh
ls -l /mount


Local driver


replace the content of the /etc/docker/test/docker-compose.yml file with the following

version: '3.8'

networks:
  net:
    ipam:
      config:
        - subnet: 192.168.101.0/24

volumes:
  vol:
    driver: local
    driver_opts:
      type: cifs
      device: //10.1.1.1/share
      o: username=usr,password=pwd

services:
  app:
    container_name: test
    image: alpine
    networks:
      net:
    volumes:
      - vol:/mount
    stdin_open: true
    tty: true
    entrypoint: /bin/sh

Comment: when using the local driver one should specify the IP address of the server instead of its DNS name otherwise the container woun't start

connect to the container and check to see if the share was successfully mounted

docker exec -t -i test /bin/sh
ls -l /mount

Leave a Reply