2

Hi i want to connect django - mysql in each container :)

i tried a lot of way but failed..

here is my mysql docker-compose.yaml

version: "3"
services:
  db:
    platform: linux/x86_64
    image: mysql:5.7
    container_name: good-mysql
    ports:
      - "3307:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=test
      - MYSQL_DATABASE=testdb
    command: 
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
    volumes:
      - /Users/Shared/data/mysql-test:/var/lib/mysql
    network_mode: bridge

i used mysql official image

and my django Dockerfile

FROM ubuntu:18.04
FROM python:3.8.6-buster

ENV PYTHONUNBUFFERED 1

# General Packages
RUN apt-get update \
    && apt-get install -y libmariadb-dev \
    && apt-get install -y python3-pip \
    && apt-get install -y build-essential \
    && apt-get install -y libcurl4-openssl-dev \
    && apt-get install -y libssl-dev \
    && pip install --upgrade pip

RUN mkdir -p /project/myproject
COPY ./myproject /project/myproject

RUN mkdir -p /app
    WORKDIR /app

RUN pip install -r requirements.txt

CMD tail -f /dev/null

and docker-compose.yaml

version: '3.8'
services:
    deepnatural-ai-api:
        build: .
        image: deepnatural-ai-api:latest
        container_name: deepnatural-ai-api
        ports:
            - 1000:1000
        external_links:
            - "good-mysql:db"
        network_mode: bridge

and i run docker-compose up -d each container

and access mysql container, run mysql -uroot -p its work!

but when i access django container run migrations its raise error

"Unknown MySQL server host 'db' (-2)")

my django settings.py is

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    '...',
]

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'testdb',
    'USER': 'root',
    'PASSWORD': 'test',
    'HOST': 'db',
    'PORT': '3306',
    'OPTIONS': {'auth_plugin': 'mysql_native_password'},
    }
}

ROOT_URLCONF = 'myproject.urls'

DEFAULT_AUTO_FIELD='django.db.models.AutoField'

STATIC_URL = '/static/'

INTERNAL_IPS = [
    '127.0.0.1',
    '0.0.0.0',
]

i have double check docker network, db host name, password, db name etc...

but django can't find db host "good-mysql"

please some advice thank you :)

EDIT:

when i run docker ps

enter image description here

and I tried change HOST: "good-mysql"

enter image description here

3 Answers 3

1
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>
Sign up to request clarification or add additional context in comments.

Comments

1

Try combining your docker-compose files:

version: '3.8'

services:
    deepnatural-ai-api:
        build: .
        image: deepnatural-ai-api:latest
        container_name: deepnatural-ai-api
        ports:
            - 1000:1000
        depends_on:
            - db
    db:
        platform: linux/x86_64
        image: mysql:5.7
        ports:
            - "3307:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=test
            - MYSQL_DATABASE=testdb
        command: 
            - --character-set-server=utf8mb4
            - --collation-server=utf8mb4_unicode_ci
        volumes:
            - mysql_test:/var/lib/mysql/data

volumes:
    mysql_test:

Try running docker-compose up -d --build now. Both containers should spin up, with the db container starting up first, because deepnatural-ai-api has it as a dependency.

If you do not want to combine the docker-compose files. Please list out the running containers after you start them both up using docker container ls.

6 Comments

i add when i docker container ls thank you :)
Did you try changing the HOST setting in Django settings to good-mysql instead of db?
yeap i tried already but still failed :(
It seems to be a networking issue between the containers. Try removing the ports configuration in your db service?
Try rebuilding your image before testing. docker-compose down & then docker-compose up -d --build
|
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.