dockerdocker-composedocker-volume

How to assign the name to anonymous volume which `COPY` command of the Docker creates?


Project structure

enter image description here

Well, most of files does not matter. The actual files are:

docker-compose.yaml

The Database service does not matter for this question, but I have mention it to avoid this service being suspected.

version: "3.5"

services:

  front_server:

    image: example-front_server-staging
    container_name: Example-FrontServer-Staging
    build:
      context: .
      dockerfile: "FrontServer.Dockerfile"
    ports: [ "442:442" ]

    depends_on: [ Database ]
    command: sh -c "npm run \"Database migration\" && node FrontServerEntryPoint.js --environment staging"

  Database:

    image: postgres:16.2
    container_name: Example-Database-Staging
    ports: [ "5431:5432" ]

    environment:

      - TZ="Asia/Tokyo"
      - POSTGRES_PASSWORD=EXAMPLE-NOTHING_TO_STEAL

    volumes:
      - DatabaseData:/data/example.jp

volumes:
  DatabaseData:
    name: Example-DatabaseData-Staging
    driver: local

FrontServer.Dockerfile

According the idea, each container and volume related with this image intended to be disposed after one-time usage. No important data stored inside. If some changes will be applied to source code, the new container and volume with executable code must be independent from previous one.

FROM node:18-alpine

WORKDIR /var/www/example.com

COPY . /var/www/example.com

RUN npm install --no-package-lock

Problem

Each time when I am executing docker compose, the COPY . /var/www/example.com creates something volume-like (is it volume or no, it is being listed via docker volume list) with long name:

DRIVER    VOLUME NAME
local     7bc56f377f45dff804abc633569757ac7c26d7a77305e91dd83338e4bdbb903d
local     3793cc2338491d5065cb81e14e64bde20f2f8fc87dcc393r47d774a0e1f92493

How I can assign the meaningful name to these ones?


Solution

  • The anonymous volume is in fact coming from the Database container.

    The postgres image, like most other standard database images, declares a Dockerfile VOLUME for its data directory /var/lib/postgresql/data. Since your Compose file doesn't explicitly mount anything there, Docker automatically creates an anonymous volume at startup time and mounts it.

    If the DatabaseData named volume is intended to hold the actual data, then you need to change its mount point to where PostgreSQL expects to find it

    services:
      Database:
        volumes:
          - DatabaseData:/var/lib/postgresql/data
          #              ^^^^^^^^^^^^^^^^^^^^^^^^
    

    If that volume contains some sort of seed data or is otherwise referenced directly from SQL calls, then you may need to create a second named volume and mount it on the standard data directory.