Upload a file to Balena container through the scp command

Ats
3 min readApr 6, 2024

--

This is the note about how to upload files or directories to a Balena container with the scp command instead of Balena CLI

Photo by Sincerely Media on Unsplash

Background

I develop an IoT project with Balena as my job. Last week, I had a moment when I wanted to upload just one config file shared by my docker containers. Usually, it should be done by a proper process like creating a commit, pushing it to Balena, and deploying it to a device. However, I just tested one config file quickly in the production environment. So I started to look for a way to do that with my production device.

What I did

First of all, it came to my mind that I could connect to my device through SSH with thebalena ssh command. So I assumed there would be a way to connect to my device with a bare SSH command.

Then I found it. It’s the same as the way GitHub works. All I have to do is store my SSH public key in Balena.

So I thought I could also use the scp command for my purpose. The scp command uses the SSH protocol so normally I would be able to use the scp command if I can use the ssh command.

To figure it out, I opened the Balena dashboard and connected to my device from the Terminal window. Obviously, in my docker containers, I need to install thescp command by myself in Dockerfile. But I found there is already installed scp in the HostOS. So I should upload files to the HostOS container through scp . Then I tested uploading a file with the following command. You can get your local IP address from the Balena dashboard.

scp -P 2222  ./test.txt [user_user_name]@[your_local_ip]:/

However, it didn’t work. I didn’t find the test.text file in the root directly. I googled the reason and found a comment in a Github issue.

Then I modified my command like below and tested it.

scp -P 22222 test.txt [your_user_name]@[your_local_ip]:/mnt/data/

This time, it worked. I’m not 100% sure but the scp seems to upload files to the/mnt/data/ directory. I googled the directory and found the docs.

The directory is the place to share files or anything with all docker containers, which is the directory where I shared files in docker-compose using resign-data:/data. So this is the exact place where I wanted to upload my config file. The following is what the command looks like at the end.

scp -P 22222 -r config_dir/ [your_user_name]@[your_local_ip]:/mnt/data/docker/volumes/[your_app_id]_resin-data/_data/config_dir

if you change your mount directory in your docker-compose file, you can modify it by following the docs.

That’s it!

--

--

Ats
Ats

Written by Ats

I like building something tangible like touch, gesture, and voice. Ruby on Rails / React Native / Yocto / Raspberry Pi / Interaction Design / CIID IDP alumni

No responses yet