简体   繁体   中英

Docker compose mounting Volume not works

After docker-compose down not -v or --volume, and rebuild docker-compose up -d --build

There is no data in the new PostgreSQL container.

Strangely, I have another volume persistent_vol as you can see, and it works perfectly.

This is my docker-compose.yml

# the version of Docker-compose
version: "3"

services:
  app:
    build:
      context: . 
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
      - persistent_vol:/vol/web
    command: > 
      sh -c "python manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=americanos
      - DB_USER=postgres
      - DB_PASS=supersecretpassword
    depends_on:
      - db

  db:
    image: postgres:11-alpine
    volumes:
      - db_vol:/vol/db
    environment:
      - POSTGRES_DB=americanos
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword

volumes:
  persistent_vol:
  db_vol:

This is inpect db docker container.

"Mounts": [
            {
                "Type": "volume",
                "Name": "americanos-project_db_vol",
                "Source": "/var/lib/docker/volumes/americanos-project_db_vol/_data",
                "Destination": "/vol/db",
                "Driver": "local",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "27b655ba943c50d63af79584f764bd3944b0918ceb299621a5f0e80562fc9638",
                "Source": "/var/lib/docker/volumes/27b655ba943c50d63af79584f764bd3944b0918ceb299621a5f0e80562fc9638/_data",
                "Destination": "/var/lib/postgresql/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

Did I miss something?

Set /vol/db as PGDATA environment variable or change volumed path to /var/lib/postgresql/data because postgres default data file are at /var/lib/postgresql/data . https://hub.docker.com/_/postgres

  db:
    image: postgres:11-alpine
    volumes:
      - db_vol:/vol/db
    environment:
      - POSTGRES_DB=americanos
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword
      - PGDATA=/vol/db # added

or

  db:
    image: postgres:11-alpine
    volumes:
      - db_vol:/var/lib/postgresql/data # changed
    environment:
      - POSTGRES_DB=americanos
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM