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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Deploy an FTP Server in Docker and Transfer Files with PHP

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/vsftpd
Parameter 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/tcp
Test 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.

FTP service login screen
FTP service login screen
FTP login credentials
FTP login credentials

Install PHP dependency

docker run --rm -i -t -v e:/dnmp/www/tinywan.com:/app composer require nicolab/php-ftp-client --ignore-platform-reqs

Upload 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 success
Key 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).

Host directory mapping
Host directory mapping

Container file system view:

Container file view
Container file 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 success

Download 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 success

FTP directory structure

FTP directory structure
FTP directory structure
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

containerFTPvsftpdfile-upload
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.