Monday, 2 May 2022

Docker compose django and mysql how to connect them in ubuntu



Check ubuntu firewall(ufw)

https://www.cyberciti.biz/faq/how-to-open-firewall-port-on-ubuntu-linux-12-04-14-04-lts/

https://askubuntu.com/questions/1121172/what-happens-if-ufw-firewall-is-inactive

sudo ufw status

# if uwf is inactive all traffice goes through


# Mysql docker-compose.yml set up

 https://stackoverflow.com/questions/47979270/django-cannot-connect-mysql-in-docker-compose

db:

ports -

    -3302:3306


this means your machine port 3302 will be mapped to docker bridge container private network 3306 mysql


if you connect to this container docker-compose exec db /bin/bash

ysql -h 127.0.0.1 -P 3302 -u root -p # will map to 3306 default docker port


# Since Django is in docker private network, port must be docker mysql port 3306, and host must be the service name db

django settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'belter',
        'HOST': 'db',  #<---
        'PORT': '3306',   #<---
    }
}

No comments:

Post a Comment