简体   繁体   中英

How to solve problem with empty docker-entrypoint-initdb.d? (PostgresQL + Docker)

Here is a part of my project structure:

在此处输入图片说明

Here is a part of my docker-compose.yml file: docker-compose.yml

Here is my Dockerfile (which is inside postgres-passport folder): 文件

I have init.sql script which should create user, database and tables (user and db are the same as in docker-compose.yml file)

But when I look into my docker-entrypoint-initdb.d folder it is empty (there is no init.sql file). I use this command:

docker exec latest_postgres-passport_1 ls -l docker-entrypoint-initdb.d/

On my server (Ubuntu) I see: 空文件夹

I need your help, what am I doing wrong? (how can I copy a folder with init.sql script. Postgres tell me that

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

(as he can't find this folder)

All code in text format below:

Full docker-compose.yml:

version: '3'

volumes:
  redis_data: {}
  proxy_certs: {}
  nsq_data: {}
  postgres_passport_data: {}
  storage_data: {}

services:

  # ####################################################################################################################
  # Http services
  # ####################################################################################################################
  back-passport:
    image: ${REGISTRY_BASE_URL}/backend:${TAG}
    restart: always
    expose:
      - 9000
    depends_on:
      - postgres-passport
      - redis
      - nsq
    environment:
      ACCESS_LOG: ${ACCESS_LOG}
      AFTER_CONFIRM_BASE_URL: ${AFTER_CONFIRM_BASE_URL}
      CONFIRM_BASE_URL: ${CONFIRM_BASE_URL}
      COOKIE_DOMAIN: ${COOKIE_DOMAIN}
      COOKIE_SECURE: ${COOKIE_SECURE}
      DEBUG: ${DEBUG}
      POSTGRES_URL: ${POSTGRES_URL_PASSPORT}
      NSQ_ADDR: ${NSQ_ADDR}
      REDIS_URL: ${REDIS_URL}
      SIGNING_KEY: ${SIGNING_KEY}
    command: "passport"

  # ####################################################################################################################
  # Background services
  # ####################################################################################################################
  back-email:
    image: ${REGISTRY_BASE_URL}/backend:${TAG}
    restart: always
    depends_on:
      - nsqlookup
    environment:
      DEFAULT_FROM: ${EMAIL_DEFAULT_FROM}
      NSQLOOKUP_ADDR: ${NSQLOOKUP_ADDR}
      MAILGUN_DOMAIN: ${MAILGUN_DOMAIN}
      MAILGUN_API_KEY: ${MAILGUN_API_KEY}
      TEMPLATES_DIR: "/var/templates/email"
    command: "email"

  # ####################################################################################################################
  # Frontend apps
  # ####################################################################################################################
  front-passport:
    image: ${REGISTRY_BASE_URL}/frontend-passport:${TAG}
    restart: always
    expose:
      - 80

  # ####################################################################################################################
  # Reverse proxy
  # ####################################################################################################################
  proxy:
    image: ${REGISTRY_BASE_URL}/proxy:${TAG}
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - "proxy_certs:/root/.caddy"
    environment:
      CLOUDFLARE_EMAIL: ${CLOUDFLARE_EMAIL}
      CLOUDFLARE_API_KEY: ${CLOUDFLARE_API_KEY}
  #      ACME_AGREE: 'true'

  # ####################################################################################################################
  # Services (database, event bus etc)
  # ####################################################################################################################
  postgres-passport:
    image: postgres:latest
    restart: always
    expose:
      - 5432
    volumes:
      - "./postgres-passport:/docker-entrypoint-initdb.d"
      - "./data/postgres_passport_data:/var/lib/postgresql/data"
    environment:
      POSTGRES_DB: ${POSTGRES_PASSPORT_DB}
      POSTGRES_USER: ${POSTGRES_PASSPORT_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSPORT_PASSWORD}

  redis:
    image: redis
    restart: always
    expose:
      - 6379
    volumes:
      - "redis_data:/data"

  nsqlookup:
    image: nsqio/nsq:v1.1.0
    restart: always
    expose:
      - 4160
      - 4161
    command: /nsqlookupd

  nsq:
    image: nsqio/nsq:v1.1.0
    restart: always
    depends_on:
      - nsqlookup
    expose:
      - 4150
      - 4151
    volumes:
      - "nsq_data:/data"
    command: /nsqd --lookupd-tcp-address=nsqlookup:4160 --data-path=/data

  # ####################################################################################################################
  # Ofelia cron job scheduler for docker
  # ####################################################################################################################
  scheduler:
    image: mcuadros/ofelia
    restart: always
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./etc/scheduler:/etc/ofelia"

Dockerfile:

FROM postgres:latest

COPY init.sql /docker-entrypoint-initdb.d/

In your docker-compose.yml file, you say in part:

postgres-passport:
  image: postgres:latest
  volumes:
    - "./postgres-passport:/docker-entrypoint-initdb.d"
    - "./data/postgres_passport_data:/var/lib/postgresql/data"

So you're running the stock postgres image (the Dockerfile you show never gets called); and whatever's in your local postgres-passport directory, starting from the same directory as the docker-compose.yml file, appears as the /docker-entrypoint-initdb.d directory inside the container.

In the directory tree you show, if you

cd deploy/latest
docker-compose up

The ./postgres-passport is expected to be in the deploy/latest tree. Since it's not actually there, Docker doesn't complain, but just creates it as an empty directory.

If you're just trying to inject this configuration file, using a volume is a reasonable way to do it; you don't need the Dockerfile. However, you need to give the correct path to the directory you're trying to mount into the container.

postgres-passport:
  image: postgres:latest
  volumes:
    #  vvv Change this path vvv
    - "../../postgres-passport/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d"
    - "./data/postgres_passport_data:/var/lib/postgresql/data"

If you want to use that Dockerfile instead, you need to tell Docker Compose to build the custom image instead of using the standard one. Since you're building the init file into the image, you don't also need a bind-mount of the same file.

postgres-passport:
  build: ../../postgres-passport
  volumes:
    # Only this one
    - "./data/postgres_passport_data:/var/lib/postgresql/data"

(You will also need to adjust the COPY statement to match the path layout; just copying the entire local docker-entrypoint-initdb.d directory into the image is probably the most straightforward thing.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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