运行命令时docker-compose找不到要注入到Postgresql的文件

Moh*_*lla 5 postgresql docker docker-compose

我正在尝试将.backup文件还原到Postgresql数据库。为此,我使用docker-compose文件启动postgres docker容器:

docker-compose.yml

   postgresql:
     image: postgres
     restart: always
     ports:
       - "5432:5432"
     environment:
       - POSTGRES_USER:postgres
     # - PGDATA:/var/lib/postgresql/data/pgdata/
     volumes:
       - ${PWD}/project/data/MX/bkup/data:/var/lib/postgresql
     command: 
       - pg_restore -U postgres -d postgres /var/lib/postgresql/ph.backup
Run Code Online (Sandbox Code Playgroud)

当我使用以下命令运行docker-compose文件时:

docker-compose up postgresql

我收到错误:

   postgresql:
     image: postgres
     restart: always
     ports:
       - "5432:5432"
     environment:
       - POSTGRES_USER:postgres
     # - PGDATA:/var/lib/postgresql/data/pgdata/
     volumes:
       - ${PWD}/project/data/MX/bkup/data:/var/lib/postgresql
     command: 
       - pg_restore -U postgres -d postgres /var/lib/postgresql/ph.backup
Run Code Online (Sandbox Code Playgroud)

即使文件在卷中,也会发生这种情况

(virtual) med@nid:~/projects/project/pkg$ docker-compose up postgresql 
Recreating pkg_postgresql_1 ... 
Recreating pkg_postgresql_1 ... done
Attaching to pkg_postgresql_1
postgresql_1     | /usr/local/bin/docker-entrypoint.sh: line 176: /pg_restore -U postgres -d postgres /var/lib/postgresql/ph.backup: No such file or directory
pkg_postgresql_1 exited with code 127

Run Code Online (Sandbox Code Playgroud)

我试图在docker-compose命令中使用-h参数pg_restore:

pg_restore -h tcp://`docker-machine ip default`:5432 -U postgres -d postgres /var/lib/postgresql/ph.backup
Run Code Online (Sandbox Code Playgroud)

可行的方法:如果我在中注释了命令目标docker-compose.yml,请启动docker容器并在其中运行命令,然后注入数据!

有没有解决的办法,意思是,有没有办法使命令直接从docker-compose.yml文件中起作用?

Dav*_*aze 4

There are two forms of Docker Compose command:. You should move the command up on to the same line

command: pg_restore -U postgres -d postgres /var/lib/postgresql/ph.backup
Run Code Online (Sandbox Code Playgroud)

The form you have individually spells out each argument (in a YAML list); for example

command:
  - /bin/ls
  - -l
  - -r
  - -t
Run Code Online (Sandbox Code Playgroud)

(Also consider just installing the PostgreSQL client tools on your host and running this outside of Docker; use localhost as the host name and the first number from the database container's ports: as the port number.)