Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Is the server running on host localhost error

I am new to Docker and I am trying to dockerize an application for the first time by following a variety of tutorials on the internet and I'm getting this error that I am unable to resolve.

I searched around, tried different things but have not found something that would help me resolve the problem.

ERROR MESSAGE : Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

Here is my docker-compose file. The error comes from jobsite.

jobsite:
    container_name: job_startup
    build: .
    # command: ["./docker_compose/django/wait_for_postgres.sh"]
    volumes:
      - .:/code
    environment:
      PRODUCTION: 'False'
    ports:
      - "8000:8000"
    # depends_on:
    #   - "db"
    # links:
    #   - "db"

  enginx:
    build: ./nginx
    container_name: job_nginx
    restart: always
    environment:
      - NGINX_PORT=80
    ports:
      - "80:80"
   depends_on:
      - "jobsite"

Here is the main Dockerfile:

    FROM python:3
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

RUN mkdir /code
WORKDIR /code

COPY requirements.txt /code/
RUN pip install -r requirements.txt

COPY . /code/

EXPOSE 8000

CMD python manage.py runserver 0.0.0.0:8000

The screenshot of the error message:

Error message screenshot

UPDATE

db:
    # image: postgres:latest
    build: ./database
    container_name: postgres_database
    # ports:
    #   - "8000:5432"
    restart: always
    environment:
      - DOCKER=True
      - POSTGRES_USER=postgres
      - POSTGRES_DB=jobs_data1
      - POSTGRES_PASSWORD=''
      - POSTGRES_HOST=127.0.0.1
      - DB_USER=john
      - DB_PASSWORD=''
      - DB_DATABASE=jobs_data1
    volumes:
      - ./postgres_database:/docker-entrypoint-initdb.d/
      - postgres_data:/var/lib/postgresql/data/
like image 467
K. John Avatar asked Nov 05 '18 13:11

K. John


People also ask

How do I host a docker internal to localhost?

Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.

Does Docker expose default ports?

Published portsBy default, when you create or run a container using docker create or docker run , it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag.


2 Answers

Your application tries to connect to PostgreSQL running on localhost. PostgreSQL, though, is obviously not running on localhost. You'll have to add a container to your docker compose configuration that starts PostgreSQL. Then, configure your Python application to use that name instead of localhost.

like image 94
Makkes Avatar answered Sep 20 '22 03:09

Makkes


The application job_startup tries to connect to a postgres database running on localhost.

Your docker-compose file is missing the db service. Also the connection string in your python script should be db as hostname, as described by your commented out links and depends_on lines.

like image 35
Patrick Avatar answered Sep 20 '22 03:09

Patrick