-
Notifications
You must be signed in to change notification settings - Fork 5
Docker and PostgreSQL: port 5432 failed: Connection refused error #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I made some changes to correct a few issues with the docker configuration. but still getting some issue. Latest Error: nginx | 2024/07/31 12:58:41 [notice] 1#1: start worker process 43 Latest docker-compose: ` db:
nginx: networks: ` latest dockerfile: ` ENV PYTHONDONTWRITEBYTECODE 1 RUN apt-get update && apt-get install -y postgresql-client COPY requirements.txt . RUN pip install --upgrade pip COPY . . RUN python manage.py collectstatic --no-input EXPOSE 5005 ` database settings and env variables are as expected I'm new to using Docker for development so I might be missing something. Help me figure out what i'm doing wrong and provide documentation on how to configure postgres for this project. Thanks. |
Hello @Edmaximus Just to summarize, you wan to use a Dockerized version of the product with PgSQL [ also in Docker ]. Please confirm if is this your intention. TY! |
Yes. I need to switch from using sqlLite to PgSQL. I currently have that working fine in my local without Docker but I need to make sure I can dockerize the project without any issue. |
I was able to sort out this issue. It was due to port conflict and a few other settings I had wrong updating docker-compose.yml to: ` db:
nginx: networks: ` IS_DOCKER = str2bool(os.getenv('IS_DOCKER', 'False')) DATABASES = { Remove the migrate command from the Dockerfile since that process is now being done thru the docker-compose build process after the db is up. And set the variables in the .env file accordingly. With those changes, I'm able to run the environment on docker (and on my local dev) without issue. |
I'm trying to switch SQLite to PostgreSQL and keep getting connection error when I try to build the docker container to start the app. Can you help me figure out what I'm doing wrong. Below is the error and relevant code.
`=> ERROR [appseed-app 9/9] RUN python manage.py migrate 1.4s
My docker-compose looks like:
`version: "3.8"
services:
appseed-app:
container_name: appseed-app
restart: always
build:
context: .
networks:
- db_network
- web_network
environment:
DATABASE_URL: postgresql://postgres:password@postgres:5432/dbname
depends_on:
postgres:
condition: service_healthy
nginx:
container_name: nginx
restart: always
image: "nginx:latest"
ports:
- "5085:5085"
volumes:
- ./nginx:/etc/nginx/conf.d
networks:
- web_network
depends_on:
- appseed-app
redis:
image: redis:7.0.12
container_name: redis
command: ["redis-server", "--port", "6379", "--slave-read-only", "no"]
restart: always
ports:
- 6379:6379
networks:
- db_network
celery:
container_name: celery
restart: always
build:
context: .
networks:
- db_network
environment:
DJANGO_SETTINGS_MODULE: "core.settings"
command: "celery -A apps.tasks worker -l info -B"
depends_on:
- appseed-app
- redis
postgres:
container_name: postgres
image: postgres:13
environment:
POSTGRES_DB: dbname
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
expose:
- 5432
networks:
- db_network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
networks:
db_network:
driver: bridge
web_network:
driver: bridge
volumes:
postgres_data:
`
Dockerfile
`FROM python:3.10.4
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install -y postgresql-client
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python manage.py collectstatic --no-input
RUN python manage.py makemigrations
RUN python manage.py migrate
EXPOSE 5005
CMD ["gunicorn", "--config", "gunicorn-cfg.py", "core.wsgi"]
`
database settings in settings.py
`DB_ENGINE = os.getenv('DB_ENGINE' , None)
DB_USERNAME = os.getenv('DB_USERNAME' , None)
DB_PASS = os.getenv('DB_PASS' , None)
DB_HOST = os.getenv('DB_HOST' , None)
DB_PORT = os.getenv('DB_PORT' , None)
DB_NAME = os.getenv('DB_NAME' , None)
if DB_ENGINE and DB_NAME and DB_USERNAME:
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.' + DB_ENGINE,
'NAME' : DB_NAME,
'USER' : DB_USERNAME,
'PASSWORD': DB_PASS,
'HOST' : DB_HOST,
'PORT' : DB_PORT,
},
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite3',
}
}`
And finally the variables in .env:
DB_ENGINE=postgresql
DB_NAME=dbname
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASS='password'
The text was updated successfully, but these errors were encountered: