Postgres isn't accepting TCP/IP on port 5432 - postgresql

I've got this docker-compose.yml file:
version: '3.7'
services:
users:
build:
context: ./services/users
dockerfile: Dockerfile-prod
expose:
- 5000
environment:
- FLASK_ENV=production
- APP_SETTINGS=project.config.ProductionConfig
- DATABASE_URL=postgres://postgres:postgres#users-db:5432/users_prod
- DATABASE_URL_DEPTS=postgres://postgres:postgres#users-db:5432/depts_prod
- DATABASE_TEST_URL=postgres://postgres:postgres#users-db:5432/users_test
- DATABASE_TEST_URL_DEPTS=postgres://postgres:postgres#users-db:5432/depts_test
depends_on:
- users-db
users-db:
build:
context: ./services/users/project/db
dockerfile: Dockerfile
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
nginx:
build:
context: ./services/nginx
dockerfile: Dockerfile-prod
restart: always
ports:
- 80:80
depends_on:
- users
from Postgres I'm getting error saying:
(psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "users-db" (172.19.0.2) and accepting
TCP/IP connections on port 5432?
The port 5432 is defined as to be exposed so why it isn't accepting connections ? When in development I'm using '5436:5432' on Postgres and '5000:5001' on the Flask app and it works just fine.
Dockerfile for the Flask app:
FROM python:3.6.9-slim
RUN apt-get update && \
apt-get install -y netcat && \
apt-get clean
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
COPY . /usr/src/app
CMD ["/usr/src/app/entrypoint-prod.sh"]
Dockerfile for the Postgres:
FROM postgres:11.2-alpine
ADD users/create.sql /docker-entrypoint-initdb.d
entrypoint:
#!/bin/sh
echo 'Waiting for postgres ...'
while ! nc -z users-db 5432; do
sleep 0.1
done
echo 'PostgreSQL started'
gunicorn -b 0.0.0.0:5000 manage:app

Related

could not translate host name "db" to address: Temporary failure in name resolution on distro

I'am starting docker compose running docker on linux
When is starting docker + fastapi + postgresql i have error:
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Temporary failure in name resolution
web_1 |
web_1 | (Background on this error at: https://sqlalche.me/e/14/e3q8)
Connection to database from sqlalchemy:
DATABASE_URL = 'postgresql://postgres:postgres#db:5432/db_ok'
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
Docker-compose.yml:
version: '3.9'
services:
web:
build: .
# command: uvicorn app.main:app --host 0.0.0.0 --port 8000
# volumes:
# - .:/usr/src/db_ok
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres
restart: always
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=db_ok
ports:
- 5432:5432
volumes:
postgres_data:
Dockerfile:
FROM python:3.10
WORKDIR ./app
COPY ./app ./app
COPY ./utils/google.deb ./app/utils
COPY ./utils/chromedriver ./app/utils
COPY req.txt req.txt
RUN pip install --upgrade pip \
&& pip install -r req.txt
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get install -y google-chrome-stable
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
But it works for windows
how i can fix this problem?

fastapi & psycopg2 : password authentication failed for user "root"

I am getting this issue on
`docker-compose up`
This is my configuration files:-
Dockerfile:
FROM python:3.9
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "uvicorn", "app.main:app","--host","0.0.0.0","--port","8000 " ]
docker-compose.yaml:
version: '3'
services:
api:
build: .
depends_on:
- postgres
ports:
- 8000:8000
volumes:
- ./:/usr/src/app:ro
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
# env_file:
# - ./.env
environment:
- DATABASE_HOST_NAME=postgres
- DATABASE_PORT=${DATABASE_PORT}
- DATABASE_NAME=${DATABASE_NAME}
- DATABASE_USERNAME=${DATABASE_USERNAME}
- DATABASE_PASSWORD=${DATABASE_PASSWORD}
- SECRET_KEY=${SECRET_KEY}
- ALGORITHM=${ALGORITHM}
- ACCESS_TOKEN_EXPIRE_MINUTES=${ACCESS_TOKEN_EXPIRE_MINUTES}
postgres:
image: postgres
environment:
- POSTGRES_USER=${DATABASE_USERNAME}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=${DATABASE_NAME}
volumes:
- postgres-db:/var/lib/postgresql/data
volumes:
postgres-db:
When i ran docker-compose build and docker-compose up -d these two commands ran succesfully but when i hit the login endpoint, it's giving the error
fastapi-api-1 | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "postgres" (192.168.112.2), port 5432 failed: FATAL: password authentication failed for user "root"
My DATABASE_USERNAME is root

pg_dump from Celery container differs from pg_dump in other containers

I can't understand where pg_dump version is coming from.
I forced everywhere postrgresql-client-13 to be installed.
/usr/bin/pg_dump --version
Celery Beat and Celery
pg_dump (PostgreSQL) 11.12 (Debian 11.12-0+deb10u1)
Other containers (web & postgre and local machine) :
pg_dump (PostgreSQL) 13.4 (Debian 13.4-1.pgdg100+1)
Here is my Dockerfile
FROM python:3
#testdriven turotial they use an other user than root but seemed to fail ehre .
# create directory for the app user
RUN mkdir -p /home/app
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
WORKDIR $APP_HOME
ENV PYTHONUNBUFFERED 1
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
RUN apt-get update -qq && apt-get install -y \
postgresql-client-13 \
binutils \
libproj-dev \
gdal-bin
RUN apt-get update \
&& apt-get install -yyq netcat
# install psycopg2 dependencies
#install dependencies
RUN pip3 install --no-cache-dir --upgrade pip && pip install --no-cache-dir --no-cache-dir -U pip wheel setuptools
COPY ./requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME
# copy project
COPY . $APP_HOME
# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]
and here is my docker-compose
version: '3.7'
services:
web:
build:
context: ./app
dockerfile: Dockerfile.prod
command: gunicorn core.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/staticfiles
expose:
- 8000
env_file:
- ./app/.env.prod
depends_on:
- db
db:
image: postgis/postgis:13-master
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./app/.env.prod.db
redis:
image: redis:6
celery:
build: ./app
command: celery -A core worker -l info
volumes:
- ./app/:/usr/src/app/
env_file:
- ./app/.env.prod
depends_on:
- redis
celery-beat:
build: ./app
command: celery -A core beat -l info
volumes:
- ./app/:/usr/src/app/
env_file:
- ./app/.env.prod
depends_on:
- redis
nginx-proxy:
image : nginxproxy/nginx-proxy:latest
container_name: nginx-proxy
build: nginx
restart: always
ports:
- 443:443
- 80:80
volumes:
- static_volume:/home/app/web/staticfiles
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
- /var/run/docker.sock:/tmp/docker.sock:ro
depends_on:
- web
nginx-proxy-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
env_file:
- ./app/.env.prod.proxy-companion
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
depends_on:
- nginx-proxy
volumes:
postgres_data:
static_volume:
certs:
html:
vhost:
I really need to have Celery with the same pg_dump version.
Can you guys provide some inputs ?

Unable to communicate with the postgreSQL database using Docker

I don't know why I am not able to fetch data from my postgreSQL database. I always got the same error when I do :
docker logs web I got that :
django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Address not available
I tried a lot of things but without effects...
Here is my docker-compose :
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
- 5432:5432
stdin_open: true
depends_on:
- db
db:
container_name: db
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=mydb
- POSTGRES_HOST=localhost
volumes:
postgres_data:
And this is the 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
RUN apk add zlib-dev jpeg-dev gcc musl-dev
# install nodejs
RUN apk add --update nodejs nodejs-npm
# copy project
ADD . .
# install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
in my settings.py I have this for the database :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'admin',
'PASSWORD': 'pass',
'HOST': 'localhost',
'PORT': '5432',
}
}
Could you help me please ?
I have absolutely no ideas how to solve that, I tried a lot of things but without any effects...
For instance, I tried to change in settings this parameter :
'HOST': 'localhost' to 'HOST':'db'
But I got that errors when I type that docker logs web :
django.db.utils.OperationalError: FATAL: password authentication failed for user "admin"
Thank you very much
have you tried to EXPOSE the port 5432 on the database container?
something like
db:
container_name: db
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=mydb
- POSTGRES_HOST=localhost
expose:
- "5432"
or inside the Dockerfile EXPOSE 5432/tcp
here is a link where you can check out the difference between ports: and expose:
What is the difference between docker-compose ports vs expose

