dockerftpapache-commons-net

FTPConnectionClosedException: Connection closed without indication


I'm trying to connect with FTP server, deployed from docker image, using Apache client:

import org.apache.commons.net.ftp.FTPClient

import java.io.IOException

object FtpClient {

  def main(args: Array[String]): Unit = {

    val ftpClient = new FTPClient()

    try {
      ftpClient.connect("localhost", 21)
      val success = ftpClient.login("one", "1234")

      if (success) {
        println("Success connect")
      }

    } catch {
      case e: IOException => throw new RuntimeException(e)
    } finally {
      // 
    }

  }
}

FTP server run by:

docker run -d     -p 21:21     -p 21000-21010:21000-21010     -e USERS="one|1234"     -e ADDRESS=ftp.site.domain     delfer/alpine-ftp-server

And I receive the following error:

      Exception in thread "main" java.lang.RuntimeException: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at ru.spb.client.FtpClient$.main(FtpClient.scala:23)
at ru.spb.client.FtpClient.main(FtpClient.scala)
     Caused by: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.getReply(FTP.java:568)
at org.apache.commons.net.ftp.FTP.getReply(FTP.java:556)

EDIT:

Docker logs returns:

Changing password for one
New password: 
Bad password: too short
Retype password: 
adduser: /ftp/one: No such file or directory
passwd: password for one changed by root
seems like pidfd_open syscall does not work, falling back to 
polling
failed to watch for direct child exit (pidfd_open error): Operation not permitted

Maybe I miss some significant connection settings?


Solution

  • FTP Container - Docker run command:

    docker run -d \
      -p 21:21 \
      -p 21000-21010:21000-21010 \
      -e USERS="one|Passw0rd!" \
      -e ADDRESS=ftp.site.domain \
      delfer/alpine-ftp-server
    

    edit your /etc/hosts

    add ftp.site.domain to 127.0.0.1

    127.0.0.1   localhost ftp.site.domain
    

    A quick alternative

    find ftp container id

    docker ps
    

    if container id is 05da9c08858f

    open into ftp container

    docker exec -it 05da9c08858f sh
    

    Check if the ftp service is running?

    in container , run command:

    netstat -tulnp | grep :21
    

    if return nothing, ftp service is not running.

    start ftp service

    vsftpd /etc/vsftpd/vsftpd.conf
    

    run command again:

    netstat -tulnp | grep :21
    

    return: ( ftp service is running. )

    netstat -tulnp | grep :21
    tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      45/vsftpd
    

    exit container:

    exit
    

    Note:

    I didn’t spend more time to check why the ftp service was not started by default in the docker run command, so I just provide a quick solution: enter the Container and start the ftp service manually.

    Your Test App

    FtpClient.scala

    import org.apache.commons.net.ftp.FTPClient
    import java.io.IOException
    
    object FtpClient {
    
    def main(args: Array[String]): Unit = {
    
    val ftpClient = new FTPClient()
    
      try {
        ftpClient.connect("ftp.site.domain", 21)
        val success = ftpClient.login("one", "Passw0rd!")
        if (success) {
          println("Success connect")
        }
      } catch {
        case e: IOException => throw new RuntimeException(e)
      } finally {
    // 
    }
    
    }
    }
    

    Download jar file:

    Download commons-net-3.9.0.jar(https://repo1.maven.org/maven2/commons-net/commons-net/3.9.0/commons-net-3.9.0.jar)

    Check version

    $ scala -version
    Scala code runner version: 1.5.4
    Scala version (default): 3.6.4
    

    Run App

    $ scala -classpath "commons-net-3.9.0.jar" FtpClient.scala
    Compiling project (Scala 3.6.4, JVM (21))
    Compiled project (Scala 3.6.4, JVM (21))
    Success connect