2

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

user977828
  • 205
  • 4
  • 15
  • Thank you but unfortunately, I got `web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "127.0.0.1" and accepting web_1 | TCP/IP connections on port 5432?` – user977828 May 17 '21 at 14:55

1 Answers1

1

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.

MagiiTech
  • 93
  • 1
  • 7
  • Thank you but unfortunately, I got `web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "127.0.0.1" and accepting web_1 | TCP/IP connections on port 5432?` – user977828 May 17 '21 at 21:31
  • 1
    From that output it looks like you're using "127.0.0.1" as the database host. Use "db" (or however your database service is called in docker-compose.yml) as the database host. – MagiiTech May 17 '21 at 21:45