'Connecting to a remote FTP server from Docker container running within remote Linux server

The goal of my project is to be able to schedule run a program from a linux server which connects to a remote (existing) ftp server and transfers from files from there to a remote database. This should all be accomplished inside a docker container pulled from Gitlab.

Please note that the program has been able to connect to the ftp server inside a docker build when running it on a Windows machine. I have also been able to connect to the ftp server on the linux server (outside of a container) using the ftp [hostname] command in bash.

When I try and use the ftp.connect(hostname, portname) command from the ftplib library in my containerized script, the following error is shown:

ftp.connect(hostname, portname)
  File "/usr/local/lib/python3.7/ftplib.py", line 152, in connect
    source_address=self.source_address)
  File "/usr/local/lib/python3.7/socket.py", line 728, in create_connection
    raise err
  File "/usr/local/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address

In my Dockerfile I have exposed ports 21 and 8080 like this:

EXPOSE 21/tcp
EXPOSE 8080/tcp

Likewise, I have the following as my docker run command within Linux bash:

docker run -p 21:21 -p 8080:8080 registry.gitlab.com/link/to/project

Does anyone know where I might be going wrong here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source