Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

docker - Postgres database, Error : NetworkError when attempting to fetch resource

I am trying to do a Docker image but I have some problems. Here is my docker-compose.yml :

version: '3.7'

services:
  web:
    container_name: web
    build:
      context: .
      dockerfile: Dockerfile
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/usr/src/web/
    ports:
      - 8000:8000
      - 3000:3000
      - 35729:35729
    stdin_open: true
    depends_on:
      - db

  db:
    restart: always
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASS=pass
      - POSTGRES_DB=mydb
      - POSTGRES_PORT=5432
      - POSTGRES_HOST=localhost
      - POSTGRES_HOST_AUTH_METHOD=trust
    container_name: db
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/

volumes:
  postgres_data:

And there my Dockerfile :

# pull official base image
FROM python:3.8.3-alpine

# set work directory
WORKDIR /usr/src/web

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update 
    && apk add postgresql-dev gcc python3-dev musl-dev

# install nodejs
RUN apk add --update nodejs nodejs-npm
RUN apk add zlib-dev jpeg-dev gcc musl-dev
# copy project
COPY . .

RUN python -m pip install -U --force-reinstall pip
RUN python -m pip install Pillow
# install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN pip install Pillow

# run entrypoint.sh
ENTRYPOINT ["sh", "./entrypoint.sh"]

Anf finally my entrypoint.sh :

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

exec "$@"

When I do that :

docker-compose up -d --build

It works perfectly. Then I type that :

docker-compose exec web npm start --prefix ./front/

It looks ok but when I type in my browser http://localhost:3000/

I got that kind of messages : Error : NetworkError when attempting to fetch resource.

I thought the front is ok but I am not able to communicate with the back and so the database.

Could you help me please ?

Thank you very much !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As I can see in docker-compose.yml file you did not define the environment variables for Postgres in the web container. Please define the environment variables for the below :

DATABASE

SQL_HOST

SQL_PORT

Then bring down the docker and bring up the docker hopefully it will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...