Django通过码头连接到Postgres-Compose

jgwigjjp  于 2022-10-23  发布在  Go
关注(0)|答案(3)|浏览(112)

我的Django项目无法连接到Postgres数据库容器。我该怎么做呢?
它在执行命令python manage.py collectstatic --noinput && python manage.py makemigrations blog && python manage.py migrate时崩溃。我知道docker run命令创建了一个新的容器,但我在docker-compose.yml中通过bash创建了更多的命令。它应该行得通,不是吗?
我的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"

我的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', 'admin@example.com', '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
    }
}

...

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

我使用:Windows 10 Docker 17.03.0-ce-Win1-(10296)Docker-ComposeVersion 1.11.2 Django==1.10.6心理拷贝2==2.7.1。
感谢您的答复

zzzyeukh

zzzyeukh1#

默认情况下,docker中的每个容器都有自己的主机名和IP。当Compose为您启动容器时,默认情况下,它还会将所有容器放在一个网络上,以允许基于DNS的发现。
这意味着您的数据库不能在本地主机上访问,但您可以通过服务名“db”访问它。更改settings.py中的此行:

'HOST': 'localhost',

致:

'HOST': 'db',
waxmsbnn

waxmsbnn2#

以下是对我起作用的方法:
在作曲中:

version: '3.7'
services: 
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes: 
        - .:/code
    ports: 
        - 8000:8000
    environment: 
        - DB_HOST=db
        - DB_NAME=web
        - DB_USER=postgres
        - DB_PASSWORD=postgres

    depends_on: 
        - db

  db:
    image: postgres:12-alpine
    volumes: 
        - postgres_data:/var/lib/postgresql/data/
    environment: 
        - POSTGRES_DB=web
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
volumes:
   postgres_data:

在设置中:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': os.environ.get('DB_NAME'),
    'USER':os.environ.get('DB_USER'),
    'PASSWORD': os.environ.get('DB_PASSWORD'),
    'HOST':os.environ.get('DB_HOST'),
    'PORT':5432,
 }
}
elcex8rz

elcex8rz3#

Django settings.py->Database={host:db}。其他数据库选项保持不变。喜欢

DATABASE_ENGINE=django.db.backends.postgresql
DATABASE_USER=postgres
DATABASE_PASSWORD=1234
DATABASE_NAME=pos
DATABASE_HOST=db
DATABASE_PORT=5432

Docker Compose YML文件:此设置为Django、Nginx、Postgres创建

version: '3.8'
services:
  nginx:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    ports:
      - '81:81'
    volumes:
      - static_volume:/home/pos/static/
      - ./docker/nginx/development:/etc/nginx/conf.d
    depends_on:
      - backend
  backend:
    restart: unless-stopped
    build:
      context: .
      dockerfile: ./docker/backend/Dockerfile
    image: pos-backend:backend # image_name: image_tag
    container_name: pos_backend
    volumes:
      - ./backend:/home/pos/
      - static_volume:/home/pos/static
      - media_volume:/home/pos/media
    environment:
      DJANGO_SETTINGS_MODULE: pos.settings
    ports:
      - "8000:8000"
    command: python manage.py runserver 0.0.0.0:8000
    expose:
      - 8000
    env_file:
      - ./backend/.env.dev
    depends_on:
      - db
  db:
    image: postgres:12
    container_name: pos_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=1234
      - POSTGRES_DB=pos
    ports:
      - '5432:5432'
    expose:
      - 5432
    networks:
      - default
volumes:
  static_volume: {}
  media_volume:
  postgres_data:

相关问题