# How to add persistent storage for Balena OS on Raspberry PI?

**URL:** <https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929>\
**Category:** balenaOS\
**Created:** [June 18, 2019, 2:38pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929 "2019-06-18T14:38:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 18, 2019, 2:38pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/1 "2019-06-18T14:38:49Z")

</div>

I am using balena os on Raspberry Pi.

Now I want to store the database and logs. I have multi containerized Application.

I read the documentation But I am unable to understand How can I add this in my docker-compose.yml?

> **[Communicate outside the container - Balena Documentation](https://www.balena.io/docs/learn/develop/runtime/#balenaos-v2120-and-above)**
>
> Talk to the host OS, supervisor, and network from within a {{ $names.company.lower }} container

Can anybody here provide a basic example for adding persistent storage for multiple application?

---

<div class="post-metadata">

**Author:** ![hedss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/hedss/32/210_2.png) [@hedss](https://forums.balena.io/u/hedss)\
**Post date:** [June 18, 2019, 2:49pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/2 "2019-06-18T14:49:14Z")

</div>

Hi there @sharvin26,

Permanent storage for a multicontainer application is described in our documentation here: [https://www.balena.io/docs/learn/develop/multicontainer/#named-volumes](https://www.balena.io/docs/learn/develop/multicontainer/#named-volumes)

As a brief guide, you can define a named volume in your `docker-compose.yml` and then bind that volume to any services you define, for example:

```auto
...
volumes:
  shared-data: {}
  service-one-data: {}
  service-two-data: {}
services:
  serviceOne:
    ...
    volumes:
      - 'shared-data:/shared'
      - 'service-one-data:/private'
  serviceTwo:
    ...
    volumes:
      - 'shared-data:/shared'
      - 'service-two-data:/private'

```

In the example above, the ‘shared-data’ volume is permanent storage shared by both services `serviceOne` and `serviceTwo` bound to the `/shared` path for both. `service-one-data` is only available to `serviceOne`, bound to the `/private` path, and `service-two-data` is only available to `serviceTwo`, bound to the `/private` path. In effect, there are three permanent storage volumes, one shared by both, and two private volumes.

Hope this helps, if you still have questions, please let us know!

Best regards, Heds

---

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 19, 2019, 5:20am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/4 "2019-06-19T05:20:10Z")

</div>

I read the documentation that you shared and a discussion on the balena forum [#22](https://forums.balena.io/t/multicontainer-how-to-configure-persistent-storage/2678/22)

It cleared some concepts but I still have some doubts. Now I have a database file that I want to persists through all container update and changes.

**First Question =\>**

Where should I add the database to persist it? and How can I add it?

**Second Question** =\>  
What I understood from the above 2 links is that using `balena volume ls` I can confirm if the volume is loaded. But I am confused about How docker compose file should be constructed for that purpose?

```auto
volumes:
  Test-Database: {}
service-1:
  volumes:
    - 'Test-Database': #what should be added here?

```

**Question 3** =\>

what should I pass the database path in my application code?

---

<div class="post-metadata">

**Author:** ![hedss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/hedss/32/210_2.png) [@hedss](https://forums.balena.io/u/hedss)\
**Post date:** [June 19, 2019, 8:40am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/5 "2019-06-19T08:40:26Z")

</div>

Hi @Sharvin26,

The `docker-compose` volume binding works in exactly the same was as regular docker-compose named volumes, and the link I provided ([https://www.balena.io/docs/learn/develop/multicontainer/#named-volumes](https://www.balena.io/docs/learn/develop/multicontainer/#named-volumes)) should explain how to use it.

As stated in the docs “Named volumes can be given arbitrary names and can be linked to a directory in one or more containers”. So you are linking the volume you’ve created to a named directory in your container:

```auto
volumes:
  - 'Test-Database:/path/to/persistent/database/in/service/container'

```

Best regards, Heds

---

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 19, 2019, 9:38am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/7 "2019-06-19T09:38:32Z")

</div>

I did that I constructed `docker-compose.yml` in this way =\>

```auto
volumes:
  Test-Database: {}
service-1:
  - 'Test-Database': /usr/src/app/app1/Database/test.db

```

**Note**

1. I am copying my application to /usr/src/app directory in the container.
2. I haven’t other parameters here in the comment that is not related to this thread. But in my application, I have added all the parameters.

My Directory Tree Structure =\>

```auto
app1
 - application
 - database
      - test.db
app2
docker-compose.yml

```

Continuously it’s only giving me installing service.

```auto
[Logs] [6/19/2019, 3:04:27 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'
[Logs] [6/19/2019, 3:04:28 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'
[Logs] [6/19/2019, 3:04:32 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'
[Logs] [6/19/2019, 3:04:37 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'
[Logs] [6/19/2019, 3:04:46 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'
[Logs] [6/19/2019, 3:05:04 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'
[Logs] [6/19/2019, 3:05:37 PM] Installing service 'service-1 sha256:a8ed7c00ddfad0a30d657dd07994af9a0fed08015b8843366cc93783e4e9541a'

```

What am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![hedss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/hedss/32/210_2.png) [@hedss](https://forums.balena.io/u/hedss)\
**Post date:** [June 19, 2019, 10:00am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/8 "2019-06-19T10:00:24Z")

</div>

```auto
  - 'Test-Database': /usr/src/app/app1/Database/test.db

```

is not correct and does not conform to the `docker-compose` specification or the examples above. As noted in both Docker and balena documentation, everything is in quotes and is the path of a directory, not a file:

```auto
  - 'Test-Database:/usr/src/app/app1/database'

```

(note also that in your file tree the `database` directory appears to be in lower case)

I’d highly recommend you look at some of the projects in `balena-io-projects`, perhaps [https://github.com/balena-io-projects/balena-sense/blob/master/docker-compose.yml](https://github.com/balena-io-projects/balena-sense/blob/master/docker-compose.yml) which also uses a persistent storage volume.

---

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 19, 2019, 10:54am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/11 "2019-06-19T10:54:55Z")

</div>

Thanks for the Solution. It’s working now.

But suppose I have a file which is dynamically created when running the Application and I want to persists that How can I add that to the persistent storage in that case?

---

<div class="post-metadata">

**Author:** ![telphan](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@telphan](https://forums.balena.io/u/telphan)\
**Post date:** [June 19, 2019, 10:57am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/12 "2019-06-19T10:57:46Z")

</div>

I would just move that file in the persistent `/path/to/persistent/` directory.

---

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 19, 2019, 11:13am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/13 "2019-06-19T11:13:58Z")

</div>

How can I do that? Do I need to create a file using a command in dockerfile and add that file path in docker compose like this =\>

```auto
  - 'Test-Database:/usr/src/app/app1/database'

```

---

<div class="post-metadata">

**Author:** ![gelbal](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/gelbal/32/1109_2.png) [@gelbal](https://forums.balena.io/u/gelbal)\
**Post date:** [June 19, 2019, 11:59am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/14 "2019-06-19T11:59:34Z")

</div>

Hi @Sharvin26, in the dockerfile you are linking a directory used by your service (in the container) to a persistent volume in the host OS (outside of the container). Then in your application code, what you write under this linked directory persists over application and host OS updates. You don’t need to specify a file name in the docker compose file. It’d work fine as long as you write your files under that linked directory.

In your case, when you setup the database in one of your services, you could specify that this database is written on persistent storage. Heds linked an example how this is done with influxdb.

Hope these make sense.

---

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 19, 2019, 12:11pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/16 "2019-06-19T12:11:31Z")

</div>

Thanks for the Response. I got that.

---

<div class="post-metadata">

**Author:** ![Sharvin26](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Sharvin26](https://forums.balena.io/u/Sharvin26)\
**Post date:** [June 20, 2019, 9:31am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/18 "2019-06-20T09:31:27Z")

</div>

I have successfully added the database as a persistent storage but when it comes to logs I am facing issue.

I have a logging mechanism in my application but when it runs in the docker container it doesn’t write anything in the Log File.

I have confirmed this by doing ssh into the container and checking if anything is written that logfile.

---

<div class="post-metadata">

**Author:** ![CameronDiver](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@CameronDiver](https://forums.balena.io/u/CameronDiver)\
**Post date:** [June 20, 2019, 12:29pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/19 "2019-06-20T12:29:54Z")

</div>

Where is this file supposed to be located @Sharvin26?

Perhaps you could post some code and we could take a look. In general, being a container should not affect something so mundane as writing to a logfile.

---

<div class="post-metadata">

**Author:** ![stef2001](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@stef2001](https://forums.balena.io/u/stef2001)\
**Post date:** [July 25, 2019, 1:07pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/22 "2019-07-25T13:07:44Z")

</div>

Hi all.  
I’va just one more question to add to this topic (but escuse me if my question is a dumb question ☹ ).

It is possible to directly create a volume on an unmounted drive (like a USB stick on the host),…something like :

’ volumes:’  
’ - ‘shared-data:/dev/sda1’

… ord do we have to mount it before on the host, or maybe create a container that mount the USB on its filesystem… and expose it to others containers …

regards

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [July 25, 2019, 2:44pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/23 "2019-07-25T14:44:52Z")

</div>

What are you trying to do? Do you want to extract data from you device (container)?

---

<div class="post-metadata">

**Author:** ![stef2001](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@stef2001](https://forums.balena.io/u/stef2001)\
**Post date:** [July 28, 2019, 12:39pm UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/25 "2019-07-28T12:39:20Z")

</div>

Hi.  
I am trying to find the best way to share an USB stick of my host BalenaOS ?  
-can i mount it directly on my balenaOS (using FASTAB, or maybe a startup srcipt)  
-do i have to mount it on a “Data COntainer” and share to to others container via Volumes ?

I’ve maybe just find a good way to do this using the udevrules in the json.config ?

> **[balena-os/meta-balena](https://github.com/balena-os/meta-balena#udevrules)**
>
> A collection of Yocto layers used to build balenaOS images - balena-os/meta-balena

Is it a good idea ?  
Thank for you help.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/janvda/32/1271_2.png) [@janvda](https://forums.balena.io/u/janvda)\
**Post date:** [July 29, 2019, 6:37am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/26 "2019-07-29T06:37:00Z")

</div>

Interesting questions.

Regarding the mounting.  
I think you have to mount it by means of startup script in one of your containers.  
Here 2 examples:

1. [https://github.com/janvda/balena-samba-rsync](https://github.com/janvda/balena-samba-rsync) \<= mounts an USB external drive.
2. [https://github.com/janvda/balena-node-red-mqtt-nginx-TIG-stack](https://github.com/janvda/balena-node-red-mqtt-nginx-TIG-stack) \<= in this repository an USB memory stick is mounted in the influx db container.

Note that you can’t mount the same drive partition in multiple containers ([https://github.com/janvda/balena-node-red-mqtt-nginx-TIG-stack/issues/3](https://github.com/janvda/balena-node-red-mqtt-nginx-TIG-stack/issues/3)).

… but what I didn’t try out (like you suggested) is to mount it in a data container and share it to other containers via Volumes. I think that is certainly worth a try. So for that I would create a subfolder in that volume and mount the drive to that subfolder.

If that doesn’t work, you can also mount it in one container and share it via [nfs](https://en.wikipedia.org/wiki/Network_File_System) to the other containers.

kr  
Jan.

---

<div class="post-metadata">

**Author:** ![stef2001](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@stef2001](https://forums.balena.io/u/stef2001)\
**Post date:** [July 29, 2019, 6:42am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/27 "2019-07-29T06:42:02Z")

</div>

I’ve just seen that we can also interact with the host config using the supervisor API  
[https://www.balena.io/docs/reference/supervisor/supervisor-api/](https://www.balena.io/docs/reference/supervisor/supervisor-api/)

Waou, in fact the BalenaOS ecosystem is so rich that i do not know how to do a simpel thing :

- just mount my /dev/sda1 on my hostOS…  
I think it should be easy to do, and a requested feature a lot of users, but i do not find the way ☹

regards

---

<div class="post-metadata">

**Author:** ![stef2001](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@stef2001](https://forums.balena.io/u/stef2001)\
**Post date:** [July 29, 2019, 6:55am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/28 "2019-07-29T06:55:54Z")

</div>

Whaouuu… thank you for you answer…and yours projects are a plenty of usefull information .

I agree with you, i think il will mount the usb stick on a mount point on a containe, and share it with others through NFS.  
I can alos answer you question, sharing this mountpoint through a Volume doesnt work ! Because ONLY the first container wich have the mount point is seeing data from the USB stick…others just see an empty Volume.

Thank for you help

PS : i will test it very soon because it is a working configuration, nut in a second time il will continue my searc in order to mount it on the Host OS… in fact i am fearing maybe about performances issues) … did you face some performances issues on your setup ?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/janvda/32/1271_2.png) [@janvda](https://forums.balena.io/u/janvda)\
**Post date:** [July 29, 2019, 7:52am UTC](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929/29 "2019-07-29T07:52:12Z")

</div>

1/ Have you tried by first creating a subfolder in the volume and then mounting it to this subfolder ? I would at least expect that the other containers see this subfolder.  
2/ Regarding performance issues. It all depends on the amount of data you want to read/write to the USB stick and the type of USB stick.

[Next page](https://forums.balena.io/t/how-to-add-persistent-storage-for-balena-os-on-raspberry-pi/15929.md?page=2)
