繁体   English   中英

Docker 运行,在 docker-compose 构建后无法将主机名“db”转换为地址:名称或服务未知

[英]Docker run, after docker-compose build could not translate host name “db” to address: Name or service not known

我目前有一个使用 docker-compose 构建的系统,它创建了一个 Django 应用程序。 我在测试版本中使用了容器(postgresql)内的数据库。 我想将我的 docker 图像上传到 docker-hub。

当我跑步时

$ sudo docker-compose up  

服务器成功上线

Recreating rest_api_db_1 ... done
Recreating rest_api_web_1 ... done
Attaching to rest_api_db_1, rest_api_web_1
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2021-06-08 07:22:10.698 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2021-06-08 07:22:10.699 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2021-06-08 07:22:10.699 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2021-06-08 07:22:10.708 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2021-06-08 07:22:10.719 UTC [26] LOG:  database system was shut down at 2021-06-08 07:15:22 UTC
db_1   | 2021-06-08 07:22:10.726 UTC [1] LOG:  database system is ready to accept connections
web_1  | Operations to perform:
web_1  |   Apply all migrations: admin, api_bus_routes, auth, contenttypes, sessions
web_1  | Running migrations:
web_1  |   No migrations to apply.
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  | June 08, 2021 - 07:22:12
web_1  | Django version 3.2.4, using settings 'rest_api.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

但是当我尝试构建和运行时

$ sudo docker-compose build
$ sudo docker run rest_api_web

我收到一个错误

psycopg2.OperationalError:无法将主机名“db”转换为地址:名称或服务未知

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known

我的 Docker-compose.yml

version: "3.8"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    depends_on:
      - db
    command: bash -c "python manage.py migrate &&
                      python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"

我的 Dockerfile

# syntax=docker/dockerfile:1
FROM python:3

ENV PYTHONUNBUFFERED=1

RUN mkdir /code
WORKDIR /code
COPY . /code/

RUN pip install -r requirements.txt

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

我的设置.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB', default='postgres'),
        'USER': os.environ.get('POSTGRES_USER', default='postgres'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD', default='postgres'),
        'HOST': os.environ.get('POSTGRES_HOST', default='db'),
        'PORT': "5432"
    }
}

Docker-compose 只能与所有服务一起运行,并在存在db的情况下创建自己的网络。 但是如果你运行单个容器,网络不存在,也找不到 db-container。 要么运行 docker-compose,要么在你的 settings.py 中定义 db 所在的有效主机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM