容器化后,NodeJS应用程序停止工作[容器中的FTP服务器] [英] NodeJS application stops working when containerised [FTP server in a container]

查看:138
本文介绍了容器化后,NodeJS应用程序停止工作[容器中的FTP服务器]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ftp服务器NodeJS应用程序容器化。当我使用npm运行它时,它工作正常,但是当我在容器中运行它时,它没有响应。

I am trying to containerise an ftp server NodeJS application. It works fine when I run it with npm but it does not respond when I run it inside of a container.

这是节点应用程序:
src / index.js文件

This is the node app: The src/index.js file

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文件

my package.json file

{
  "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文件

FROM node:12

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 20-21
EXPOSE 65500-65515

CMD ["npm", "start"]

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

I am testing the FTP server using FileZilla. When I run the server using npm start and connect using FileZilla

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镜像

I am using this command to build the docker image

 docker build -t rrakshak/ftp-demo .

我正在使用它来运行docker

And I am using this to run the docker

 docker run -p 5000:5000 rrakshak/ftp-demo:latest

这是FileZilla上的消息控制台

This is the message on the FileZilla console

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能够连接但未收到其期望的文件列表响应。

It looks like when the server is running inside of a container the FileZilla is able to connect but is not receiving the file listing response it expects.

- ----------更新-----------------------

------------Update-----------------------

将主机设置为0.0 .0。在Filezilla上给我一组新消息

setting the host to 0.0.0. give me a new set of messages on Filezilla

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

为什么我的应用程序在节点中运行时为什么能运行,但在容器化时却不能运行?

Why does my app work when I run it in node but not when I containerise it?

推荐答案

使用被动端口在容器中的 0.0.0.0:5000 上监听已定义

Listen on 0.0.0.0:5000 in the container, with passive ports defined

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连接中使用:

Build the container as is and then run with the following ports mapped, which can all be used in an ftp connection:

docker run -p 5000:5000 -p 65500-65515:65500-65515 --rm rrakshak/ftp-demo

给出响应:

$ 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客户端设置为使用被动模式。

The ftp client must be set to use passive mode.

当FTP客户端位于活动模式,则FTP服务器收到 PORT 从客户端命令并创建一个新的TCP connecti从容器返回到客户端以获取该 PORT 上的数据。

When an FTP client is in active mode, the FTP server receives a PORT command from the client and creates a new TCP connection from the container back out to the client for data on that PORT.

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

Due to the Docker port mapping into the container, the source address of this data connection often won't match what the FTP client is using as the initial destination for the FTP server. Similar issues occur when setting up FTP servers behind NAT on classic servers.

这篇关于容器化后,NodeJS应用程序停止工作[容器中的FTP服务器]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