首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >容器化时NodeJS应用程序停止工作[容器中的FTP服务器]

问容器化时NodeJS应用程序停止工作[容器中的FTP服务器]
EN

Stack Overflow用户
提问于 2020-10-20 13:24:42
回答 2查看 657关注 0票数 3

我正在尝试容器一个ftp服务器NodeJS应用程序。当我使用npm运行它时,它工作得很好,但当我在容器中运行它时,它没有响应。

这是节点app: src/index.js文件

代码语言:javascript
复制
const FtpSvr = require ( 'ftp-srv' );

const hostname = '127.0.0.1';
const port = 21;

const ftpServer = new FtpSvr ({
url:`ftp://${hostname}:${port}`,
anonymous:true
} );

ftpServer.on('login', ({connection, username, password}, resolve, reject) =>
{
    resolve({root : "./"})

    connection.on('STOR', (error, fileName) => {
        console.log("STOR")
    });
});

ftpServer.on ( 'client-error', (connection, context, error) =>
{
    console.log ( `error: ${error}` );
});

ftpServer.listen().then(() => {console.log(`Server running at http://${hostname}:${port}/`);});

我的package.json文件

代码语言:javascript
复制
{
  "name": "ftp-server",
  "version": "1.0.0",
  "description": "FTP server to receive images from cameras and save them on Azure Blob storage",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/index.js"
  },
  "author": "Rakshak",
  "license": "ISC",
  "dependencies": {
    "ftp-srv": "4.3.4"
  }
}

我的docker文件

代码语言:javascript
复制
FROM node:12

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 20-21
EXPOSE 65500-65515

CMD ["npm", "start"]

我正在使用FileZilla测试FTP服务器。当我使用npm运行服务器时,使用FileZilla启动并连接

代码语言:javascript
复制
Connecting to 127.0.0.1:5000...
Status:         Connection established, waiting for welcome message...
Status:         Insecure server, it does not support FTP over TLS.
Status:         Logged in
Status:         Retrieving directory listing...
Status:         Directory listing of "/" successful

我正在使用这个命令来构建docker镜像

代码语言:javascript
复制
 docker build -t rrakshak/ftp-demo .

我要用这个来运行码头

代码语言:javascript
复制
 docker run -p 5000:5000 rrakshak/ftp-demo:latest

这是FileZilla控制台上的消息

代码语言:javascript
复制
Connecting to 127.0.0.1:5000...
Status:         Connection established, waiting for welcome message...
Error:          Connection closed by server
Error:          Could not connect to server
Status:         Waiting to retry...
Status:         Connecting to 127.0.0.1:5000...
Status:         Connection established, waiting for welcome message...
Error:          Connection closed by server
Error:          Could not connect to server

看起来,当服务器在容器内运行时,FileZilla能够连接,但没有收到它期望的文件列表响应。

-更新

将主机设置为0.0.0。在Filezilla上给我一组新的消息

代码语言:javascript
复制
Status:         Connecting to 127.0.0.1:21...
Status:         Connection established, waiting for welcome message...
Status:         Insecure server, it does not support FTP over TLS.
Status:         Logged in
Status:         Retrieving directory listing...
Command:    PWD
Response:   257 "/"
Command:    TYPE I
Response:   200 Switch to "binary" transfer mode.
Command:    PASV
Response:   502 Command not supported
Command:    PORT 127,0,0,1,231,209
Response:   500 The given address is not yours
Error:          Failed to retrieve directory listing

为什么我的应用程序在节点中运行时可以工作,但当我将其容器化时却无法工作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-21 09:27:12

监听容器中的0.0.0.0:5000,并定义被动端口

代码语言:javascript
复制
const FtpSvr = require ( 'ftp-srv' );
  
const hostname = '0.0.0.0';
const port = 5000;

const ftpServer = new FtpSvr ({
  url: `ftp://${hostname}:${port}`,
  anonymous: true,
  pasv_url: `ftp://${hostname}:${port}`,
  pasv_min: 65500,
  pasv_max: 65515,
});

按原样构建容器,然后使用映射的以下端口运行,这些端口都可以在ftp连接中使用:

代码语言:javascript
复制
docker run -p 5000:5000 -p 65500-65515:65500-65515 --rm rrakshak/ftp-demo

给出了响应:

代码语言:javascript
复制
$ curl ftp://localhost:5000
-rw-r--r-- 1 1 1          141 Oct 21 01:22 Dockerfile
drwxr-xr-x 1 1 1         4096 Oct 21 01:21 node_modules
-rw-r--r-- 1 1 1        21137 Oct 21 01:21 package-lock.json
-rw-r--r-- 1 1 1           52 Oct 21 01:21 package.json
-rw-r--r-- 1 1 1          660 Oct 21 01:23 server.js
-rw-r--r-- 1 1 1        20287 Oct 21 01:21 yarn.lock

ftp客户端必须设置为使用被动模式。

当 FTP 客户端处于active mode,时,FTP 服务器接收来自客户端的PORT 命令并创建一个从容器返回到客户端的新 TCP 连接,以获取该 PORT 上的数据。

由于Docker端口映射到容器,此数据连接的源地址通常与FTP客户端用作FTP服务器的初始目的地的地址不匹配。在传统服务器上设置NAT之后的FTP服务器时也会出现类似的问题。

票数 2
EN

Stack Overflow用户

发布于 2020-10-21 08:30:08

尝试绑定到0.0.0.0。当你在Docker内部运行时,绑定127.0.0.1将不起作用,因为请求将来自外部(至少从Docker容器的角度来看)。

有关排除此类网络问题的提示,您可以在answer to this related question中找到一些想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64438942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档