Deploy an FTP Server in Docker and Transfer Files with PHP
This guide shows how to run a vsftpd Docker container, configure ports and volumes, install the PHP ftp-client library via Composer, and use PHP code to upload and download files through the container’s internal network.
Requirements
Docker environment
FTP server image: fauria/vsftpd (Docker Hub URL: https://hub.docker.com/r/fauria/vsftpd)
ThinkPHP6 project for upload script
PHP FTP client library: Nicolab/php-ftp-client (GitHub URL: https://github.com/Nicolab/php-ftp-client)
Deploy the FTP service
docker run -d -v e:/ftp:/home/vsftpd -p 20:20 -p 21:21 \
-p 21100-21110:21100-21110 \
-e FTP_USER=tinywan -e FTP_PASS=123456 \
-e PASV_ADDRESS=127.0.0.1 -e PASV_MIN_PORT=21100 \
-e PASV_MAX_PORT=21110 \
--name dnmp-vsftpd --network dnmp_backend --restart=always fauria/vsftpdParameter explanation
--name dnmp-vsftpd: container name --network dnmp_backend: Docker network to join (must be the same network used by other containers) -v e:/ftp:/home/vsftpd: bind‑mount host directory e:/ftp into container path /home/vsftpd Passive mode ports 21100‑21110 are exposed and mapped.
Verify container is running
docker ps
# Example output
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
# 0c8abe7311e3 fauria/vsftpd "/usr/sbin/run-vsftp…" 22 minutes ago Up 22 minutes 0.0.0.0:20-21->20-21/tcp, 0.0.0.0:21100-21110->21100-21110/tcpTest the service
Open a browser or FTP client, connect to 127.0.0.1 on port 21, and log in with the credentials tinywan / 123456. The login screen is shown in the following image.
Install PHP dependency
docker run --rm -i -t -v e:/dnmp/www/tinywan.com:/app composer require nicolab/php-ftp-client --ignore-platform-reqsUpload an entire directory
$ftp = new \FtpClient\FtpClient();
$ftp->connect('dnmp-vsftpd'); // use container network name
$ftp->login('tinywan', '123456');
$source_directory = Env::get('root_path') . 'public/upload/ftp';
$target_directory = '/'; // maps to /home/vsftpd inside the container
$res = $ftp->putAll($source_directory, $target_directory);
var_dump($res); // true on successKey points
Connect to the FTP container by its Docker network alias ( dnmp-vsftpd) instead of 127.0.0.1. $source_directory must be an absolute path on the host, e.g. e:/dnmp/www/tinywan.com/public/upload/ftp. putAll uploads every file and sub‑directory under the source path.
Host‑side directory mapping can be inspected (example image below).
Container file system view:
Upload a single file
$ftp = new \FtpClient\FtpClient();
$ftp->connect('dnmp-vsftpd');
$ftp->login('tinywan', '123456');
$local_file = Env::get('root_path') . 'public/upload/ftp/tinyaiai.js';
$remote_file = 'remote_tinyaiai.js';
$res = $ftp->put($remote_file, $local_file, FTP_BINARY);
var_dump($res); // true on successDownload a file
$local_file = Env::get('root_path') . 'public/upload/ftp/local_text.js';
$remote_file = 'test.txt';
$res = $ftp->get($local_file, $remote_file, FTP_BINARY);
var_dump($res); // true on successFTP directory structure
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
