Create FTP server with Docker Swarm

Waqar Mansoor
3 min readMar 3, 2021

FTP or File Transfer Protocol have been used to transfer files between client and server, to entertain this transfer an FTP server is needed to be setup and the client connects to that server with some popular tools like FileZilla, CyberDuck etc. In this tutorial we will see how to create a local FTP server within the docker container and we will also up this docker container by using the docker swarm engine. The tech stack we will be using is given below :

  1. Python (2.7 or higher)
  2. pyftpdlib (Library for setting up the FTP server)
  3. Docker Engine
  4. Docker Swarm

First you need to install python from the links below :

For MacOS : https://www.python.org/downloads/mac-osx/
For Windows : https://www.python.org/downloads/windows/

I recommend you to use PyCharm community version for the python development, its super easy to use, try this link out

https://www.jetbrains.com/pycharm/download/

Now you need to install docker in your machine you can download it from here

https://www.docker.com/get-started

Create a python file by copying the code below with name main.py

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

def main():
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()

# Define a new user having full r/w permissions and a read-only
# anonymous user having full r/w permissions and a read-only
authorizer.add_user('root', 'root', '/file-server/', perm='elradfmwMT')
authorizer.add_anonymous('/file-server-anon/', perm='elradfmwMT')

# Instantiate FTP handler class
handler = FTPHandler
handler.authorizer = authorizer
# Passive ports
handler.passive_ports = range(5000, 5020)
handler.permit_foreign_addresses = True

handler.banner = "pyftpdlib Based FTP Server"

# connect using an address 0.0.0.0 with port 21
address = ('', 21)
server = FTPServer(address, handler)

# set a limit for connections
server.max_cons = 256
server.max_cons_per_ip = 5

# start ftp server
server.serve_forever()


if __name__ == '__main__':
main()

Create two directories in the same folder named

  • file-server
  • file-server-anon

Create a docker file with name Dockerfile in the same folder

#Docker File for Python FTP Server --Author Waqar Mansoor
FROM python:2.7
RUN pip install pyftpdlib
COPY ./main.py ./
EXPOSE 21
CMD [ "python", "./main.py" ]

Build the docker image with this command

$ docker build -t ftp-server .

you can see the built image in the list by using this command

$ docker images

Create a docker swarm file

version: '3.7'

services:
ftp:
image: ftp-server:latest
volumes:
- ../file-server/:/file-server/
- ../file-server-anon/:/file-server-anon/
ports:
- "21:21"
- "5000-5020:5000-5020"

In the above configuration there are two volumes mounted outside the docker container i.e file-server (for normal users), file-server-anon (for anonymous. users)

Now run the command below to deploy the docker swarm stack

$ docker stack deploy --compose-file ftp-stack.yml ftp-server-stack

After deploying the stack file you should see these two executed lines indicating the successful deployment of stack file.

Try to connect your deployed ftp server with ftp tool, here we will be using cyberduck.

Trying to connect as an anonymous user

After successful connection, transfer files in the folder by using the upload option, the directories you have created i.e file-server, file-server-anon earlier will be updated automatically after transfer.

To download all project files please use the link below

--

--

Waqar Mansoor

I am a tech enthusiast and a visionary person and been in the software industry for the last 3 years, I believe in sharing knowledge with each other.