Create a Samba Share and Use from in a Docker Container

At some point in either your cloud- or container-development life, you’re going to have to share a folder from the Linux server. You may only have to do this in a dev environment, where you want to be able to share files with other developers on a third-party, cloud-hosted instance of Linux. Or maybe file sharing is part of an app or service you are building.
And because Samba (the Linux application for Windows file sharing) is capable of high availability and scaling, it makes perfect sense that it could be used (by leveraging a bit of creativity) within your business, your app stack, or your services.
You might even want to use a Samba share to house a volume for persistent storage (which I’m going to also show you how). This could be handy if you want to share the responsibilities for, say, updating files for an NGINX-run website that was deployed via Docker.
Even if you’re not using Samba shares for cloud or container development, you’re going to need to know how to install Samba and configure it such that it can be used for sharing files to your network from a Linux server and I’m going to show you how it’s done.
There are a few moving parts here, so pay close attention.
I’m going to assume you already have Docker installed on a Ubuntu server but that’s the only assumption I’ll make.
How to Install Samba on Ubuntu Server
The first thing we have to do is install Samba on Ubuntu Server. Log into your instance and install the software with the command:
1 |
sudo apt-get install samba -y |
When that installation finishes, start and enable the Samba service with:
1 |
sudo sysemctl enable --now smbd |
Samba is now installed and running.
You then have to add a password for any user who’ll access the share. Let’s say you have the user Jack. To set Jack’s Samba password, issue the following command:
1 |
sudo smbpasswd -a jack |
You’ll be prompted to type and verify the password.
Next, enable the user with:
1 |
sudo smbpasswd -e jack |
How to Configure Your First Samba Share
Okay, let’s assume you want to create your share in the folder /data. First, create that folder with the command:
1 |
sudo mkdir /data |
In order to give it the proper permissions (so those users who need access), you might want to create a new group and then add users to the group. For example, create a group named editors with the command:
1 |
sudo groupadd editors |
Now, change the ownership of the /data directory with the command:
1 |
sudo chow -R :editors /data |
Next, add a specific user to that new group with:
1 |
sudo usermod -aG editors USER |
Where USER is the specific user name.
Now, make sure the editors group has write permission for the /data directory with:
1 |
sudo chmod -R g+w /data |
At this point, any member of the editors group should be able to access the Samba share. How they do that will depend on the operating system they use.
How to Create a Persistent Volume Mapped to the Share
For our next trick, we’re going to create a persistent Docker volume (named public) that is mapped to the /data directory. This is done with the following command:
1 |
docker volume create --opt type=none --opt o=bind --opt device=/data public |
To verify the creation, you can inspect the volume with the command:
1 |
docker volume inspect public |
The output will look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[ { "CreatedAt": "2023-07-27T14:44:52Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/public/_data", "Name": "public", "Options": { "device": "/data", "o": "bind", "type": "none" }, "Scope": "local" } ] |
Let’s now add an index.html file that will be housed in the share and used by our Docker NGINX container. Create the file with:
1 |
nano /data/index.html |
In that file, paste the following:
Save and close the file.
Deploy the NGINX Container
We can now deploy our NGINX container that will use the index.html file in our public volume that is part of our Samba share. To do that, issue the command:
1 |
docker run -d --name nginx-samba -p 8090:80 -v public:/usr/share/nginx/html nginx |
Once the container is deployed, point a web browser to http://SERVER:8090 (where SERVER is the IP address of the hosting server), and you should see the index.html file that we created above (Figure 1).
-
Figure 1: Our custom index.html has been officially served in a Docker container.
Another really cool thing about this setup is that anyone with access to the Samba share can edit the index.html file (even with the container running) to change the page. You don’t even have to stop the container. You could even create a script to automate updates of the file if you like. For this reason, you need to be careful who has access to the share.
Congrats, you’ve just used Docker and Samba together. Although this might not be a wise choice for production environments, for dev or internal services/apps, it could certainly come in handy.