繁体   English   中英

docker-compose:在pgsql容器上运行命令

[英]docker-compose: run a command on a pgsql container

我正在尝试运行以下docker-compose文件:

version: "3"
services:
  db:
    image: postgres
    container_name: pgsql
    environment:
      - foo=foo
      - bar=bar
    volumes:
      - ./sql/:/opt/sql
    command: bash /opt/sql/create-db.sql
#    command: ps -aux
  web:
    image: benit/debian-web
    container_name: web
    depends_on:
      - db
    ports:
      - 80:80
    volumes:
      - ./html:/var/www/html

我在该行遇到错误:

command: bash /opt/sql/create-db.sql

这是因为pgsql服务没有启动。 可以使用以下command: ps -aux监视它command: ps -aux

pgsql服务启动后,如何运行脚本?

您可以使用卷来提供初始化sql脚本:

version: "3"
services:
  db:
    image: postgres
    container_name: pgsql
    environment:
      - foo=foo
      - bar=bar
    volumes:
      - ./sql/:/opt/sql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
  web:
    image: benit/debian-web
    container_name: web
    depends_on:
      - db
    ports:
      - 80:80
    volumes:
      - ./html:/var/www/html

这将起作用,因为原始Posgresql dockerfile包含一个脚本(在Posrgres启动后运行),该脚本将执行/docker-entrypoint-initdb.d/文件夹中的所有*.sql文件。

通过在该位置挂载本地卷,您的sql文件将在正确的时间运行。

该图像的文档中实际上提到了它: How to extend this image部分下的https://hub.docker.com/_/postgres

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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