Skip to main content

Using a Local SFTP-Server for Testing

·2 mins

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, a test file is needed that is going to be uploaded onto the SFTP-server.

  • /someotherdirectory/hello_sftp.txt

Then, a directory that is being used as volume-mount for the Docker container.

  • /Users/youruser/sftp-in

Finally, another directory is needed where we will download the previously uploaded file to:

  • /some-download-dir

It’s time to 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>

Now, everything is in its place and we can upload a file.

Uploading a File to the SFTP-Server #

With the test-file in its place on the host machine at /someotherdirectory/hello_sftp.txt we can now 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 about it. The file has been uploaded from your local host, aka your developer machine, to the SFTP-server, whereas the upload-directory of the SFTP-server is a directory on your host.

We can check in three ways if it worked.

On our SFTP-server:

sftp> ls /upload
/upload/hello_sftp.txt

On the host:

$ ls /Users/youruser/sftp-in

Just for the fun of it, from inside the Docker container, by first opening a shell:

docker exec -ti 743fe6ac65af /bin/bash

From there, we list the file:

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

Downloading a File from the SFTP-Server #

Of course, we can also do it the other way round and download a file from the SFTP-server.

Let’s 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

We can check this on our host with:

$ ls /some-download-dir/

Thanks for reading. Hope it helped!

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

Resources #