Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't keep postgres data persistent using Github CodeSpaces with Docker-Compose

I set up a Github codespaces environment using devcontainer.json and docker-compose.yaml. Everything works fine, but the postgres database defined in docker-compose.yml loses its data every time the container needs to be re-built.

Here's the bottom part of the docker-compose.yml

      db:
        image: postgres:latest
        restart: unless-stopped
        volumes:
          - postgres-data:/var/lib/postgresql/data
        environment:
          POSTGRES_USER: test_user
          POSTGRES_DB: test_db
          POSTGRES_PASSWORD: test_pass
       
   volumes:
     postgres-data:

as you can see, I am trying to map the postgres data volume into a postgres-data volume, but this doesn't work for some reason.

What am I doing wrong that's preventing postgres data from persisting between container builds?

like image 362
JasonGenX Avatar asked Oct 14 '22 20:10

JasonGenX


2 Answers

Another option would be to look into using Spawn. (Disclaimer - I'm one of the devs working on it).

We've written some documentation about exactly how to use Spawn-hosted databases with GitHub codespaces here: https://docs.spawn.cc/blog/2021/08/01/spawn-and-codespaces

This will allow you to provision a database thats independent from the GitHub codespace and preserve data between restarts.

You get some extra features with Spawn like arbitrary save points, resets and loading back to saved revisions with Spawn - but the key functionality of spinning up a database for a GitHub codespace and preserving data is one of the things it works extremely well for.

like image 124
cjheppell Avatar answered Oct 22 '22 01:10

cjheppell


according to https://docs.github.com/en/codespaces/customizing-your-codespace/configuring-codespaces-for-your-project#dockerfile , only docker images can be pulled from source and set-up, nowhere they mention that volume persistence is guaranteed.

and after going through this https://code.visualstudio.com/docs/remote/devcontainerjson-reference looks like mounts and few other features related to volumes are not supported for codespaces.

workspaceMount : Not yet supported in Codespaces or when using Clone Repository in Container Volume.

workaround :

in .devcontainer folder where your dockerfile is present add a line like this

RUN curl https://<your_public_cloud>/your_volume.vol -O

here <your_public_cloud> can be google drive, aws or any endpoint where you have access to download the volume. its also the volume you needed to be persist.

  • and once its downloaded you can mount the volume to postgres service or make a hotswap.

  • and when you want to save, just upload the volume to your cloud storage provider.

  • repeat the process every time you build, and save and upload before "unbuild" or dismissing your codespace whatever you like to call.

hope that eases your issue, happy coding!

like image 36
nikhil swami Avatar answered Oct 22 '22 01:10

nikhil swami