无图无真相,先放个效果图:node
使用 docker 来跑一些服务很方便,可是有的时候想管理容器里面的文件却很麻烦 -- 通常常规作法有3种:linux
经过数据卷或数据卷容器的方式git
启动容器的时候时候启动 vsftpd 或者 sshd 等服务,并开启端口映射,而后经过 ftp/sftp 连上去管理github
进入容器的终端,经过命令行管理docker
可是这些作法都有必定的缺陷和不便:typescript
1和2都是须要在启动容器的时候作一些配置,若是容器已经启动了就歇菜了。并且2须要额外的端口映射,占用主机的端口。3的作法比较 geek ,而容器中一般只有基础的 shell 工具,没有一个熟悉的工具套件环境,操做比较费时费力,不够直观。shell
我所能想到比较好的解决方案是综合2和3的方案,构建一个不须要额外的启动配置的 FTP
服务器:ftp-server-for-dockernpm
这是一个 docker 专用的 FTP 服务器 - 容许经过 FTP 协议来访问 docker 容器中的文件(即便 docker 容器已经启动了也能够的哟)。基于 linux shell
sh
和 docker 的docker cp
.服务器
推荐使用 npm 安装:网络
npm install -g ftp-server-for-docker
好比说在 localhost:21
上启动 FTP 服务器,并运行 test
用户经过密码 123456
来访问:
$ ftp-server-for-docker -H localhost -p 21 --users test:123456 FtpServer start listening at localhost:21
使用 ftp-server-for-docker
来浏览容器 wordpress
中的文件:
想要知道更多使用方法,能够查看命令行帮助:
$ ftp-server-for-docker --help Usage: ftp-server-for-docker [options] Options: --port, -p Specify which port to listen on(default is 21) [number] --host, -H Specify which host to bind(default is 0.0.0.0) [string] --annoymous, -a Whether enable annoymous user [boolean] --users, -u Specify users (in form like username:password, can be multiple) [array] --config, -c Specify which configuration file to use --debug Whether enable debug mode [boolean] -h, --help Show help [boolean] -?, --help Show help [boolean] Examples: ftp-server-for-docker -h localhost -p 21 Start the FTP Server on -u test:123 localhost:21, allow user `test` to access.
这个 FTP 服务器网络方面采用了比较成熟的 ftpd ,而文件系统模块替换成了新写的 Docker FS.
文件系统的基本操做(stat
/readdir
/open
/unlink
...)都是经过 docker exec
、docker cp
和临时文件来完成,好比:
stat
是经过 docker exec <container> stat xxx
来查看文件状态
readdir
是经过 docker exec <container> ls -1 --color=none
来列出目录下的文件
open
是经过 docker cp <container>:<path> <temp-path>
把容器中的文件拷贝到临时文件中,而后再读取的
具体实现流程参见源代码
这个项目中的主要源代码都使用 TypeScript -- 都是在 src/*.ts
中,经过 tsc
编译后生成成 lib/*.js
,最终npm打包的只有 lib/*.js
没有 src/*.ts
.
这是我第一次使用 TypeScript. TypeScript 的强类型带来不少方便,也带来不少不便。综合而言,TypeScript 让我想起了曾经使用过的 C#,结合 Visual Studio Code 来用仍是利大于弊。TypeScript 能在编辑和编译的时候就检查出不少类型问题,并且智能提示确实很棒。
FileZilla 没法列出目录结构 -- 能够尝试配置传输模式为 主动模式
,被动模式还有问题,正在解决ing...
中文目录显示乱码 -- 已知问题,正在解决ing...
若有其余问题,欢迎来提 issue , 固然更欢迎 fork 并解决后来提 PR.
本文地址:http://www.clarencep.com/2017... 转载请注明出处