Home / Technology / Create a Samba Share and Use from in a Docker Container

Create a Samba Share and Use from in a Docker Container

Overview

This article provides a step-by-step guide on how to create a Samba share from within a Docker container using Ubuntu Server as the host operating system. The tutorial covers two main topics:

Installing and configuring Samba on an Ubuntu server

  • Install Samba with sudo apt-get install samba -y
  • Start and enable the Samba service
  • Set a password for users who will access the share

Creating a persistent Docker volume mapped to the Samba share:

  • Create a new group and add users to it, setting permissions accordingly
  • Create a persistent Docker volume with docker volume create –opt type=none –opt o=bind –opt device=/data public

Deploying an NGINX container using the Docker volume:

  • Mount the Docker volume to the /usr/share/nginx/html directory in the NGINX container
  • Run a new NGINX instance with docker run -d –name nginx-samba -p 8090:80 -v public:/usr/share/nginx/html nginx

Testing the setup:

  • Verify that the index.html file is served correctly from the Samba share

The article concludes by noting that this setup may not be suitable for production environments, but it can be useful for development or internal services/apps.

Key takeaways:

  • Install and configure Samba on an Ubuntu server
  • Create a persistent Docker volume mapped to a shared directory
  • Deploy an NGINX container using the Docker volume
  • Test the setup and verify that files are served correctly from the Samba share

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:

sudo apt-get install samba -y

When that installation finishes, start and enable the Samba service with:

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:

sudo smbpasswd -a jack

You’ll be prompted to type and verify the password.

Next, enable the user with:

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:

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:

sudo groupadd editors

Now, change the ownership of the /data directory with the command:

sudo chow -R :editors /data

Next, add a specific user to that new group with:

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:

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:

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:

docker volume inspect public

The output will look something like this:

[
{
"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:

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:

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.

FAQ for Samba Shares

General Questions

  1. What is Samba? Samba is a free, open-source software implementation of the SMB/CIFS protocol, which allows users to access and share files between different operating systems, including Windows, Linux, and macOS.
  2. Why use Samba shares? Samba shares provide a convenient way to share files between devices on your network, making it easy for multiple users to collaborate on projects or access shared resources.

Setting Up Samba Shares

  1. How do I set up a new Samba share? To set up a new Samba share, you’ll need to:
    • Create a directory on your server that will be accessible via the network
    • Edit the /etc/smb.conf file to add a new share definition for the directory
    • Restart the Samba service to apply changes
  2. What permissions should I set up for my Samba shares? You’ll want to set up read-only and read-write permissions as needed, depending on your specific use case.

Accessing and Managing Samba Shares

  1. How do users access a Samba share? Users can access a Samba share using the network path of the share (e.g., serversharename) or by browsing to the shared directory.
  2. Can I set up password protection for my Samba shares? Yes, you can set up password protection for your Samba shares using Kerberos authentication.

Security and Best Practices

  1. How do I secure a Samba share from unauthorized access? To secure a Samba share, use:
    • Strong passwords
    • Limited network connections to the server
    • Encryption (e.g., SSL/TLS)
  2. What are some common security risks associated with Samba shares? Common security risks include:
    • Unauthorized access via weak passwords or default settings
    • Malware transmission through infected files shared over the network

Troubleshooting

  1. Why won’t my Samba share connect to the server? Check that your client is configured correctly and that there are no firewall issues blocking connections.
  2. What if I’m experiencing permission issues accessing a Samba share? Check the permissions set on both the shared directory and any intermediate directories in the path.

Additional Tips

  1. Can I use Samba shares with Docker containers? Yes, you can use Docker volumes to mount external storage for your containers.
  2. How do I integrate my Linux distribution’s native file system features (e.g., fstab) with Samba shares? You’ll need to configure the relevant settings in your /etc/fstab or smb.conf files.

Release Notes

  • Always consult the official Samba documentation for up-to-date information on releases and security patches.
  • Follow best practices to ensure secure sharing of files across your network.

 

The post Create a Samba Share and Use from in a Docker Container appeared first on The New Stack.