3

I ran docker system prune, forgetting about a particular Postgres database whose contents I still wanted.

I think I probably still have the volume in macOS's Docker virtual machine. However, I have no idea which it is, they all look like:

✗ docker volume ls
DRIVER    VOLUME NAME
local     0bb96275a994e1eacafe016547a194d8f91580eb2b42e47897f4c90b48f7f92a
local     1a5adc7281788533e612b8c11cede2847aaf3f9819678322950ba4c199711481
local     1a6f07dd7a704731d0ad42e57a95d6ea136951c47de6e28aa3d2b8d8d891bf4f
local     2ad31cdc84c0e606e4535b1e22c9826fdf7cf21772cdb3c1604179aea6f861ff
[70 more volumes not listed]

My questions: How can I launch a Docker container from the postgres:9.6-alpine image I was using for the application, and have it use one of those volumes to check if it's the one I am looking for?

The database container was previously created by docker-compose. If I can launch the whole application with docker-compose and have the database use a particular volume, I can quite quickly discover if it's the right one.

1 Answer 1

4

Basic loop

This little bash loop will run though all the volumes, mounting them into a temporary container and printing its contents:

for vol in $(docker volume ls --format "{{.Name}}"); do \
  echo "$vol ======================================"; \
  docker run --rm -v $vol:/tmp debian:buster ls /tmp; \
  echo '------------------------------------------'; \
done

Include a grep in the loop

To simplify search over 70 volumes, you can add grep to the command in the loop so that it only prints stuff that matches the pattern:

docker run --rm -v $vol:/tmp debian:buster ls /tmp | grep pg_; \

This way you will see only files with pg_ in name and that should be enough to find Postgres volumes.

Interact with the database in the loop

Suppose that you still have many results, and need to inspect database tables to find the right volume, you could do:

docker run -v $vol:/var/lib/postgresql/data --rm -u postgres postgres:9.6-alpine bash -c 'source /docker-entrypoint.sh; docker_temp_server_start; psql -U postgres db -c "\d"; docker_temp_server_stop; exit'

This:

  • creates a container based on postgres:9.6-alpine with the volume attached
  • starts the entrypoint script (that starts up the Postgres engine)
  • runs a psql command to list the tables of the database db

Recovering the volume

Once you found the right volume, you start PostgreSQL container with it like this:

docker run -v volume_name:/var/lib/postgresql/data postgres:9.6-alpine

You can then interact with it in the usual way. For example, use docker ps to get the name of the container, then:

docker exec -i <name of container> pg_dump -U postgres -F c db > database.dump

to dump the database.

8
  • 1
    That is really great, thanks. It has immediately reduced the list of candidates to 50. Apr 2, 2021 at 14:44
  • 1
    What I am trying to do next is to use the command to run: psql -U postgres db -c "\d", to see the names of tables, which will pinpoint the exact one I want. Apr 2, 2021 at 14:47
  • docker run --rm -v $vol:/tmp postgres:9.6-alpine psql -U postgres db -c "\d"; doesn't work in the script because the psql overrides the default behaviour, in which Postgres starts up, so it can't find a database to query. Apr 2, 2021 at 14:52
  • 1
    @DanieleProcida That is a bit harder that just list folder contents, you have to spin up the database, query it and then shut it down to proceed to the next item in loop. I can help you with it a bit later but you must know that there is a risk of data corruption on other volumes, where there is something except the database you are looking for.
    – anemyte
    Apr 2, 2021 at 14:57
  • 1
    @DanieleProcida here is, as promised, a command to start the database and execute psql: docker run -v $vol:/var/lib/postgresql/data --rm -u postgres postgres:9.6-alpine bash -c 'source /docker-entrypoint.sh; docker_temp_server_start; psql -d postgres -c "\l"; docker_temp_server_stop; exit' \ . Adjust the psql part as you like.
    – anemyte
    Apr 2, 2021 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.