Docker waiting for postgresql to execute

I am using postgresql with django in my project. I got them in different containers, and the problem is that I need to wait for postgres before running django. At this time, I am doing this with sleep 5in the command.sh file for the django container. I also found that netcat can do the trick, but I would prefer a path without additional packages. curl and wget cannot do this because they do not support the postgres protocol. Is there any way to do this?

+8
source share
8 answers

The problem with your tiziano solution is that curl is not set by default, and I wanted to avoid installing additional materials. In any case, I did what I said. Here is a script if someone needs it.

import socket
import time
import os

port = int(os.environ["DB_PORT"]) # 5432

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    try:
        s.connect(('myproject-db', port))
        s.close()
        break
    except socket.error as ex:
        time.sleep(0.1)
+7
source

If you have one, psqlyou can simply add the following code to your .sh file:

RETRIES=5

until psql -h $PG_HOST -U $PG_USER -d $PG_DATABASE -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
  echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..."
  sleep 1
done
+15
source

Postgres. ( 6). npm start , Postgres.

services:
  practice_docker: 
    image: dockerhubusername/practice_docker
    ports: 
      - 80:3000
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; npm start'
    depends_on:
      - db
    environment:
      - DATABASE_URL=postgres://postgres:password@db:5432/practicedocker
      - PORT=3000   
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=practicedocker
+12

- , , TCP.

Dockerfile

RUN git clone https://github.com/vishnubob/wait-for-it.git

-compose.yml

version: "2"
services:
   web:
     build: .
     ports:
       - "80:8000"
     depends_on:
       - "db"
     command: ["./wait-for-it/wait-for-it.sh", "db:5432", "--", "npm",  "start"]
   db:
     image: postgres
+4

?

- :

while ! curl http://$POSTGRES_PORT_5432_TCP_ADDR:$POSTGRES_PORT_5432_TCP_PORT/ 2>&1 | grep '52'
do
  sleep 1
done

.

+2

- bash-:

while ! nc -z HOST PORT; do sleep 1; done;
./run-smth-else;
+1

. Docker . , db , - ur db, . , , . , . :) : docker-compose 2.1.

version: '2.1'

services:
  my-app:
    build: .
    command: su -c "python manage.py runserver 0.0.0.0:8000"
    ports:
       - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
    links:
      - db
    volumes:
      - .:/app_directory

  db:
    image: postgres:10.5
    ports:
      - "5432:5432"
    volumes:
      - database:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  database:

.sh. , , ;) Cya

0

, docker .

Here is the documentation.

In your yaml file, you can set properties linksto express dependency between services. Then the service will start as you wish.

-3
source

Source: https://habr.com/ru/post/1626418/


All Articles