如何使用现有的ENTRYPOINT修改Dockerfile以添加“ wait-for-it.sh”?

时间:2019-10-04 00:45:47

标签: docker docker-compose

我想修改Docker映像wrouesnel/postgres_exporter,使其可以在Docker Compose多容器应用程序中使用,并等待Postgres接受连接。我的项目具有以下结构:

.
├── docker-compose.yml
└── exporter
    ├── Dockerfile
    └── wait-for-it.sh

docker-compose.yml读取的地方

version: "3"
services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: mypassword
    networks:
      - mynetwork

  exporter:
    build: exporter
    environment:
      DATA_SOURCE_NAME: "postgresql://postgres:mypassword@db:5432/postgres?sslmode=disable"
    ports:
      - "9187:9187"
    networks:
      - mynetwork

networks:
  mynetwork:

wait-for-it.shhttps://github.com/vishnubob/wait-for-it/blob/master/wait-for-it.sh获得,并且Dockerfile读取

FROM wrouesnel/postgres_exporter
COPY wait-for-it.sh wait-for-it.sh
ENTRYPOINT ["./wait-for-it.sh", "db:5432", "--", "./postgres_exporter"]

但是,如果我docker-compose build并尝试docker-compose up,则会出现以下错误:

> docker-compose up
Recreating postgres-performance-testing_exporter_1 ... 
Recreating postgres-performance-testing_exporter_1 ... done
Attaching to postgres-performance-testing_db_1, postgres-performance-testing_exporter_1
exporter_1  | standard_init_linux.go:211: exec user process caused "no such file or directory"

我不知道找不到哪个文件,为什么不呢?都不都复制到我的WORKDIR吗?

1 个答案:

答案 0 :(得分:1)

您不能在wrouesnel/postgres_exporter映像中以wait-for-it.sh的形式运行bash脚本,因为等待是完全基于bash的。

如果您从头开始研究wrouesnel的Dockerfile,那么此映像中根本没有任何内容。

FROM scratch
ARG binary
COPY $binary /postgres_exporter
EXPOSE 9187
ENTRYPOINT [ "/postgres_exporter" ]

因此,使用此类映像的方法是将它们用作多阶段映像并复制二进制文件并扩展您的映像。

FROM wrouesnel/postgres_exporter 

FROM debian:7.11-slim
RUN useradd -u 20001 postgres_exporter
USER postgres_exporter
COPY --from=0 /postgres_exporter /postgres_exporter
EXPOSE 9187
COPY wait-for-it.sh wait-for-it.sh
USER root
RUN chmod +x wait-for-it.sh
USER postgres_exporter
RUN pwd
ENTRYPOINT ["./wait-for-it.sh", "db:5432", "--", "./postgres_exporter"]