大网数据广告活动
服务器运维问答

Docker-compose IP address for Database

· 52CE 我爱测

I found a Django project and failed to get it running in Docker container in the following way: git clone https://github.com/hotdogee/django-blast.git $ cat requirements.txt in this files the below dependencies had to be updated:…

问题原文

原帖链接 · user977828 · CC BY-SA 4.0

I found a Django project and failed to get it running in Docker container in the following way:

  1. git clone https://github.com/hotdogee/django-blast.git

  2. $ cat requirements.txt in this files the below dependencies had to be updated:

    • kombu==3.0.30
    • psycopg2==2.8.6

I have the following Dockerfile:

FROM python:2
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

For docker-compose.yml I use:

version: "3"

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

Which IP address is docker-compose using because I have to change the below hardcoded addresses:

$ grep -ir "127.0.0.1" 
i5k/settings_prod.py:    'HOST': '127.0.0.1',
i5k/settings.py:    'HOST': '127.0.0.1',
i5k/settings.py:        'LOCATION': '127.0.0.1:11211',
Binary file i5k/settings.pyc matches
example/blastdb/AGLA_new_ids.faa.phd:13312790801051
grep: data/db: Permission denied
blast/static/blast/components/code-mirror/codemirror-4.0/mode/nginx/index.html:    fastcgi_pass   127.0.0.1:9000;
blast/static/blast/components/code-mirror/codemirror-4.0/mode/nginx/index.html:    fastcgi_pass 127.0.0.1:9000;
axes/tests.py:        reset(ip='127.0.0.1')
axes/decorators.py:            ip_address = '127.0.0.1'

Thank you in advance

已采纳解答

原帖链接 · MagiiTech · CC BY-SA 4.0

Don't use ip addresses from within containers. Instead use the links flag to connect multiple containers together, then use the linked container's name.

version: "3"

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

The link allows the web container to reach the db container with the hostname db.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-05-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。