Search code examples
docker-composecodespaces

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?


Solution

  • 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!