Making CIFS Shares available to Docker
17 Oct 2024Introduction
Mounting CIFS (SMB) shares in Linux can be a convenient way to access network resources as part of the local filesystem.
In this guide, I’ll walk you through the steps for properly configuring a CIFS share in /etc/fstab
on a Linux system.
I’ll also show you how to ensure that network mounts are available before services like Docker start up.
Step 1: Modify /etc/fstab
To mount a CIFS share automatically at boot, we need to modify the /etc/fstab
file. First, open it in a text editor:
Now, add or modify the CIFS entry in the file. A typical CIFS entry looks like this:
Explanation:
//server_address/share_name
: The remote server and share you want to mount (e.g.,//192.168.1.100/shared
)./local/mount/point
: The local directory where the share will be mounted.cifs
: The filesystem type for CIFS/SMB.credentials=/path/to/credentials
: Points to a file containing your username and password (this is optional, but recommended for security).file_mode=0755,dir_mode=0755
: Sets the file and directory permissions for the mounted share.uid=1000,gid=1000
: Specifies the user and group IDs that should own the files (replace1000
with your user/group IDs)._netdev
: Ensures that the mount waits for network availability before mounting.0 0
: The last two values are for dump and fsck; they can usually remain0
.
Step 2: Create a Credentials File
For better security, you can use a separate credentials file rather than hard-coding the username and password in /etc/fstab
. To do this, create a file to store the username and password for the share:
Add the following lines to the file:
Make sure the credentials file is secure by setting appropriate permissions:
This ensures only the root user can read the file, which helps protect sensitive information.
Step 3: Test the Mount
After adding the CIFS line to /etc/fstab
and configuring the credentials file, it’s time to test the mount. You can do this by running:
If everything is configured correctly, the CIFS share should mount automatically. If you encounter any issues, check the system logs for errors. Use one of these commands to inspect the logs:
Ensuring Mounts are Available Before Docker
If you’re running Docker on the same system and need to ensure that your CIFS mounts are available before Docker starts, you’ll want to modify Docker’s systemd service. Here’s how:
First, create a directory for Docker service overrides:
Next, create a custom override file:
Add the following content:
This configuration ensures Docker waits until all remote filesystems (like CIFS) are mounted before starting.
Finally, reload the systemd configuration and restart Docker:
Now, Docker will wait for your CIFS mounts to be available before starting any containers that might rely on them.
By following these steps, you can ensure your CIFS shares are mounted reliably on boot and integrated seamlessly with other services like Docker. This is especially useful for network-based resources that are critical to your containers or other local services.