简体   繁体   English

docker 容器内的 C# FTP 服务器 - 连接问题

[英]C# FTP server inside docker container - issue with the connection

I'm not able to connect to docker container ftp server.我无法连接到 docker 容器 ftp 服务器。 Here is my docker file:这是我的 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

To run FTP server I create TcpListener the following way:要运行 FTP 服务器,我按以下方式创建 TcpListener:

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

When I run it locally from the IDE I can connect to it using FileZilla.当我从 IDE 在本地运行它时,我可以使用 FileZilla 连接到它。 When I run it inside a container - no luck.当我在容器内运行它时 - 不走运。 What am I doing wrong?我究竟做错了什么?

FileZilla logs: 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

However I don't see anything in the application logs... Not sure what's happening但是我在应用程序日志中看不到任何内容...不确定发生了什么

how do you run your container?你如何运行你的容器? You should publish your port as well as exposing.您应该发布您的端口以及公开。 Expose is a docker-inside concept and publish is out of docker one. Expose 是一个 docker-inside 概念,而 publish 是 docker 之一。 I mean:我是说:

docker run -d -p 6001:6001 YOUR_IMAGE_NAME

As you may know, FTP protocol has two types, ACTIVE and PASSIVE.您可能知道,FTP 协议有两种类型,ACTIVE 和 PASSIVE。 You should open at least 10 ports from X to Y (range) to let your client access your FTP server using default FTP commands.您应该打开至少 10 个从 X 到 Y(范围)的端口,让您的客户端使用默认的 FTP 命令访问您的 FTP 服务器。 I highly recommend you to see this article https://www.jscape.com/blog/bid/80512/active-vs-passive-ftp-simplified我强烈推荐你看这篇文章https://www.jscape.com/blog/bid/80512/active-vs-passive-ftp-simplified

Hi having the same problem and I figured out why but not how to solve it.嗨有同样的问题,我想出了为什么但不知道如何解决它。

You have two connections, one is the controlconnection and the other is your dataconnection and they need to have the same IP obviously.您有两个连接,一个是控制连接,另一个是您的数据连接,显然它们需要具有相同的 IP。

So now I don't know your code but based on the output your controlconnection is getting the IP 127.0.0.1 but your dataconnection is getting the IP of the container 172.17.0.2.所以现在我不知道你的代码,但基于 output,你的 controlconnection 得到了 IP 127.0.0.1,但你的 dataconnection 得到了 ZA12A3079E14CED47B2069BA52B172 的容器。 and since the two connections aren't the same IP the server shuts down the connection.并且由于两个连接不同 IP 服务器关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM