使用docker搭建ftp服务器

2022-04-13 18:02:02

最近在做的一个调度系统的需求里,有一个需求,需要用到ftp服务器进行文件的下载和上传。刚好新分配的两个linux服务器上没有搭建ftp,想着就用docker安装一下。

 安装的方式,参照:Linux下使用docker搭建ftp服务器 - william_zhao - 博客园

顺序无非,拉取镜像,启动镜像,访问服务器。

1)docker拉取ftp镜像

docker pull fauria/vsftpd

2)启动ftp镜像

docker run -d -p 2121:21 -p 2020:20 -p 21100-21110:21100-21110 \
-v /home/dispatch/ftp/root:/home/vsftpd/ftp \
-e FTP_USER=ftp \
-e FTP_PASS=123456 \
 -e PASV_ADDRESS=***.**.**.** \
-e PASV_MIN_PORT=21100 \
-e PASV_MAX_PORT=21110 \
--name vsftpd \
--restart=always fauria/vsftpd

参数解释:

-p 2121:21 -p 2020:20  映射daocker和宿主机的端口号,通过ftp客户端,连接宿主机的2121端口,可以连接ftp服务器。2020端口,为docker映射到宿主机的数据传输端口。

-v /home/dispatch/ftp/root:/home/vsftpd/ftp 挂载的本机文件路径。注意:这个地方是有一个坑。/home/vsftpd/ftp 为docker-ftp的文件存放路径。这个不可以随便写,并且,每个人的也都不一样。路径格式为/home/vsftpd/${user}    ${user} 为我后面设置的用户名。也就是FTP_USER=ftp。所以上面那个作者,设置了自己账户名为root之后,他的挂载源路径就为什么必须是root原因了。关于这个配置,等下看下配置文件就知道。

/home/dispatch/ftp/root 这个路径是应道宿主机的文件路径。这个可以随便写。

-e FTP_USER=ftp -e FTP_PASS=123456  分别为账号密码。

PASV_ADDRESS 为宿主机的IP

3)启动成功之后,使用ftp客户端连接上我们的ftp服务器。 我用的是CuteFtp 9.0

出现下面提示信息,即显示连接成功

 我们在此新建一个文件夹

  找到上面我们挂载的文件路径/home/dispatch/ftp/root ,打开就可以看到刚才新建的文件。同样在ftp服务器上文件的新增、删除、变更。使用ftp客户端都可以看到。至此docker版的ftp服务器就搭建完成。可以再去测试一下文件的上传下载。

4)关于上面作者所提到的坑

关于挂载路径设置的问题,其实在配置文件中的都有配置。

1、进入ftp的docker容器

docker exec -it vsftpd /bin/bash

2、打开/etc/vsftpd/vsftpd.conf

vi /etc/vsftpd/vsftpd.conf

这个也就是挂载到宿主机的ftp文件路径。也就是跟每个用户名是相关的。不同的用户名,对应不同的文件路径。当然也可以自行修改配置。建议把配置文件和log日志也挂在本地

贴出整个配置文件。如下:

# Run in the foreground to keep the container running:
background=NO

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO

# Uncomment this to allow local users to log in.
local_enable=YES

## Enable virtual users
guest_enable=YES

## Virtual users will use the same permissions as anonymous
virtual_use_local_privs=YES

# Uncomment this to enable any form of FTP write command.
# Run in the foreground to keep the container running:
background=NO

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO

# Uncomment this to allow local users to log in.
local_enable=YES

## Enable virtual users
guest_enable=YES

## Virtual users will use the same permissions as anonymous
virtual_use_local_privs=YES

# Uncomment this to enable any form of FTP write command.
write_enable=YES

## PAM file name
pam_service_name=vsftpd_virtual

## Home Directory for virtual users
user_sub_token=$USER
local_root=/home/vsftpd/$USER

# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
chroot_local_user=YES

# Workaround chroot check.
# See https://www.benscobie.com/fixing-500-oops-vsftpd-refusing-to-run-with-writable-root-inside-chroot/
# and http://serverfault.com/questions/362619/why-is-the-chroot-local-user-of-vsftpd-insecure
allow_writeable_chroot=YES
# Run in the foreground to keep the container running:
background=NO

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO

# Uncomment this to allow local users to log in.
local_enable=YES

## Enable virtual users
guest_enable=YES

## Virtual users will use the same permissions as anonymous
virtual_use_local_privs=YES

# Uncomment this to enable any form of FTP write command.
write_enable=YES

## PAM file name
pam_service_name=vsftpd_virtual

## Home Directory for virtual users
user_sub_token=$USER
local_root=/home/vsftpd/$USER

# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
chroot_local_user=YES

# Workaround chroot check.
# See https://www.benscobie.com/fixing-500-oops-vsftpd-refusing-to-run-with-writable-root-inside-chroot/
# and http://serverfault.com/questions/362619/why-is-the-chroot-local-user-of-vsftpd-insecure
allow_writeable_chroot=YES

## Hide ids from user
hide_ids=YES

## Enable logging
xferlog_enable=YES
xferlog_file=/var/log/vsftpd/vsftpd.log

## Enable active mode
port_enable=YES
connect_from_port_20=YES
ftp_data_port=20

##| Disable seccomp filter sanboxing
seccomp_sandbox=NO

### Variables set at container runtime
pasv_address=192.168.0.16
pasv_max_port=21110
pasv_min_port=21100
pasv_addr_resolve=NO
pasv_enable=YES
file_open_mode=0666
local_umask=077
xferlog_std_format=NO
reverse_lookup_enable=YES
pasv_promiscuous=NO
port_promiscuous=NO

参数的解释可以另行搜索

  • 作者:JYWWABF
  • 原文链接:https://blog.csdn.net/weixin_41753664/article/details/123374929
    更新时间:2022-04-13 18:02:02