Cannot connect Lumen with PostgreSQL on Docker

I have Lumen with PostgreSQL dockerized with docker-compose. Both works like a charm but I cannot make a migration or insert data because Lumen container is not connected to the PostgreSQL one, it appears the following error:
SQLSTATE[08006] [7] could not connect to server: Connection refused.Is
the server running on host "postgresql" (172.18.0.3) and accepting
TCP/IP connections on port 9906?
Dockerfile
FROM php:7-apache
RUN apt-get upgrade -y \
&& apt-get update -y \
&& apt-get install git-core libzip-dev openssl unzip zip libpq-dev postgresql-client -y \
&& apt-get clean \
&& docker-php-ext-install mbstring pdo pdo_pgsql pgsql
# Apache Config
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN a2enmod rewrite
# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
EXPOSE 8100
docker-compose.yml
version: '3.5'
services:
app:
build: .
container_name: form_app
depends_on:
- postgresql
ports:
- "8100:80"
volumes:
- .:/var/www/html/
stdin_open: true
tty: true
postgresql:
image: postgres
restart: always
container_name: form_pgsql
environment:
POSTGRES_USER: formularios
POSTGRES_PASSWORD: formularios
POSTGRES_DB: formularios
ports:
- "9906:5432"
volumes:
- pgdata:/var/lib/postgresql/data
admin:
image: adminer
container_name: form_adminer
ports:
- "8880:8080"
volumes:
pgdata:
.env
APP_NAME=Lumen
APP_ENV=local
APP_KEY=feZh5izBbOLIek27RQip9ciwTdRHPV7R
APP_DEBUG=true
APP_URL=http://localhost:8100
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=pgsql
DB_HOST=postgresql
DB_PORT=9906
DB_DATABASE=formularios
DB_USERNAME=formularios
DB_PASSWORD=formularios
CACHE_DRIVER=file
QUEUE_CONNECTION=sync