Can't connect to MySQL while building docker-compose image - python

I have a configuration of docker-compose, and on building database step, django management throws an error:
django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
I think - the docker refusing connections.
Configuration by socket similar not working (Says error connection by socket)
Docker Compose file
version: '3'
services:
db:
image: mariadb:5.5
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "bh"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
ports:
- "3302:3306"
behealthy_dev:
build: .
container_name: behealthy_dev
expose:
- "86"
command: python manage.py runserver 0.0.0.0:80
volumes:
- /behealthy_dev/
ports:
- "86:86"
depends_on:
- db
Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /behealthy_dev
WORKDIR /behealthy_dev
ADD requirements.txt /behealthy_dev/
RUN pip install -r requirements.txt
ADD . /behealthy_dev/
RUN python manage.py makemigrations
RUN python manage.py migrate
RUN python manage.py collectstatic --noinput
Database settings (settings)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bh',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'root',
'OPTIONS': {
'sql_mode': 'traditional',
'init_command': 'SET innodb_strict_mode=1',
'charset': 'utf8mb4',
},
},
}
Have any solutions? I tried to resolve from other stack answer but it's not working for me.

in your service behealthy_dev change the MySQL host from
'HOST': '127.0.0.1'
to
'HOST': 'db',
Edit:
Change also the MYSQL_ROOT_PASSWORD: "root" to:
`MYSQL_ROOT_PASSWORD: "rootpassword"`
and your Database settings to:
'PASSWORD': 'rootpassword',

Related

Postgres+Docker+Django OperationalError

I've got an issue with connecting PostgreSQL with Django using Docker.
Error: web_1 | django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"
If I don't use Docker (just runserver command in prompt) it looks as it should, no errors. In another project Docker works correctly with this config.
My docker-compose.yml:
version: '3.8'
services:
web:
build: .
command: python bookstore/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
links:
- db:db
db:
image: postgres:14
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD= postgres
- POSTGRES_DB= postgres
My settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}

docker-compose Error... [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started

docker-compose.yml
version: "3.7"
services:
db:
platform: linux/x86_64
image: mysql:5.7
volumes:
- ./db_data1:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
MYSQL_DATABASE: "${DB_DATABASE}"
MYSQL_USER: "${DB_USER}"
MYSQL_PASSWORD: "${DB_ROOT_PASSWORD}"
ports:
- 3306:3306
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
web:
build:
dockerfile: ./Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- ./web_data1:/app
ports:
- 8000:8000
environment:
DJANGO_DB_HOST: db:3306
DJANGO_DB_NAME: "${DB_DATABASE}"
DJANGO_DB_USER: "${DB_USER}"
DJANGO_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
my_setting.py
DATABASES = {
'default' : {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': "${DB_ROOT_PASSWORD}",
'HOST': 'db',
'PORT': '3306',
}
}
SECRET = 'django-insecure-#kb%p45em8hdhja^+2jal#(*mzw1c3jk5gvsx(_cn#q^u#u&b0'
ALGORITHM = 'HS256'
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "docker_train.wsgi:application"]
.env
DB_ROOT_PASSWORD=password
DB_DATABASE=test
DB_USER=root
I run docker-compose up but got this error.
How can I approach it to solve the problem?
error
docker-training11-db-1 | 2021-11-09 05:14:47+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
docker-training11-web-1 | Watching for file changes with StatReloader
docker-training11-web-1 | Performing system checks...
As I am able to identify, you are facing some issue with your DATABASE details. Kindly verify them first.
django is not able to resolve db host name.
instead of 'db' try passing private ip of base machine.

django OperationalError when trying to migrate with docker

django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
That is the error I get when running docker-compose exec web python manage.py migrate
my docker-compose.yml contains:
version: '3.8'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
This Is what I put for DATABASE in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
I have tried running docker-compose up -d --build and then docker-compose exec web python manage.py migrate
But that doesn't work.
You will need to add an environment for your db service:
services:
# ...
db:
# ...
environment:
POSTGRES_PASSWORD: postgres
as POSTGRES_PASSWORD is the only non-optional env variable needed to run the image as stated in the docs.

Wrong configuration for database in seetings.py or docker

I'm learning how to work with Compose + Django using this manual https://docs.docker.com/compose/django/
Here are my configuration files
docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
When I run using docker-compose up everything is fine.
But when I run using python manage.py runserver I got this error
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
Guess, I have wrong cofiguration
You forgot set hostname in db service.
version: '3'
services:
db:
image: postgres
hostname: db
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Tip: Don't use runserver when you deploy production server. Check this doc
The solution is described in comments
Edit)
separate settings example. Make your original settings.py to base.py in settings folder.
<your_app>/settings/local.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': 5432,
}
}
<your_app>/settings/deploy.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
So finally, your settings folder contains three python file. base.py, local.py, deploy.py
And when you run server with local settings, python manage.py runserver --settings <your_app>.settings.local.
This is a common problem in docker/django setup , postgres container is initialized but the engine is not ready yet.
You must wait for the postgresql server to start before running the web container.
You can do this in several ways , have a look at dockerize
dockerize -timeout=20s -wait ${POSTGRES_PORT}
I guess you trying to run manage.py runserver on host machine. Error means, that you have no access to host 'db' from it (try ping db in console). There are several causes of this:
Your docker container db is down. Try run docker-compose up -d db
You are using Windows as host system. In Windows there is no access to containers from host, you must install postgres locally.
You have networking problems. Try ping 127.0.0.11 (it's docker's DNS server)
P.S. Do you really want local Django use Postgres in container?

Docker Postgres + Django: Pytest fails

Since I moved to Docker Postgres, my tests via pytest fail. I now identified as the problem my database connection.
How it works:
When I use PostgreSQL 10 on my Mac it works smoothly. As database credentials in my settings.py I use:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'project',
'USER': 'MyName',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
How it fails: Since I moved to Docker + Postgres, I have to use different credentials to connect to my Docker Postgres:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
As I want to use Docker rather than PostgreSQL, but still want to be able to test, I wonder if you have any solution for my problem?
Here my console log output: https://pastebin.com/MZsccuGJ
My Dockerfile
# Pull base image
FROM python:3
# Set environment varibles
ENV PYTHONUNBUFFERED 1
# Set work directory
RUN mkdir /code
WORKDIR /code
# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev
# Define ENTRYPOINT
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
# Copy project
COPY . /code/
Docker-Compose
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
container_name: project

Categories

Resources