Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

python - Django connection to postgres by docker-compose

My django project cannot connect to postgres database container. What I should to do?

It crashes on commands python manage.py collectstatic --noinput && python manage.py makemigrations blog && python manage.py migrate. I know docker run command creates a new container but I have more commands as one by bash in docker-compose.yml. It should works, shouldn't it?

my Dockerfile:

FROM python:3.6-alpine
MAINTAINER Name <name@domain>

ENV PYTHONUNBUFFERED 1
ENV INSTALL_PATH /heckblog
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH


COPY requirements.txt requirements.txt

# make available run pip install psycopg2
RUN apk update && 
    apk add --virtual build-deps gcc python3-dev musl-dev && 
    apk add postgresql-dev
RUN pip3 install -r requirements.txt

# add bash into alpine linux
RUN apk add --update bash && rm -rf /var/cache/apk/*

COPY ./heckblog .
#RUN pip install .

CMD gunicorn -b 0.0.0.0:8000 --access-logfile - "config.wsgi:application"

my docker-compose.yml:

version: '2'


services:
    db:
        image: postgres:alpine
        environment:
            POSTGRES_USER: blogdmin
            POSTGRES_PASSWORD: password
            POSTGRES_DB: heckblog
            PGDATA: /tmp/pgdata
        volumes:
            - postgres_data:/tmp/pgdata

    web:
        build: .
        command: >
            bash -c "sleep 10 &&
            python manage.py collectstatic --noinput && 
            python manage.py makemigrations blog && 
            python manage.py migrate &&
            echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', '[email protected]', 'pass')" | python manage.py shell &&
            gunicorn -b 0.0.0.0:8000 --access-logfile - --reload "config.wsgi:application""                
        volumes:
            - ./heckblog:/heckblog
        depends_on:
            - db
        environment: 
            IN_DOCKER: 1
        ports:
            - "80:8000"


volumes:
    postgres_data:

settings.py:

...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'heckblog',
        'USER': 'blogdmin',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '', # default port
    }
}

...

Output of docker-compose up --build:

web_1  |        TCP/IP connections on port 5432?
web_1  | could not connect to server: Connection refused
web_1  |        Is the server running on host "localhost" (127.0.0.1) and accepting
web_1  |        TCP/IP connections on port 5432?
web_1  |
heckblog_web_1 exited with code 1

I use: Windows 10 Docker 17.03.0-ce-win1-(10296) docker-compose version 1.11.2 Django==1.10.6 psycopg2==2.7.1.

Thanks for answers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Each container in docker by default gets its own hostname and IP. When compose spins up the containers for you, it also places all of the containers on a network by default to permit DNS based discovery.

What this means is that your database is not reachable on localhost, but you can reach it by the service name "db". Change this line in your settings.py:

    'HOST': 'localhost',

to:

    'HOST': 'db',

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...