Docker编写ftpd-从主机访问服务器

jmp7cifd  于 2023-01-30  发布在  Docker
关注(0)|答案(2)|浏览(119)

我正在尝试从主机访问ftpd服务器
使用ftp localhostftp <my_ip>
但我得到的是ftp: connect: Connection refused

version: '3'

services:
  ftpd-server:
    container_name: ftpd-server
    image: stilliard/pure-ftpd:hardened
    ports:
      - 21:21
      - 20:20
      - 30000-30009:30000-30009
    volumes:
      - './ftp/data:/home/username/'
      - './ftp/pass:/etc/pure-ftpd/passwd'
    environment:
      PUBLICHOST: "0.0.0.0"
      FTP_USER_NAME: "user"
      FTP_USER_PASS: "pass"
      FTP_USER_HOME: "/home/username"
    restart: always

由于我使用PUBLICHOST: "0.0.0.0"和端口转发21:21,我希望能够连接。
Docker日志

Removing ftpd-server ... done
Removing network mytest_default
No stopped containers
Creating network "mytest_default" with the default driver
Creating ftpd-server ... 
Creating ftpd-server ... done
Attaching to ftpd-server
ftpd-server    | Creating user...
ftpd-server    | Password: 
ftpd-server    | Enter it again: 
ftpd-server    | Setting default port range to: 30000:30009
ftpd-server    | Setting default max clients to: 5
ftpd-server    | Setting default max connections per ip to: 5
ftpd-server    | Starting Pure-FTPd:
ftpd-server    |   pure-ftpd  -l puredb:/etc/pure-ftpd/pureftpd.pdb -E -j -R -P 0.0.0.0 -s -A -j -Z -H -4 -E -R -G -X -x   -p 30000:30009 -c 5 -C 5

我怎样才能实现从主机连接到我的ftp服务器上的容器?

3pmvbmvn

3pmvbmvn1#

您可以将network_mode: host添加到服务定义中以使其工作。

services:
  ftpd-server:
    # ...
    network_mode: host
    # ...

然后测试:

$ ftp -p localhost 21
Connected to localhost.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 5 allowed.
220-Local time is now 16:04. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 15 minutes of inactivity.
kkih6yb8

kkih6yb82#

我这边的一个工作示例如下:

version: "1.0"
services:
  ftpd-server:
    image: stilliard/pure-ftpd:hardened
    ports:
      - "21:21"
      - "30000-30009:30000-30009"
    volumes:
      - './ftp/data:/home/username/'
      - './ftp/pass:/etc/pure-ftpd/passwd'
    environment:
      PUBLICHOST: "0.0.0.0"
      FTP_USER_NAME: "username"
      FTP_USER_PASS: "changeme!"
      FTP_USER_HOME: "/home/username"
    restart: always

添加行“网络模式:主机”将导致最新Docker安装出现以下错误(对于我的情况,它是Docker版本20.10.13,在Windows 10操作系统上构建a224086,并支持WSL2):

"host" network_mode is incompatible with port_bindings

这是新版本Docker中涉及的防护设施,以避免端口配置错误,请查看以下链接了解详细信息:https://forums.docker.com/t/docker-errors-invalidargument-host-network-mode-is-incompatible-with-port-bindings/103492 .

相关问题