13

Each time I run the following command:

> docker-compose up -d

... in a directory where I have the following docker-compose.yaml file, I get a new, randomly named volume.

docker-compose.yaml:

version: '3.7'

services:

  pgadmin:
    restart: always
    image: dpage/pgadmin4
    container_name: pgadmin_container
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-foobar}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-password}
    ports:
      - "${PGADMIN_PORT:-8080}:80"

output at command line:

C:\postgres> docker-compose up -d
Creating network "postgres_default" with the default driver
Creating pgadmin_container ... done
C:\postgres> docker volume ls
DRIVER              VOLUME NAME
local               3ee8f970ff477052c6fba9001575a0efa0254deb8dcf73ca97d0422231c74931
C:\postgres> docker-compose down
Stopping pgadmin_container ... done
Removing pgadmin_container ... done
Removing network postgres_default
C:\postgres> docker-compose up -d
Creating network "postgres_default" with the default driver
Creating pgadmin_container ... done
C:\postgres> docker volume ls
DRIVER              VOLUME NAME
local               3ee8f970ff477052c6fba9001575a0efa0254deb8dcf73ca97d0422231c74931
local               705dad9c905eb8f1679a9ee4ff290363c40f5285b8048204cab44bce26916845
C:\postgres>

You'll see that where there was one volume with a 64-char name after the first "up", after the second call to "docker-compose up", there were two. This pattern continues.

What is causing the randomly named volumes? How do I prevent their creation of force the system re-use them?

I've actually edited down my docker-compose.yaml file to get to the bare minimum to recreate the problem. There is also, actaully, a Postgres database being started with the same file.

MalcLear
  • 1,124
  • 1
  • 10
  • 21

2 Answers2

26

If you use docker inspect dpage/pgadmin4 to have a look, you will see next:

"Volumes": {
    "/var/lib/pgadmin": {}
}, 

This means in its Dockerfile, it defines a Anonymous volumes like next:

VOLUME ["/var/lib/pgadmin"]

Above will make volume name changes every time when you up/down service. To make it not change, you could override it with Named volumes, like next:

version: '3.7'

services:
  pgadmin:
    restart: always
    image: dpage/pgadmin4
    container_name: pgadmin_container
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-foobar}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-password}
    ports:
      - "${PGADMIN_PORT:-8080}:80"
    volumes:
      - my-data:/var/lib/pgadmin

volumes:
    my-data:

You can refer to this & this to learn more about it.

atline
  • 28,355
  • 16
  • 77
  • 113
  • Different from the example YAML file I gave in my question, I did actually have a "volumes" section. My problem was that I wasn't specifying the container's directory of "/var/lib/pgadmin", but was rather using something different. In effect, it was the same as not having it. Thanks for your answer, it has resolved my issue. – MalcLear Jul 24 '19 at 12:20
4

According to the docs the image creates a volume in:

/var/lib/pgadmin

This is the working directory in which pgAdmin stores session data, user files, configuration files, and it's configuration database. Mapping this directory onto the host machine gives you an easy way to maintain configuration between invocations of the container.

A docker inspect container_id shows:

"Mounts": [
    {
        "Type": "volume",
        "Name": "70c8ae8e1de3e5d6c71fa9c63930cc75761132f4fdb75a2982b32454313b78c6",
        "Source": "/var/lib/docker/volumes/70c8ae8e1de3e5d6c71fa9c63930cc75761132f4fdb75a2982b32454313b78c6/_data",
        "Destination": "/var/lib/pgadmin",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

So it seems that an anonymos volume is created each time In order to reuse the volume use docker-compose start/stop instead of docker-compose up/down. Similar issue discussed here,

b0gusb
  • 4,283
  • 2
  • 14
  • 33
  • 2
    docker-compose down doesn’t delete volumes by default (ref. https://docs.docker.com/compose/reference/down/). – masseyb Jul 24 '19 at 06:53
  • I think this explains the reason [Preserve volume data when containers are created](https://docs.docker.com/compose/#preserve-volume-data-when-containers-are-created) – b0gusb Jul 24 '19 at 07:12