Docker连接到“现有”数据库,但显示为空

我发现了大约15种不同的方式来设置Docker以连接到现有的Postgre数据库。他们都没有为我工作。看来我正在连接数据库。它似乎是空的。这是docker-compose up --build的最终输出:

db_1   |
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   |
db_1   | 2020-05-22 21:32:42.213 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-05-22 21:32:42.214 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-05-22 21:32:42.214 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-05-22 21:32:42.216 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-05-22 21:32:42.227 UTC [26] LOG:  database system was shut down at 2020-05-22 21:32:32 UTC
db_1   | 2020-05-22 21:32:42.231 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 38 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, base, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | May 22, 2020 - 21:32:44
web_1  | Django version 3.0.4, using settings 'merrittsecurity.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

它说我有38个未应用的迁移,这一事实告诉我它正在使用一个空数据库。我的PyCharm Django项目在Docker外部运行良好,在转换之前,它在带有SQLite3的Docker中运行良好。

日志条目:侦听IPv4地址“ 0.0.0.0”,端口5432似乎很奇怪。这可能是问题吗?

docker-compose.yml:

version: '3.7'

services:
  db:
#    image: keinos/sqlite3:latest
    image: postgres
    expose:
      - 5432
    environment:
        - POSTGRES_DB=databsename
        - POSTGRES_USER=me
        - POSTGRES_PASSWORD=dbpassword
    volumes:
      - pgdata:/usr/local/var/postgresql/
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8005:8000"
    depends_on:
      - db
volumes:
  pgdata: {}

我在这里替换了数据库名称,用户和密码。它们通常包含实际值,而不是变量。我从几个示例中得到了这种特殊的构造方法。

Dockerfile:

FROM ubuntu:18.04
FROM python:3.7.0-alpine

# Set work directory
RUN mkdir /code
WORKDIR /code

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONMALLOC debug

# Install dependencies
# The next two lines were added to get postgre to build
RUN \
 apk add --no-cache postgresql-libs && \
 apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev

# The next line was added to get pillow to build
RUN apk add build-base python-dev py-pip jpeg-dev zlib-dev

 #python3 -m pip install -r requirements.txt --no-cache-dir && \
 #apk --purge del .build-deps


#RUN apk add build-base python-dev py-pip jpeg-dev zlib-dev
ENV LIBRARY_PATH=/lib:/usr/lib

COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN apk --purge del .build-deps

# Copy django project
ADD . /code/

setting.py的相关部分:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': os.getenv('POSTGRE_USER'),
        'PASSWORD': os.getenv('POSTGRE_PASSWORD'),
        'HOST': os.getenv('POSTGRE_HOST'),
        'PORT': '5432',
    }
}

POSTGRE _...值在我的.env文件中。除了POSTGRE_HOST以外,它们在Docker之外都能正常工作。我通常将其设置为localhost。这会导致错误,因此我按照网上的示例将其替换为db。其他值与上述docker-compose.yml文件中提供的值相同。这是MacOS的最新版本。知道我在做什么错吗?