Do you quickly need an SFTP server for testing something on your local developer machine?

Yes?

Here you go…

Setup

Note: I did this on a Mac with Docker Desktop up and running.

First, create a test file that will be uploaded to the SFTP server:

  • /someotherdirectory/hello_sftp.txt

Then create a directory that will be used as the Docker container volume mount:

  • /Users/youruser/sftp-in

Finally, create another directory where we will download the previously uploaded file:

  • /some-download-dir

Now pull the Docker image and run it:

$ docker run -v /Users/youruser/sftp-in:/home/foo/upload -p 2222:22 -d atmoz/sftp foo:pass

Let’s connect to the SFTP server:

$ sftp -oPort=2222 foo@localhost
foo@localhost's password:
Connected to localhost.
sftp>

Everything is in place and we can upload a file.

Uploading a File to the SFTP-Server

With the test file on the host at /someotherdirectory/hello_sftp.txt, upload it:

sftp> put /someotherdirectory/hello_sftp.txt /upload/hello_sftp.txt
Uploading /someotherdirectory/hello_sftp.txt to /upload/hello_sftp.txt
/someotherdirectory/hello_sftp.txt          100% 0  0.0KB/s 00:00

That’s it. The file has been uploaded from your local host (your developer machine) to the SFTP server; the server’s upload directory is a directory on your host.

We can check in three ways that it worked.

On the SFTP server:

sftp> ls /upload
/upload/hello_sftp.txt

On the host:

$ ls /Users/youruser/sftp-in

And from inside the Docker container (first open a shell):

docker exec -ti 743fe6ac65af /bin/bash

Then list the file:

root@743fe6ac65af:/# ls /home/foo/upload/
hello_sftp.txt

Downloading a File from the SFTP-Server

You can also download a file from the SFTP server.

Retrieve the previously uploaded file and save it in another directory:

sftp> get /upload/hello_sftp.txt /some-download-dir/hello_again.txt
Fetching /upload/hello_sftp.txt to /some-download-dir/hello_again.txt

Check it on the host:

$ ls /some-download-dir/

Thanks for reading. Hope it helped!

Feel free to buy me a coffee if you liked this post.

Resources