简体   繁体   English

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

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

I am trying to run the following docker-compose file: 我正在尝试运行以下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

I am encountering an error with the line: 我在该行遇到错误:

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

It is because pgsql service is not started. 这是因为pgsql服务没有启动。 It can be monitored with command: ps -aux 可以使用以下command: ps -aux监视它command: ps -aux

How can I run my script once pgsql service is started ? pgsql服务启动后,如何运行脚本?

You can use a volume to provide an initialization sql script: 您可以使用卷来提供初始化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

This will work because original Posgresql dockerfile contains a script (that runs after Posrgres has been started) which will execute any *.sql files from /docker-entrypoint-initdb.d/ folder. 这将起作用,因为原始Posgresql dockerfile包含一个脚本(在Posrgres启动后运行),该脚本将执行/docker-entrypoint-initdb.d/文件夹中的所有*.sql文件。

By mounting your local volume in that place, your sql files will be run at the right time. 通过在该位置挂载本地卷,您的sql文件将在正确的时间运行。

It's actually mentioned in documentation for that image: https://hub.docker.com/_/postgres under the How to extend this image section. 该图像的文档中实际上提到了它: How to extend this image部分下的https://hub.docker.com/_/postgres

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

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