【问题标题】:C# FTP server inside docker container - issue with the connectiondocker容器内的C# FTP服务器 - 连接问题
【发布时间】:2021-04-12 15:11:16
【问题描述】:

我无法连接到 docker 容器 ftp 服务器。这是我的 docker 文件:

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
WORKDIR /app
COPY . ./
RUN dotnet publish MD.Ftp.Server -c Release -o out -r linux-x64 
FROM mcr.microsoft.com/dotnet/runtime:5.0
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "MD.Ftp.Server.dll"]
EXPOSE 6001/tcp

为了运行 FTP 服务器,我通过以下方式创建 TcpListener:

tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, settings.Port));

当我从 IDE 本地运行它时,我可以使用 FileZilla 连接到它。当我在容器内运行它时 - 运气不好。我做错了什么?

FileZilla 日志:

Status:         Disconnected from server
Status:         Connecting to 127.0.0.1:6001...
Status:         Connection established, waiting for welcome message...
Status:         Plain FTP is insecure. Please switch to FTP over TLS.
Status:         Logged in
Status:         Retrieving directory listing...
Command:    PWD
Response:   257 "/"
Command:    TYPE I
Response:   200 In IMAGE type
Command:    PASV
Response:   227 Enter Passive Mode (172,17,0,2,234,142)
Command:    LIST
Response:   150 File is Ok, about to open connection.
Error:          Connection timed out after 20 seconds of inactivity
Error:          Failed to retrieve directory listing
Status:         Disconnected from server
Status:         Connecting to 127.0.0.1:6001...
Status:         Connection established, waiting for welcome message...
Status:         Plain FTP is insecure. Please switch to FTP over TLS.
Status:         Logged in
Status:         Retrieving directory listing...
Command:    PWD
Response:   257 "/"
Command:    TYPE I
Response:   200 In IMAGE type
Command:    PASV
Response:   227 Enter Passive Mode (172,17,0,2,234,143)
Command:    LIST
Response:   150 File is Ok, about to open connection.
Error:          Connection timed out after 20 seconds of inactivity
Error:          Failed to retrieve directory listing

但是我在应用程序日志中看不到任何内容...不确定发生了什么

【问题讨论】:

    标签: c# docker .net-core tcp tcplistener


    【解决方案1】:

    你如何运行你的容器?您应该发布您的端口以及公开。 Expose 是一个 docker-inside 的概念,而 publish 是一个 docker 之外的概念。 我的意思是:

    docker run -d -p 6001:6001 YOUR_IMAGE_NAME
    

    您可能知道,FTP 协议有两种类型,主动和被动。您应该至少打开 10 个从 X 到 Y(范围)的端口,让您的客户端使用默认的 FTP 命令访问您的 FTP 服务器。 我强烈推荐你看这篇文章https://www.jscape.com/blog/bid/80512/active-v-s-passive-ftp-simplified

    【讨论】:

    • 是的,我试过了,还是不行:(
    • 更新了问题。它说“无法检索目录列表”。这是我能做到的。
    【解决方案2】:

    您好,遇到同样的问题,我知道原因,但不知道如何解决。

    你有两个连接,一个是控制连接,另一个是你的数据连接,它们显然需要具有相同的 IP。

    所以现在我不知道您的代码,但根据输出,您的 controlconnection 正在获取 IP 127.0.0.1,但您的 dataconnection 正在获取容器 172.17.0.2 的 IP。由于这两个连接不是同一个 IP,服务器会关闭连接。

    【讨论】:

    • 思考过程很好。不过,其他答案更好。继续!
    猜你喜欢
    • 1970-01-01
    • 2022-08-20
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2021-09-15
    相关资源
    最近更新 更多