Sarit Sarit - 1 month ago 12
Python Question

dockerize does not delay the container initialization

I am now preparing the images for my project. I use

dockerize
to control my initialization. I am not sure that hardcode the
IP
address given by docker is way to go or not?

Problem:
backend
does not wait until the
database
finish initialization first.

Terminal says

backend_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
backend_1 | Is the server running on host "sakahama_db" (172.21.0.2) and accepting
backend_1 | TCP/IP connections on port 5432?


Here are my files:

devdb.dockerfile

FROM postgres:9.5

# Install hstore extension
COPY ./Dockerfiles/hstore.sql /docker-entrypoint-initdb.d

RUN mkdir -p /var/lib/postgresql-static/data
ENV PGDATA /var/lib/postgresql-static/data


hstore.sql

create extension hstore;


backend.dockerfile

FROM python:2

RUN apt-get update && apt-get install -y wget

ENV DOCKERIZE_VERSION v0.2.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

RUN mkdir /usr/src/app
WORKDIR /usr/src/app

COPY requirements ./requirements
RUN pip install -r requirements/local.txt

COPY . .

EXPOSE 8000

CMD echo "dockerize"
CMD ["dockerize", "-wait", "tcp://sakahama_db:5432"]

CMD echo "migrate"
CMD ["python", "sakahama/manage.py", "migrate"]

CMD echo "runserver"
CMD ["python", "sakahama/manage.py", "runserver", "0.0.0.0:8000"]


docker-compose.yml

version: "2"

services:

backend:
build:
context: .
dockerfile: Dockerfiles/backend.dockerfile
restart: "always"
environment:
DATABASE_URL: postgres://username:password@sakahama_db:5432/sakahama
REDISCLOUD_URL: redis://redis:6379/0
links:
- sakahama_db
ports:
- "9000:8000"

sakahama_db:
build:
context: .
dockerfile: Dockerfiles/devdb.dockerfile
environment:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_DB: sakahama
ports:
- "5435:5432"

redis:
image: redis
expose:
- "6379"


Question: How to use
dockerize
properly?

Update:

I had tried temporary solution like this. But it does not work
backend-entrypoint.sh


#!/bin/bash
echo "dockerize"
dockerize -wait tcp://sakahama_db:5432

echo "migrate"
python sakahama/manage.py migrate

echo "runserver"
python sakahama/manage.py runserver 0.0.0.0:8000


and docker-compose.yml:

I add one line
command: ["sh", "Dockerfiles/backend-entrypoint.sh"]

Answer

When your Postgres container is up it start to receive tcp packages you send with the command: dockerize -wait tcp://sakahama_db:5432 but it does not mean that the Postgres service is ready. It take some time to load, to set up user, password, create the db or load the databases and make all grants needed.

I had a similar issue with Flask and MySQL, I created an sh script like you did and inside it I made a simple loop to check if the service was up before starting the Flask application

I am not a shell script Senior but here follow the script:

# testing if databas is up
mysql -h database -uroot -proot databasename
ISDBUP=$?
while [[ $ISDBUP != "0" ]]; do
    echo "database is not up yet, waiting for 5 seconds"
    sleep 5;
    mysql -h database -uroot -proot databasename -e "SELECT 1;";
    ISDBUP=$?
done

# starting the application
python server.py app