2

I am new to DevOps and I am trying to Dockerize my Django-Mysql connection.My Docker file looks like this:-

FROM python:3.6
ENV PYTHONUNBUFFERED 1

RUN mkdir /docker_dir

WORKDIR /docker_dir

ADD . /docker_dir/
RUN pip install -r requirements.txt

My docker-compose.yml file looks like this:-

version: '3'

services:
  db:
    image: mysql
    restart: always
    command: --default-authentication-plugin=mysql_native_password --mysqlx=0
    environment:
      - MYSQL_HOST=127.0.0.1
      - MYSQL_PORT=3306  # cannot change this port to other number
      - MYSQL_DATABASE=cgi_assignments # name you want for the database
      - MYSQL_USER="root" # change to whatever username you want
      - MYSQL_PASSWORD="root" #change to the password you want for user
      - MYSQL_ROOT_PASSWORD="root" #change to good root password
    ports:
      - "3307:3306"
    volumes:
      - .setup.sql:/docker-entrypoint-initbd.d/setup.sql
  
  web:
    build: .
      # command: python manage.py runserver 0.0.0.0:8000
    command: python manage.py runserver 0.0.0.0:8000
    container_name: docker_dir
    volumes:
      - .:/docker_dir
    ports:
      - "8000:8000"
    depends_on:
      - db
    links: 
      - db 

and my database on Django settings file looks like this:-

DATABASES = {
    'default': {
        'NAME': 'cgi_assignments',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'db',
        'PORT': 3306,
    },

}

If I use ports:-"3307:3306" it gives me the following error:-

django.db.utils.OperationalError: (1130, "Host '172.18.0.3' is not allowed to connect to this MySQL server")

and I use ports:-"3306:3306" it gives me the following error:-

 listen tcp 0.0.0.0:3306: bind: address already in use

6
  • 127.0.0.1 is not going to work in container to container networking, use 0.0.0.0. Commented Nov 26, 2020 at 9:32
  • I am using this now @KlausD. MYSQL_HOST=0.0.0.0 still same error django.db.utils.OperationalError: (1130, "Host '172.18.0.3' is not allowed to connect to this MySQL server") Commented Nov 26, 2020 at 9:39
  • I read somewhere by default root user is not accessible from outsite ( localhost only ). You should try to change the user below into non root username - MYSQL_USER="root" # change to whatever username you want. and dont forget to change your django configuration to match this User Commented Nov 26, 2020 at 10:49
  • @SKos I tried adding a new non-root user and then using them on my docker and Django setting files didn't work Commented Nov 26, 2020 at 11:05
  • Did you still got the same error message ? Commented Nov 26, 2020 at 11:26

1 Answer 1

1

Removing the volumes configuration worked for me.

Sign up to request clarification or add additional context in comments.

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.