I've set up Samba in a Docker container for sharing folders with guest access, but I'm having trouble adding persistent users in the Docker environment. I know that a Samba user needs to be a local user on the machine, but I'm a bit lost on how to make that work in a disposable Docker setup. I've created bind mounts for /var/lib/samba (where Samba stores user info) and /etc/samba (for smb.conf). Should I also bind mount the /home folder and other locations where Linux defines users? Any suggestions would be greatly appreciated!
3 Answers
You shouldn't bind mount /home or touch anything in /etc for this. If you're using a pre-built image, you should set up users via environment variables. Guest access works because it's already set up in both the host's Samba configuration and the container, likely by default. If you built the image, try using a script during the build process to run useradd and smbpasswd commands to sync up users with your host machine's Samba setup.
I haven't done this myself, but I'd guess you'd need to bind mount the entire /etc directory because the useradd command does some specific file management when creating new users. Just mounting only the relevant files like passwd, group, and shadow might not work as expected. If you want to allow access to the user's home directory, you should create a separate folder for that and mount it too. However, keep in mind, each container user is best kept separate from the host system's users for security reasons. I tested this on an Ubuntu machine with a Rocky 9 container, and it worked well for me.
I'm just curious, what situation would you need to run SMB inside a container anyway?

Related Questions
How To Get Your Domain Unblocked From Facebook
How To Find A String In a Directory of Files Using Linux