Deploy an FTP Server in Docker and Transfer Files with PHP FTP Client
This guide shows how to run a Dockerized FTP server, configure its network and ports, install the PHP ftp-client library, and use PHP code to upload and download files through the containerized service.
Environment
Docker host for containerised services.
FTP server image: https://hub.docker.com/r/fauria/vsftpd
PHP application framework: ThinkPHP 5.1
PHP FTP client library: https://github.com/Nicolab/php-ftp-client
Deploy FTP service with Docker
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/vsftpdMain parameters : --name dnmp-vsftpd – assigns a stable container name. --network dnmp_backend – connects the container to the same Docker network used by other services (required for inter‑container communication). -v e:/ftp:/home/vsftpd – bind‑mounts the host directory e:/ftp into the container at /home/vsftpd, which becomes the FTP root. -p 20:20 -p 21:21 – exposes the control ports. -p 21100-21110:21100-21110 – forwards a passive‑mode port range.
Environment variables FTP_USER, FTP_PASS, PASV_ADDRESS, PASV_MIN_PORT, PASV_MAX_PORT configure the default account and passive mode.
Verify the container
docker psThe output lists the container ID, image name, command, creation time, status, and port mappings (e.g., 0.0.0.0:20-21->20-21/tcp and 0.0.0.0:21100-21110->21100-21110/tcp). If the container is listed as Up, the FTP service is running.
Install the PHP FTP client library
docker run --rm -i -t \
-v e:/dnmp/www/tinywan.com:/app \
composer require nicolab/php-ftp-client --ignore-platform-reqsThis command mounts the application source directory into a temporary Composer container and adds nicolab/php-ftp-client to composer.json without checking platform requirements.
Upload an entire directory
$ftp = new \FtpClient\FtpClient();
$ftp->connect('dnmp-vsftpd'); // use the Docker network name, not the host IP
$ftp->login('tinywan', '123456');
$source_directory = Env::get('root_path') . 'public/upload/ftp'; // absolute host path
$target_directory = '/'; // maps to /home/vsftpd/tinywan inside the container
$res = $ftp->putAll($source_directory, $target_directory);
var_dump($res); // true on successKey points :
The FTP client connects to the container by its network alias dnmp-vsftpd. $source_directory must be an absolute path on the host; the bind‑mount makes those files visible to the FTP server. putAll() recursively uploads every file and sub‑directory from the local source to the remote target.
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 successThese snippets demonstrate basic FTP operations (list, upload, download) using the php-ftp-client library inside a Docker‑based development environment.
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.
