返回列表 发帖

CentOS8_在Docker中安装PureFTPd

查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#

已经设置开机自动启动Docker:
[root@centos8 ~]# systemctl is-enabled docker
enabled
[root@centos8 ~]#


从公网下载镜像:( 以下是下载Redhat6.9的镜像 )
[root@centos8 ~]# docker pull registry.access.redhat.com/rhel6:6.9
6.9: Pulling from rhel6
13cd330500ad: Pull complete
55e5ecc272dd: Pull complete
Digest: sha256:b831589d62fd5cfe62f08f2ffe13c32d7396e2577e960fd148402f588a92e6ba
Status: Downloaded newer image for registry.access.redhat.com/rhel6:6.9
registry.access.redhat.com/rhel6:6.9
[root@centos8 ~]#


查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY                         TAG       IMAGE ID       CREATED       SIZE
registry.access.redhat.com/rhel6   6.9       04b4d8781fb6   4 years ago   199MB
[root@centos8 ~]#


使用镜像“registry.access.redhat.com/rhel6:6.9”创建并启动容器“rhel6_1”:
[root@centos8 ~]# docker run -itd --name rhel6_1 -p 21:21 -v /opt/share:/home/dir_1 registry.access.redhat.com/rhel6:6.9
e9eaa92621c5b3d382787aa6b65b36d64ccfcd3716a450bd734b32541fee6997
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个21是宿主机的端口,暴露给外部直接访问;第二个21是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/home/dir_1;假如目录不存在,就会自动创建。
registry.access.redhat.com/rhel6:6.9 : 镜像名称:版本号


在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID   IMAGE                                  COMMAND       CREATED              STATUS              PORTS                               NAMES
e9eaa92621c5   registry.access.redhat.com/rhel6:6.9   "/bin/bash"   About a minute ago   Up About a minute   0.0.0.0:21->21/tcp, :::21->21/tcp   rhel6_1
[root@centos8 ~]#

注释:宿主机的TCP 21端口,映射到容器“rhel6_1”的TCP 21端口。



###

进入容器“rhel6_1”:
[root@centos8 ~]# docker exec -it rhel6_1 /bin/bash
[root@e9eaa92621c5 /]#
[root@e9eaa92621c5 /]# pwd
/
[root@e9eaa92621c5 /]# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var
[root@e9eaa92621c5 /]#


查看容器“rhel6_1”的系统版本信息:
[root@e9eaa92621c5 /]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.9 (Santiago)
[root@e9eaa92621c5 /]# uname -r
4.18.0-193.el8.x86_64
[root@e9eaa92621c5 /]#



[root@e9eaa92621c5 /]# vim 1.txt
bash: vim: command not found
[root@e9eaa92621c5 /]#

不可以直接使用公网Yum源:
[root@e9eaa92621c5 /]# yum -y install vim
Loaded plugins: ovl, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Setting up Install Process
No package vim available.
Error: Nothing to do

[root@e9eaa92621c5 /]#

[root@e9eaa92621c5 /]# cd /etc/yum.repos.d
[root@e9eaa92621c5 yum.repos.d]# ls
redhat.repo
[root@e9eaa92621c5 yum.repos.d]#
[root@e9eaa92621c5 yum.repos.d]# cat redhat.repo
#
# Certificate-Based Repositories
# Managed by (rhsm) subscription-manager
#
# *** This file is auto-generated.  Changes made here will be over-written. ***
# *** Use "subscription-manager repo-override --help" if you wish to make changes. ***
#
# If this file is empty and this system is subscribed consider
# a "yum repolist" to refresh available repos
#[root@e9eaa92621c5 yum.repos.d]#


Redhat6使用CentOS6的公网Yum源,可参考:CentOS6使用新Yum源+EPEL
[root@e9eaa92621c5 yum.repos.d]# ls
CentOS-Base.repo  epel.repo  redhat.repo
[root@e9eaa92621c5 yum.repos.d]#


现在可以使用公网Yum源安装rpm软件包:
[root@e9eaa92621c5 ~]# yum -y install vim

[root@e9eaa92621c5 ~]# yum -y install wget



###

在容器“rhel6_1”中安装PureFTPd:

先安装依赖软件包:
yum -y install gcc gcc-c++ autoconf automake tar
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

编译安装:
tar -zxvf pure-ftpd-1.0.47.tar.gz && cd pure-ftpd-1.0.47
./configure --prefix=/usr/local/pureftpd --without-inetd --with-altlog --with-puredb --with-throttling --with-peruserlimits --with-tls
make && make install


