how to start postgres prior to run docker

David Pekker

I am super embarrassed to ask this question, because it seems a very basic one, but somehow I can't find the answer in docs.

I have a django app that uses postgres. In docker-compose.yaml there is the following requirement:

version: "2"

services:

database:
  image: postgres:9.5
  environment:
    POSTGRES_DB: ${POSTGRES_DATABASE}
    POSTGRES_USER: ${POSTGRES_USER}
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    POSTGRES_DATA: /var/lib/postgresql/data/pgdata

when I run my docker image: docker run -it --name myapp myimage it keeps repeating:

The database is not ready.
wait for postgres to start...

I ran postgres in detached mode: docker run -it -d postgres:9.5 but it does not help

Krzysztof Atłasik

Please have a look at this doc.

Second example is exacly what you need:

You create sh script and add it to your app container by using ADD or COPY:

#!/bin/bash
# wait-for-postgres.sh

set -e

host="$1"
shift
cmd="$@"

until psql -h "$host" -U "postgres" -c '\l'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

Then you modify your docker-compose.yaml like this:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-postgres.sh", "db", "python", "app.py"]
  db:
    image: postgres

In command you're orerriding default command of your container. Of course "python", "app.py" part is depedent on how you start your app. Fo Java it would be for example "java", "-jar", "my-app.jar" etc.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How would one specify which containers to start and not to start in docker-compose? (docker run vs docker create)

分類Dev

Oracle SQL start with/prior

分類Dev

How to start Docker daemon on Ubuntu?

分類Dev

How to make Postgres start automatically on boot

分類Dev

Docker and Gitlab - how to modify the docker run

分類Dev

npm install and run dev for laravel / vue on Docker container start

分類Dev

How to customize a offical docker postgres image and build it?

分類Dev

How to delete postgres database within docker container

分類Dev

How to run multiple commands via START command

分類Dev

How to set a flag in docker at the time of start

分類Dev

docker-compose up versus docker-compose run, why the latter seems not to start the services?

分類Dev

How to run a cron job inside a docker container?

分類Dev

How to run 2 commands with docker exec

分類Dev

How to run symfony via docker-composer

分類Dev

How to run docker containers in their network with an external gateway?

分類Dev

How to run Tensorboard and jupyter concurrently with docker?

分類Dev

How to schedule a docker run on google cloud

分類Dev

How can I run Thanos on Docker?

分類Dev

how to actually install mediawiki and run with docker?

分類Dev

How do I run a sql file of inserts through docker run?

分類Dev

Docker run: how to run Ubuntu:16.04 without command?

分類Dev

How to calculate running total for prior years in R?

分類Dev

Rake cannot run Rspec specs in Docker/Rails/Postgres setup (but web app works fine)

分類Dev

How to connect to Postgres database on Docker in Windows 10 using SQLAlchemy?

分類Dev

How to delete and recreate a postgres database using a single docker command?

分類Dev

How start remote debug on the very first run in react native

分類Dev

How to run powershell script on computer start-up

分類Dev

How to start mongo docker image with db user and password

分類Dev

How can I find start up scripts, or any batch files that run on start up?

Related 関連記事

  1. 1

    How would one specify which containers to start and not to start in docker-compose? (docker run vs docker create)

  2. 2

    Oracle SQL start with/prior

  3. 3

    How to start Docker daemon on Ubuntu?

  4. 4

    How to make Postgres start automatically on boot

  5. 5

    Docker and Gitlab - how to modify the docker run

  6. 6

    npm install and run dev for laravel / vue on Docker container start

  7. 7

    How to customize a offical docker postgres image and build it?

  8. 8

    How to delete postgres database within docker container

  9. 9

    How to run multiple commands via START command

  10. 10

    How to set a flag in docker at the time of start

  11. 11

    docker-compose up versus docker-compose run, why the latter seems not to start the services?

  12. 12

    How to run a cron job inside a docker container?

  13. 13

    How to run 2 commands with docker exec

  14. 14

    How to run symfony via docker-composer

  15. 15

    How to run docker containers in their network with an external gateway?

  16. 16

    How to run Tensorboard and jupyter concurrently with docker?

  17. 17

    How to schedule a docker run on google cloud

  18. 18

    How can I run Thanos on Docker?

  19. 19

    how to actually install mediawiki and run with docker?

  20. 20

    How do I run a sql file of inserts through docker run?

  21. 21

    Docker run: how to run Ubuntu:16.04 without command?

  22. 22

    How to calculate running total for prior years in R?

  23. 23

    Rake cannot run Rspec specs in Docker/Rails/Postgres setup (but web app works fine)

  24. 24

    How to connect to Postgres database on Docker in Windows 10 using SQLAlchemy?

  25. 25

    How to delete and recreate a postgres database using a single docker command?

  26. 26

    How start remote debug on the very first run in react native

  27. 27

    How to run powershell script on computer start-up

  28. 28

    How to start mongo docker image with db user and password

  29. 29

    How can I find start up scripts, or any batch files that run on start up?

ホットタグ

アーカイブ