i solved my problem
i used IPv4Address of mysql container like 172.17.0.3
i ran command docker network inspect bridge and check IPv4Address
and insert my settings.py like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdb',
'USER': 'root',
'PASSWORD': 'test',
'HOST': '172.17.0.3', # <---- mysql container IPv4Address
'PORT': '3306',
'OPTIONS': {'auth_plugin': 'mysql_native_password'},
}
}
and its work!
Answer from Jade Han on Stack Overflowhow can i connect django container to mysql container - Stack Overflow
python - Dockerize a Django app with a MySQL container - Stack Overflow
Linking django and mysql containers using docker-compose - Stack Overflow
How to connect dockerized Django app to Mysql database(phpmyadmin) on the host machine.
Videos
i solved my problem
i used IPv4Address of mysql container like 172.17.0.3
i ran command docker network inspect bridge and check IPv4Address
and insert my settings.py like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdb',
'USER': 'root',
'PASSWORD': 'test',
'HOST': '172.17.0.3', # <---- mysql container IPv4Address
'PORT': '3306',
'OPTIONS': {'auth_plugin': 'mysql_native_password'},
}
}
and its work!
pip install dj-database-url
settings.py
import dj_database_url
DATABASES = {"default": dj_database_url.config()}
and in your django container's config you need to add:
environment:
- DATABASE_URL=mysql://<username>:<password>@<db_container_name>/<db_name>
Hi i think this answer helps you ##1.- Reset all your migrations
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
##2.- See and apply your migrations again
python manage.py showmigrations
python manage.py makemigrations
python manage.py migrate
You should not reset your migrations unless you want to wipe all of the data completely and start over. Your migrations should exist beforehand. So if you dont mind about old migrations you can delete them and use python manage.py makemigrations and then proceed to following steps:
So if your applications starts at the moment, we need to update your docker-compose file in a way that it uses entrypoint.sh. An ENTRYPOINT allows you to configure a container that will run as an executable.
First things first, create your entrypoint.sh file on the same level as docker-compose.yaml.
Next, don't forget to add chmod +x entrypoint.sh so entrypoint can be executed
entrypoint.sh file:
#!/bin/bash
set -e
echo "${0}: running migrations."
python manage.py migrate --noinput
echo "${0}: collecting statics."
python manage.py collectstatic --noinput
python manage.py runserver 0.0.0.0:8000
Afterwards update your docker-compose.yaml file. Change your command line to:
command:
- /bin/sh
- '-c'
- '/code/entrypoint.sh'
Additionally you should store all of your pip requirements in requirements.txt file and in your Dockerfile you should run pip install -r requirements.txt
You can dump your pip requirements with a command pip freeze > requirements.txt
In Django settings.py file make sure you have something like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django1',
'USER': 'django',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': 3306,
}
}
then in your docker-compose.yml file make sure you have something along the lines of:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: docker
MYSQL_DATABASE: docker
MYSQL_USER: docker
MYSQL_PASSWORD: docker
then as per the docker/django tutorial you are following run the following again to rebuild everything and things should start working
docker-compose run web django-admin.py startproject composeexample .
In response to a further question, the mysql root password variable is required by docker when creating new databases.
EDIT: added run to docker-compose above; see edit comment
you don't need to worry about environment variable. When linking containers together you just use the container alias defined by the link as if it was the hostname.
for instance if your docker-compose.yml file were:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db:mydb
In your django settings you would have to set the database host to mydb.
Hi, The title says it all. I have installed phpmyadmin which primarily use for managing databases. I have created a django app docker which trying to connect to the database on my machine it is showing this error,
django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
Here is my Dockerfile
# Dockerfile FROM python:3.8
install nginx
RUN apt-get update && apt-get install nginx vim -y --no-install-recommends COPY nginx.default /etc/nginx/sites-available/default RUN ln -sf /dev/stdout /var/log/nginx/access.log
&& ln -sf /dev/stderr /var/log/nginx/error.log
The enviroment variable ensures that the python output is set straight
to the terminal with out buffering it first
ENV PYTHONUNBUFFERED 1
create root directory for our project in the container
RUN mkdir /opt/app
set working dir
WORKDIR /opt/app
ENV PORT=8080
EXPOSE 8080
Copy the project files to working dir
COPY . /opt/app
install dependencies, you can change it to production.txt to deploy on the production env
RUN pip install -r requirements/development.txt
CMD ["python", "manage.py", "runserver"]
This is my database config
DATABASES = {"default": { 'ENGINE': 'django.db.backends.mysql', 'NAME': env.str('DB_DATABASE'), 'HOST': env.str('DB_HOST'), 'USER': env.str('DB_USERNAME'), 'PASSWORD': env.str('DB_PASSWORD') } } DATABASES["default"]["ATOMIC_REQUESTS"] = True
Any help would be appreciated....