GlusterFS Volume as Samba Share

Published on Tuesday, July 19, 2016

We made Docker Container using a Dockerfile in the GlusterFS post that can mount a GlusterFS volume (running on Raspberry Pis); lets extend our Dockerfile and add Samba Server to expose the mounted directory as Samba Share so it can be accessed from Windows. For this we need to add these additional lines into the Dockerfile

RUN apt-get -y install samba

EXPOSE 138/udp
EXPOSE 139
EXPOSE 445
EXPOSE 445/udp

We are installing samba and exposing the TCP / UDP ports that Samba uses; if we build and run this container; we need to expose these ports using -p 138:138/udp -p 139:139 -p 445:445 -p 445:445/udp parameters in docker run command. After running it; to expose the directory through Samba; we need to add the following lines into /etc/samba/smb.conf (at the end)

[data]
path = /data
read only = no

Samba uses its own password files; to add the root user into it; run smbpasswd –a root and finally restart the Samba Daemon using service smbd restart Now if we use \\DOCKERMACHINE from the Windows; we should see data share and can access it using root and entered password. These are lots of manual steps after running the container; to solve this; lets create a setup.sh shell script that we will add into the container (through Dockerfile); we will use Environment Variables as we can pass them in dr run command. Our final docker run command will look like this

docker run --name glustersamba --cap-add SYS_ADMIN --device /dev/fuse --rm -e glusterip=Gluster-Server-IP -e glusterhost=Gluster-Server-FriendlyName -e glustervolume=Gluster-Volume-Name -p 138:138/udp -p 139:139 -p 445:445 -p 445:445/udp -it khurramaziz/gluster:3.5.2-samba

  • Notice the three environment variables, glusterip, glusterhost and glustervolume that are passed using –e
  • Notice the Samba ports being exposed using –p
  • Notice that SYS_ADMIN capability and /dev/fuse device is added; required for glusterfs client / mounting
  • khurramaziz/gluster:3.5.2-samba exists on Docker Registry; you can go ahead and run the above command and it will download the image layers; you upload the created image using docker push imagename:tag

If you are not interested how the image is made up; you can skip the remaining post; as I have pushed this image on to the Docker Hub Registry and you can issue the above command and it will work!

Here’s the setup.sh that’s using the above three environment variables to mount the GlusterFS volume at /data and then exposing it through Samba

#!/bin/sh
smbpath="/etc/samba/smb.conf"
echo $glusterip $glusterhost >> /etc/hosts
mkdir /data
mount -t glusterfs $glusterhost:$glustervolume /data
smbpasswd -a root
echo [data] >> $smbpath
echo path = /data >> $smbpath
echo read only = no >> $smbpath
service smbd restart

And here’s the Dockerfile that’s adding the above setup.sh and running it on start up using CMD directive

FROM ubuntu
MAINTAINER Khurram <khuziz@hotmail.com>

RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install software-properties-common python-software-properties
RUN apt-get -y install libpython2.7 libaio1 libibverbs1 liblvm2app2.2 librdmacm1 fuse
RUN apt-get -y install curl nano
RUN curl -sSL
https://download.gluster.org/pub/gluster/glusterfs/3.5/3.5.2/Debian/jessie/apt/pool/main/g/glusterfs/glusterfs-common_3.5.2-4_amd64.deb > glusterfs-common_3.5.2-4_amd64.deb
RUN curl -sSL
https://download.gluster.org/pub/gluster/glusterfs/3.5/3.5.2/Debian/jessie/apt/pool/main/g/glusterfs/glusterfs-client_3.5.2-4_amd64.deb > glusterfs-client_3.5.2-4_amd64.deb
RUN dpkg -i glusterfs-common_3.5.2-4_amd64.deb
RUN dpkg -i glusterfs-client_3.5.2-4_amd64.deb

RUN apt-get -y install samba

EXPOSE 138/udp
EXPOSE 139
EXPOSE 445
EXPOSE 445/udp

ADD setup.sh /setup.sh
RUN chmod +x /setup.sh

CMD /setup.sh && /bin/bash

Ideally; if we are following Micro Services Architecture; we should have a separate container for Samba Server; the GlusterFS Client Container will act as a producer exposing the mounted GlusterFS volume and Samba Server Container acting as Consumer exposing that volume as Samba Share. Sadly this is not possible (or atleast I dont know any way) as Docker Volume that get created will have the files that are there before we mount GlusterFS volume. When the GlusterFS volume is mounted into the producer container; the consumer container will continue to see the “before files + directories” and not what’s in the GlusterFS volume

  • Docker has plugins support, there are Volume Plugins using which we can create the Volumes that gets stored according to the used plugin / driver. There also exist GlusterFS volume plugins that we can use; we will not require the GlusterFS Client Container; instead host will mount the volume and such volumes can be used as Docker Volume in the containers

image

A proof of concept of producer / consumer implementation using Docker Volume

  • Notice the producer is Ubuntu and consumer is CentOS
  • Notice for the producer container run command; name is defined as its required for consumer container run command’s –volumes-from section
  • Notice for the producer container volume only target path is defined; it will create a Docker volume automatically and map as the defined path into the container; this volume / directory will get stored out of the Docker’s Union File System and given the name that can be used in other containers if they are run using –volumes-from

Resources