Local SFTP with Docker

As part of a project at Kegate, I was tasked with setting up a local SFTP server, which turned out to be quite a challenging but valuable experience. This setup is an excellent way to test file transfer functionality in C# applications.

If you’re planning to use the Renci.SshNet library for SFTP connections, here’s a detailed guide on setting up a local SFTP server with Docker, along with a simple C# example to upload a file.

Step 1: Set Up the Local SFTP Server with Docker

The easiest way to start an SFTP server is by using Docker and the atmoz/sftp image, which provides a ready-to-use solution.

1. Create a docker-compose.yml File

Start by creating a docker-compose.yml file with the following content:

				
					services:
  sftp:
    container_name: sftp-server
    image: atmoz/sftp
    ports:
      - "2222:22"  # Map container's port 22 to host's port 2222
    volumes:
      - /c/local_ftp/data:/home  # Map your local directory to container's /home
      - ./users.conf:/etc/sftp/users.conf:ro  # Mount users.conf
      - ./sshd_config:/etc/ssh/sshd_config  # Mount the sshd_config file
    command: >
      sh -c "chown -R 1002:100 /home/admin && exec /entrypoint"
    restart: always

				
			

2. Create the Required Files

users.conf

Configure the SFTP server users. Example content:

				
					admin:password:1002:100

				
			

This creates an admin user with the password password, UID 1002, and GID 100.

sshd_config

Modify the SSH settings to enable connections. Example content:

				
					# /path/to/sshd_config
PermitRootLogin yes
PasswordAuthentication yes
UsePAM no
Subsystem sftp /usr/lib/openssh/sftp-server
AllowUsers admin
				
			
  • Start the Container

    Run the following commands in the directory where your docker-compose.yml file is located:

				
					docker-compose up -d

				
			

Once everything is set up, make sure the SFTP server is running in Docker by using the following command:

Step 2: Test the SFTP Server in C#

Once the server is configured and running, let’s test it with the Renci.SshNet library in C#. Here is an example C# code to transfer a text file to the configured SFTP server:

				
					using System;
using System.IO;
using Renci.SshNet;

class Program
{
    static void Main()
    {
        string host = "127.0.0.1";  // Adresse du serveur SFTP
        int port = 2222;  // Port SFTP (défini dans Docker Compose)
        string username = "admin";  // Nom d'utilisateur SFTP
        string password = "password";  // Mot de passe SFTP
        string localFilePath = "test.txt";  // Fichier local à transférer
        string remoteFilePath = "/home/admin/test.txt";  // Destination sur le serveur

        try
        {
            // Connexion au serveur SFTP
            using (var sftp = new SftpClient(host, port, username, password))
            {
                sftp.Connect();  // Connexion

                // Upload du fichier
                using (var fileStream = new FileStream(localFilePath, FileMode.Open))
                {
                    sftp.UploadFile(fileStream, remoteFilePath);
                    Console.WriteLine("Fichier uploadé avec succès !");
                }

                sftp.Disconnect();  // Déconnexion
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Erreur : " + ex.Message);
        }
    }
}

				
			

Conclusion

With Docker Compose and the Renci.SshNet library, you can quickly set up a local environment to test SFTP functionality in your C# applications. This approach simplifies development and debugging before moving to a production environment.

If you want to go further or need help integrating new technologies into your projects, we, the Kegate team, are here to assist you. Whether it’s modernising your systems, automating processes, or developing custom solutions, we’re ready to help you achieve your goals.

Good luck with your projects! 🚀

Articles Connexes