-1

I have this docker-compose.yml, and I have a Postgres database and Grafana running over it to make queries on data.

version: "3"

services:
  db:
    image: postgres
    container_name: db
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=my_secret_password
      
  grafana:
    image: grafana/grafana
    container_name: grafana
    depends_on:
      - db
    ports:
      - "3000:3000"

I start this compose with the command docker-compose up, but then, if I want to not lose any data, I must run docker-compose stop instead of docker-compose down.

I also read about docker commit, but "the commit operation will not include any data contained in volumes mounted inside the container", so I guess it's no use for my needs.

What's the proper way to store the created volumes and reusing them with commands up/down, so even when recreating the containers? I must use some sort of backup methods provided by every image (so, for example, a DB export for Postgres, and some other type of export for Grafana), or there is a way to do this inside docker-compose.yml?

EDIT: I also read about volumes, but is there a standard way to store everything?

In the link provided by @DannyB, setting volumes to ./postgres-data:/var/lib/postgresql instead of ./postgres-data:/var/lib/postgresql/data caused the container to not store the actual folder.

My question is: every image must follow a particular pattern like the one above? This path to data to store the volume underlying is present in every Docker image Readme? Or is there something like:

volumes:
  - ./my_image_root:/
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
crissal
  • 2,547
  • 7
  • 25
  • Does this answer your question? [How to persist data in a dockerized postgres database using volumes](https://stackoverflow.com/questions/41637505/how-to-persist-data-in-a-dockerized-postgres-database-using-volumes) – DannyB Jun 26 '21 at 14:39
  • This is not just about Postgres, but also for other images – crissal Jun 26 '21 at 14:42
  • @crissal: Please refer this: https://stackoverflow.com/questions/67720761/how-to-know-which-data-is-within-a-docker-container-to-be-able-to-map-it-correct/67725956#67725956 similar to mysql you can use even in postgres – Akshay Hegde Jun 26 '21 at 14:56
  • Thank you @AkshayHegde, so I need everytime to specify the volume(s) of the image – crissal Jun 26 '21 at 15:25
  • @crissal what is the point of containerization if your entire container's filesystem is overridden with a bind volume? Short answer for your last volume example: don't even try to do that ! The possible volumes and their mount points entirely depend on your actual requirements and on the application being containerized. – Zeitounator Jun 26 '21 at 15:25
  • @Zeitounator I need a way to store volumes, in order to not lose them when running `docker compose down` or a fatal crash occurs to Docker. I don't want nor need to override the container fs with my data – crissal Jun 26 '21 at 15:28
  • Well then seems like you have all the info you need from below answer. There's also a worth-a-read official documentation: https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes – Zeitounator Jun 26 '21 at 15:31
  • @crissal only first time after `docker-compose up -d` whatever data you want to retain and want to keep in local directory – Akshay Hegde Jun 26 '21 at 15:33

1 Answers1

4

Docker provides for volumes as the way to persist volumes between container invocations and to share data between containers.

They are quite simple to declare and use in compose files:

volumes:
  postgres:
  grafana:

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=my_secret_password
    volumes:
      - postgres:/var/lib/postgresql/data
      
  grafana:
    image: grafana/grafana
    depends_on:
      - db
    volumes:
      - grafana:/var/lib/grafana
    ports:
      - "3000:3000"

Optionally, you can also set a local directory as your container volume with the added convince of having the files easily accessible not only from inside the container. This is especially helpful for mounting specific config files to their location in the container, you can edit the file locally like any other file restart the container with the updated configuration (certificates and other similar files also make good use of this option). And you do that like so:

    volumes:
     - /home/myusername/postgres_data/:/var/lib/postgresql/data/

PS. I have omitted the container_name and version directives from this compose.yml because (as of docker 20.10), the docker compose spec determines version automatically, and docker compose exposes enough functionality that accessing the containers directly using short names isn't necessary usually.

Noam Yizraeli
  • 4,446
  • 18
  • 35
Chris Becke
  • 34,244
  • 12
  • 79
  • 148