Set up Samba Server with Docker

Set up Samba Server with Docker

·

2 min read

Recently I need to do some test on Samba server. The most easy way I can think of is to use Docker. Even with Docker, the process is not smooth. In this article, I will walk you through the process and the problems I have been through.

The first step is to find a suitable docker image, I found dperson/samba, which is the mostly downloaded one.

If you follow the documentation to run command below, you can't connect to it.

sudo docker run -it -p 139:139 -p 445:445 -d dperson/samba -p

Then I search the project's repo, and found this issue Can't access shared folders, which is exact the same thing I encountered. The reason is that MacOS does not allow smb connections to "local" shares, e.g. 127.0.0.1. To solve the issue, we need to make an alias to the loopback address, and use the alias for port bindings.

sudo ifconfig lo0 127.0.0.2 alias up
--expose 139 -p 127.0.0.2:139:139  \
--expose 445 -p 127.0.0.2:445:445

So the working command is like below.

docker run -it -d --name samba \
    -p 127.0.0.2:139:139 \
    -p 127.0.0.2:445:445 \
    dperson/samba \
    -s "share;/share;yes;no;no;all;none;${USER};comment" \
    -u "${USER};password" \
    -p

The -u "${USER};password" set up the username and password for Samba server. The -s "share;/share;yes;no;no;all;none;${USER};comment" set up share folder configurations. It follows the format of -s "<name;/path>[;browse;readonly;guest;users;admins;writelist;comment]". And don't forget the last -p paramter, which is used to config permission according to the project's document.

So with this commnad, we can start a Samba server with share fold /share.

And lastly we can access this folder on Mac with the step: Finder > Go > Connect to Server... > smb://127.0.0.2 > Connect.