development

볼륨을 사용하여 고정 된 postgres 데이터베이스에 데이터를 유지하는 방법

big-blog 2020. 6. 5. 08:06
반응형

볼륨을 사용하여 고정 된 postgres 데이터베이스에 데이터를 유지하는 방법


내 docker compose 파일에는 web, nginx 및 postgres의 세 가지 컨테이너가 있습니다. Postgres는 다음과 같습니다 :

postgres:
  container_name: postgres
  restart: always
  image: postgres:latest
  volumes:
    - ./database:/var/lib/postgresql
  ports:
    - "5432:5432

내 목표는 ./databasepostgres 컨테이너 내부 에있는 로컬 폴더에 해당하는 볼륨을로 마운트하는 /var/lib/postgres입니다. 이 컨테이너를 시작하고 postgres에 데이터를 삽입 /var/lib/postgres/data/base/하면 postgres 컨테이너에 추가하는 데이터로 가득 차 있지만 로컬 시스템에서는 폴더 ./database만 가져 data옵니다. 즉 ./database/data비어 있습니다. . 왜?

노트:

업데이트 1

Nick의 제안에 따라 나는 다음 docker inspect을 발견했다.

    "Mounts": [
        {
            "Source": "/Users/alex/Documents/MyApp/database",
            "Destination": "/var/lib/postgresql",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        },
        {
            "Name": "e5bf22471215db058127109053e72e0a423d97b05a2afb4824b411322efd2c35",
            "Source": "/var/lib/docker/volumes/e5bf22471215db058127109053e72e0a423d97b05a2afb4824b411322efd2c35/_data",
            "Destination": "/var/lib/postgresql/data",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],

내가 코딩하지 않은 다른 볼륨에 의해 데이터가 도난당한 것처럼 보입니다. 왜 그런지 잘 모르겠습니다. postgres 이미지가 해당 볼륨을 생성합니까? 그렇다면 다시 시작할 때 마운트 하는 볼륨 대신 해당 볼륨 을 사용 하는 방법이 있습니까? 그렇지 않으면 다른 볼륨을 비활성화하고 내 볼륨을 사용하는 좋은 방법이 ./database있습니까?

업데이트 2

Nick 덕분에 해결책을 찾았습니다! (및 다른 친구) 아래 답변하십시오.


이상하게도 솔루션은 바뀌게되었습니다.

volumes:
  - ./postgres-data:/var/lib/postgresql

volumes:
  - ./postgres-data:/var/lib/postgresql/data

모든 Postgres 데이터에 대한 공통 볼륨생성 할 수 있습니다

 docker volume create pgdata

또는 작성 파일로 설정할 수 있습니다

   version: "3"
   services:
     db:
       image: postgres
       environment:
         - POSTGRES_USER=postgres
         - POSTGRES_PASSWORD=postgress
         - POSTGRES_DB=postgres
       ports:
         - "5433:5432"
       volumes:
         - pgdata:/var/lib/postgresql/data
       networks:
         - suruse
   volumes: 
     pgdata:

볼륨 이름 pgdata 를 생성 하고이 볼륨을 컨테이너 경로에 마운트합니다.

이 볼륨을 검사 할 수 있습니다

docker volume inspect pgdata

// output will be
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/pgdata/_data",
        "Name": "pgdata",
        "Options": {},
        "Scope": "local"
    }
]

I would avoid using a relative path. Remember that docker is a daemon/client relationship.

When you are executing the compose, it's essentially just breaking down into various docker client commands, which are then passed to the daemon. That ./database is then relative to the daemon, not the client.

Now, the docker dev team has some back and forth on this issue, but the bottom line is it can have some unexpected results.

In short, don't use a relative path, use an absolute path.


I think you just need to create your volume outside docker first with a docker create -v /location --name and then reuse it.

And by the time I used to use docker a lot, it wasn't possible to use a static docker volume with dockerfile definition so my suggestion is to try the command line (eventually with a script ) .

참고URL : https://stackoverflow.com/questions/41637505/how-to-persist-data-in-a-dockerized-postgres-database-using-volumes

반응형