修改PureFTPd的配置文件:(可拿以下代码直接覆盖原文件)
[root@e9eaa92621c5 ~]# cat /usr/local/pureftpd/etc/pure-ftpd.conf |grep -v "^#" |grep -v "^$"
ChrootEveryone               yes
BrokenClientsCompatibility   no
MaxClientsNumber             50
Daemonize                    yes
MaxClientsPerIP              8
VerboseLog                   no
DisplayDotFiles              yes
AnonymousOnly                no
NoAnonymous                  yes
SyslogFacility               ftp
DontResolve                  yes
MaxIdleTime                  15
PureDB /usr/local/pureftpd/etc/pureftpd.pdb
LimitRecursion               10000 8
AnonymousCanCreateDirs       no
MaxLoad                      4
PassivePortRange             9000 9045
AntiWarez                    yes
Umask                        133:022
MinUID                       100
AllowUserFXP                 no
AllowAnonymousFXP            no
ProhibitDotFilesWrite        no
ProhibitDotFilesRead         no
AutoRename                   no
AnonymousCantUpload          yes
CreateHomeDir                yes
MaxDiskUsage                   99
CustomerProof                yes
IPV4Only                     yes



启动PureFTPd服务:
[root@e9eaa92621c5 ~]# /usr/local/pureftpd/sbin/pure-ftpd /usr/local/pureftpd/etc/pure-ftpd.conf
[root@e9eaa92621c5 ~]#

确认PureFTPd服务正在运行中:
[root@e9eaa92621c5 ~]# ps -ef |grep pure-ftpd | grep -v "grep"
root        9990       1  0 21:13 ?        00:00:00 /usr/local/pureftpd/sbin/pure-ftpd /usr/local/pureftpd/etc/pure-ftpd.conf
[root@e9eaa92621c5 ~]#

检测PureFTPd的监听端口:(PureFTPd服务运行时)
[root@e9eaa92621c5 ~]# netstat -anp |grep pure-ftpd
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      9990/pure-ftpd      
[root@e9eaa92621c5 ~]#



创建一个真实的系统用户:
[root@e9eaa92621c5 ~]# useradd -d /home/pure -s /sbin/nologin pure
[root@e9eaa92621c5 ~]#

设置FTP目录的权限:
[root@e9eaa92621c5 ~]# chmod -R 777 /home/dir_1
[root@e9eaa92621c5 ~]#


添加FTP虚拟用户(zhuohua),并指定其FTP目录:
[root@e9eaa92621c5 ~]# /usr/local/pureftpd/bin/pure-pw useradd zhuohua -u pure -d /home/dir_1
Password:
Enter it again:
[root@e9eaa92621c5 ~]#

每添加一个新的FTP虚拟用户后,都要重新生成数据库文件:
[root@e9eaa92621c5 ~]# /usr/local/pureftpd/bin/pure-pw mkdb
[root@e9eaa92621c5 ~]#



###

Windows客户端使用FlashFXP连接容器“rhel6_1”的FTP:(使用PureFTPd用户验证)
图片1.png

可能会出现以下错误:(FlashFXP其实可以自动更改模式)
图片2.png

解决方法:(指定站点不使用被动模式)
图片3.png

图片4.png

实验至此,对于容器“rhel6_1”的FTP目录里面的所有内容,zhuohua可以下载、上传、修改、删除。(支持中文)



###

容器“rhel6_1”默认是没有安装crontab周期性计划任务:
[root@e9eaa92621c5 ~]# service crond status
crond: unrecognized service
[root@e9eaa92621c5 ~]#

安装crond服务:
[root@e9eaa92621c5 ~]# yum -y install vixie-cron

安装crond服务成功后:(其实crond服务是不会随容器的启动而启动)
[root@e9eaa92621c5 ~]# service crond status   
crond is stopped
[root@e9eaa92621c5 ~]# chkconfig --list crond
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@e9eaa92621c5 ~]#



###

让PureFTPd服务、crond服务随容器的启动而启动:

在容器“rhel6_1”里创建一个脚本:
[root@e9eaa92621c5 ~]# cat /etc/auto_service.sh
#!/bin/bash
/usr/local/pureftpd/sbin/pure-ftpd /usr/local/pureftpd/etc/pure-ftpd.conf
service crond start
/bin/bash


[root@e9eaa92621c5 ~]# chmod 777 /etc/auto_service.sh
[root@e9eaa92621c5 ~]# ll /etc/auto_service.sh
-rwxrwxrwx. 1 root root 96 Apr  3 21:57 /etc/auto_service.sh
[root@e9eaa92621c5 ~]#



在宿主机把容器“rhel6_1”创建成一个新镜像:
[root@centos8 ~]# docker commit rhel6_1 rhel6_1:3.8.1
sha256:1498f4c0edaea118e3f4c1c4c8425a8c709a30688d1d0bbaccac3495fe05804b
[root@centos8 ~]#

笺注:
rhel6_1:3.8.1 : 新镜像名称:版本号


再次,查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY                         TAG       IMAGE ID       CREATED          SIZE
rhel6_1                            3.8.1     1498f4c0edae   23 seconds ago   834MB
registry.access.redhat.com/rhel6   6.9       04b4d8781fb6   4 years ago      199MB
[root@centos8 ~]#



在宿主机创建Dockerfile文件:
[root@centos8 ~]# vi Dockerfile
写入:
FROM rhel6_1:3.8.1
CMD ["/bin/bash","/etc/auto_service.sh"]

笺注:
rhel6_1:3.8.1 : 源镜像名称:版本号


根据Dockerfile文件创建成一个新镜像:
[root@centos8 ~]# docker build -f ./Dockerfile -t rhel6_1:3.8.2 .
Sending build context to Docker daemon  629.8kB
Step 1/2 : FROM rhel6_1:3.8.1
---> 1498f4c0edae
Step 2/2 : CMD ["/bin/bash","/etc/auto_service.sh"]
---> Running in ba1102b5a0f4
Removing intermediate container ba1102b5a0f4
---> 61a0b857815c
Successfully built 61a0b857815c
Successfully tagged rhel6_1:3.8.2

[root@centos8 ~]#

笺注:
rhel6_1:3.8.2 : 新镜像名称:版本号


再次,查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY                         TAG       IMAGE ID       CREATED              SIZE
rhel6_1                            3.8.2     61a0b857815c   About a minute ago   834MB
rhel6_1                            3.8.1     1498f4c0edae   2 minutes ago        834MB
registry.access.redhat.com/rhel6   6.9       04b4d8781fb6   4 years ago          199MB
[root@centos8 ~]#



使用镜像“rhel6_1:3.8.2”创建并启动容器“rhel6_2”:
[root@centos8 ~]# docker run -itd --name rhel6_2 -p 2121:21 -p 9000-9045:9000-9045 -v /opt/share:/home/dir_1 rhel6_1:3.8.2
746f7edc2dd213e126509c5e2801d0ea1691d4e7aa2e6e462b151313dec2a3fb
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 2121、9000-9045是宿主机的端口,暴露给外部直接访问;21、9000-9045是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/home/dir_1;假如目录不存在,就会自动创建。
rhel6_1:3.8.2 : 镜像名称:版本号


在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID   IMAGE                                  COMMAND                  CREATED          STATUS          PORTS                                                                                                  NAMES
746f7edc2dd2   rhel6_1:3.8.2                          "/bin/bash /etc/auto…"   34 seconds ago   Up 30 seconds   0.0.0.0:9000-9045->9000-9045/tcp, :::9000-9045->9000-9045/tcp, 0.0.0.0:2121->21/tcp, :::2121->21/tcp   rhel6_2
e9eaa92621c5   registry.access.redhat.com/rhel6:6.9   "/bin/bash"              2 hours ago      Up 15 minutes   0.0.0.0:21->21/tcp, :::21->21/tcp                                                                      rhel6_1
[root@centos8 ~]#

注释:
宿主机的TCP 2121端口,映射到容器“rhel6_2”的TCP 21端口。
宿主机的TCP 9000-9045端口,映射到容器“rhel6_2”的TCP 9000-9045端口。
宿主机的TCP 9000-9045端口,其实可以不映射到容器“rhel6_2”的TCP 9000-9045端口。



###

进入容器“rhel6_2”:
[root@centos8 ~]# docker exec -it rhel6_2 /bin/bash
[root@746f7edc2dd2 /]#


查看PureFTPd服务的状态:
[root@746f7edc2dd2 /]# ps -ef |grep pure-ftpd | grep -v "grep"
root           8       1  0 22:42 ?        00:00:00 /usr/local/pureftpd/sbin/pure-ftpd /usr/local/pureftpd/etc/pure-ftpd.conf
[root@746f7edc2dd2 /]#

检测PureFTPd的监听端口:(PureFTPd服务运行时)
[root@746f7edc2dd2 /]# netstat -anp |grep pure-ftpd
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      8/pure-ftpd         
[root@746f7edc2dd2 /]#

查看crond服务的状态:
[root@746f7edc2dd2 /]# service crond status
crond (pid  25) is running...
[root@746f7edc2dd2 /]#

笺注:实验至此,容器“rhel6_2”的PureFTPd服务、crond服务会随宿容器的启动而启动。



在宿主机让容器“rhel6_2”随宿主机的启动而启动:
[root@centos8 ~]# docker update --restart=always rhel6_2
rhel6_2
[root@centos8 ~]#

笺注:实验至此,容器“rhel6_2”的PureFTPd服务、crond服务会随宿主机的启动而启动。



###

Windows客户端使用FileZilla连接容器“rhel6_2”的FTP:(使用PureFTPd用户验证)
图片5.png
注释:指定站点要使用端口TCP 2121

图片6.png
注释:指定站点要使用主动模式。

图片7.png

实验至此,对于容器“rhel6_2”的FTP目录里面的所有内容,zhuohua可以下载、上传、修改、删除。(支持中文)


笺注:
Docker宿主机的防火墙其实无需特意打开任何TCP端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports

[root@centos8 ~]#





相关文章:
CentOS8_在Docker中安装vsftpd
CentOS8_在Docker的容器内自动启动服务

PureFTPd虚拟用户验证
本地备份站点目录和数据库+crontab计划任务

返回列表