1

I am attempting to set up a Django project with a Postgres database on Semaphore-ci via Docker-Compose. The database container does not appear to be running. I am receiving an error message 'django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known'. I have included my docker-compose.yml, semaphore.yml and settings.py below.

docker-compose.yml

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python3 ./zenmon/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/opt/app
    ports:
      - "8000:8000"
    depends_on:
      - db

semaphore.yml

# .semaphore/semaphore.yml

version: v1.0
name: Initial Pipeline
agent:
  machine:
    # Use a machine type with more RAM and CPU power for faster container
    # builds:
    type: e1-standard-2
    os_image: ubuntu1804
blocks:
  - name: Build
    task:
      # Mount a secret which defines DOCKER_USERNAME and DOCKER_PASSWORD
      # environment variables.
      # For info on creating secrets, see:
      # https://docs.semaphoreci.com/essentials/using-secrets/
      secrets:
        - name: dockerhub
      jobs:
      - name: Docker build
        commands:
          # Authenticate with Docker Hub
          - 'echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin'
          - sem-version python 3.8
          - checkout
          - mkdir .pip_cache
          - cache restore
          - pip install --cache-dir .pip_cache -r requirements.txt
          - cache store
          - docker-compose build
          - 'docker pull $DOCKER_USERNAME/zenmon:latest || true'
          - 'docker-compose build --cache-from=$DOCKER_USERNAME/zenmon:latest -t $DOCKER_USERNAME/zenmon:latest .'
          - 'docker push $DOCKER_USERNAME/zenmon:latest'
          - docker images

  - name: Run & Test Docker image
    task:
      # Mount a secret which defines DOCKER_USERNAME and DOCKER_PASSWORD
      # environment variables.
      # For info on creating secrets, see:
      # https://docs.semaphoreci.com/essentials/using-secrets/
      secrets:
        - name: dockerhub
      prologue:
        commands:
          # Authenticate with Docker Hub
          - echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
          - sem-version python 3.8  
          - checkout
          - mkdir .pip_cache
          - cache restore
          - pip install --cache-dir .pip_cache -r requirements.txt
          - cache store
          - docker pull "$DOCKER_USERNAME"/zenmon
          - docker-compose up -d --build
      jobs:
      - name: Check Running Images
        commands:
          - docker ps
      - name: Run Unit test
        commands:
          - cd zenmon
          - python3 manage.py makemigrations
          - python3 manage.py migrate
          - python3 manage.py test dashboard.tests.test_models
          - python3 manage.py test dashboard.tests.test_views
          - python3 manage.py test dashboard.tests.test_forms
      - name: Checklist
        commands:
          - cd zenmon
          - python3 manage.py check --deploy
      - name: Style check
        commands:
          - pip install flake8
          - flake8 zenmon/ --max-line-length=127

settings.py (database section)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

I have already tried these solutions and these solutions.

Any suggestions of how to resolve this issue would be appreciated.

dan118
  • 23
  • 1
  • 3

2 Answers2

0

Try 'localhost' as HOST instead of 'db'. And you should expose postgres' ports in the compose-file, like so:

ports:
 - "5432:5432"
Benni
  • 1
  • When I tried this I received the following error: `django.db.utils.OperationalError: could not connect to server: Connection refused00:01 Is the server running on host "localhost" (127.0.0.1) and accepting00:01 TCP/IP connections on port 5432?'` – dan118 Aug 13 '21 at 16:08
0

Your port mapping in the docker-compose.yml file is not correct.

The PostgreSQL inside the docker container runs on port 5432. But as I can see, you ONLY map the port 8000.

This is what you need to use:

ports:
    - 8000:8000 # HTTP port
    - 5432:5432 # DB port

Syntax:

<port on the host>:<container port>

Because by default the PostgreSQL listen on port 5432 you need to use this port at least on the right side on this expression. The port on the left tells the port that you use to connect to the DB from your localhost.

If this does not help, then

  1. Log in to the PostgreSQL container and try to connect to the db from the comnand line. You can use this basic test, if you have telnet comnand installed in the contaiber: telnet 5432

  2. If it works, then check the opened ports on your host machine.

  3. If the connection from the container does not work, that means your postgres installation is not correct. Fix it.

  4. Maybe you do not need to open the port 5432 in the compose file, because your app runs in the container and connects to the DB from inside the container. In that case I am sure that your DB is not running properly in the Docker container, so you need to fix it first.

If you checked the staff mentioned above and your app still not working rise a new question with extra info.

I hope that this helps you.

zappee
  • 20,148
  • 14
  • 73
  • 129