SoFunction
Updated on 2025-04-11

How to deploy FTP and Nginx using Docker and access files in FTP via HTTP

Docker deploys FTP and Nginx and accesses files in FTP via HTTP.

Using Docker to deploy FTP and Nginx to access files in FTP over HTTP is a common requirement, which is usually used to provide access to files stored on an FTP server over the web.

Here are the detailed steps on how to do it:

1. Deploy the FTP server (vsftpd)

We usefauria/vsftpdMirroring, because it is simple to configure and commonly used.

Create a storage directory:Create a directory on the host for FTP storage files.

mkdir -p /path/to/ftp/data

Will/path/to/ftp/dataReplace with the actual path you want to use.

Run the vsftpd container:

docker run -d -p 21:21 -p 20:20 -p 21100-21110:21100-21110 \
    -v /path/to/ftp/data:/home/vsftpd \
    -e FTP_USER=ftpuser -e FTP_PASS=ftppass \
    -e PASV_ENABLE=YES -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110 \
    --name ftpserver fauria/vsftpd
  • -p 21:21 -p 20:20: Map the control port (21) and data port (20) of FTP.
  • -p 21100-21110:21100-21110: Map the port range of passive mode. Passive mode is important for Docker environments.
  • -v /path/to/ftp/data:/home/vsftpd: Mount the host directory to the FTP user directory in the container.
  • -e FTP_USER=ftpuser -e FTP_PASS=ftppass: Set the FTP username and password. Please make sure to modify it to your own username and password.
  • -e PASV_ENABLE=YES -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110: Enable passive mode and specify port ranges, which is important for using FTP in Docker containers.
  • --name ftpserver: Name the container.

Verify the FTP connection:Use an FTP client (such as FileZilla) to connect to your server.

2. Deploy Nginx

Create an Nginx configuration file:Create a name calledThe content of the file is as follows:

user  root; # Users use root, otherwise they do not have permission to access files uploaded by FTP usersworker_processes  auto;

error_log  /var/log/nginx/ notice;
pid        /var/run/;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx//*.conf; # Comment out this sentence, otherwise the default configuration inside will override the server configuration below        server {
                listen       80;
                listen  [::]:80;
                server_name  localhost; # Modify to your domain name or IP address
                location / {
                        root   /ftp/data; # The root directory accessed by Nginx will be implemented later by mounting the FTP data directory                        index   ;
                }

                error_page   500 502 503 504  /;
                location = / {
                        root   /ftp/data;
                }
        }
}

Running the Nginx container:

Bash

docker run -d -p 80:80 \
    -v /path/to/ftp/data:/ftp/data \
    -v $(pwd)/:/etc/nginx// \
    --name webserver nginx:latest
  • -p 80:80: Map port 80 of Nginx.
  • -v /path/to/ftp/data:/ftp/data: Key steps!Mount the FTP data directory into the Nginx container/ftp/dataTable of contents. In this way, Nginx can access files on FTP.
  • -v $(pwd)/:/etc/nginx//: Mount the Nginx configuration file we created into the container.
  • --name webserver: Name the container.
  • nginx:latest: Use the latest Nginx image.

3. Access FTP files

Now you can access the server's IP address through your browser (e.g.http://your_server_ip) to browse the files on FTP. Nginx will provide a file list, you can click on the link to download the file.

Use Docker Compose (recommended)

Use Docker Compose to manage multiple containers more easily.

Create a name calledThe content of the file is as follows:

version: "3.9"
services:
  ftpserver:
    image: fauria/vsftpd
    ports:
      - "21:21"
      - "20:20"
      - "21100-21110:21100-21110"
    volumes:
      - ./ftpdata:/home/vsftpd # Pay attention to using relative paths    environment:
      - FTP_USER=ftpuser
      - FTP_PASS=ftppass
      - PASV_ENABLE=YES
      - PASV_MIN_PORT=21100
      - PASV_MAX_PORT=21110
    restart: always

  webserver:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./ftpdata:/ftp/data # Share the same data volume      - ./:/etc/nginx/ # Mount the configuration file    depends_on:
      - ftpserver # Depend on ftpserver startup    restart: always
volumes:
 ftpdata: #Define the data volume

Run in the directory where the file is locateddocker-compose up -dThe FTP and Nginx containers can be started simultaneously.

Key points and precautions:

  • Passive mode (PASV):In a Docker environment, FTP clients usually need to use passive mode. Ensure that the port range of passive mode is correctly configured.
  • Data volume:Use data volumes to share files between FTP and Nginx containers. This is the key to implementing this feature.
  • Security:The above configuration is just a basic example. In production environments, you need to consider security issues such as using SFTP instead of FTP, configuring a firewall, restricting access, etc.
  • Port Mapping:Make sure no other services on the host occupy these ports.
  • File permissions:Make sure both the FTP container and the Nginx container have the correct permissions to access the shared directory.

I hope the above steps can help you successfully build FTP and Nginx services and access files on FTP via HTTP. Using Docker Compose can greatly simplify the deployment and management process and is highly recommended.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.