title: Connecting Django docker to remote database
body: <p>I have a Django application running on docker connected to a database in another container on the same host. This seams to work fine, but when I try to change the connection to a database on another server, it fails to connect. Not only that, but when I try to connect to the Django application in the browser(like admin or api) I get no response, and see no activity in the output log. The application runs fine with the remote database if I run it outside the container, and the database is set to accept all IPs for the user I am trying to connect with.</p>
  <p>Any Ideas as to why I am not getting a connection?</p>
  <p>Dockerfile:</p>
  <pre><code>FROM python:3.8-alpine
  
  ENV PATH=&quot;/scripts:${PATH}&quot;
  
  COPY ./requirements.txt /requirements.txt
  RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
  RUN apk add mariadb-dev python3-dev postgresql-dev
  RUN pip install -r /requirements.txt
  RUN apk del .tmp
  
  RUN mkdir /app_django
  COPY ./app_django /app_django
  WORKDIR /app_django
  COPY ./scripts /scripts
  
  RUN chmod +x /scripts/*
  
  RUN mkdir -p /vol/web/media
  RUN mkdir -p /vol/web/static
  RUN adduser -D user
  RUN chown -R user:user /vol
  RUN chmod -R 755 /vol/web
  USER user
  
  CMD [&quot;entrypoint.sh&quot;]
  </code></pre>
  <p>docker-compose.yml</p>
  <pre><code>version: '3.7'
  
  services:
    db:
      build: mysql/
      environment:
        MYSQL_ROOT_PASSWORD: 'password'
        MYSQL_DATABASE: database
      volumes:
        - ./mysql/data:/var/lib/mysql
        - ./mysql/conf:/etc/mysql/conf.d
        - ./mysql/entrypoint-initdb.d:/docker-entrypoint-initdb.d/
        - ./mysql/backup_database:/var/local/mysql/backups
      ports:
        - 3306:3306
      restart: always
      
    pdb:
      build:
        context: .
      ports:
        - &quot;8000:8000&quot;
      volumes:
        - ./pdb_django:/pdb_django
      command: sh -c &quot;python manage.py runserver 0.0.0.0:8000&quot;
      environment:
        - DEBUG=1
  </code></pre>
  <p>entrypoint.sh</p>
  <pre><code>#!/bin/sh
  
  set -e
  
  python manage.py collectstatic --noinput
  
  uwsgi --socket :8000 --master --enable-threads --module app.wsgi
  </code></pre>
  <p>Django db settings (the commented parts is used when conection to db on host container):</p>
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'database',
          'HOST': '&lt;remote IP&gt;',
          # 'HOST': 'db',
          'USER': 'user',
          'PASSWORD': 'password',
          'PORT': '3308'
          # 'PORT': '3306',
      }
  }
  </code></pre>
  <p>requirements.txt</p>
  <pre><code>Django==3.1.4
  uWSGI&gt;=2.0.18,&lt;2.1
  django-filter==2.4.0
  djangorestframework==3.12.2
  mysql-connector-python==8.0.22
  mysqlclient==2.0.2
  PyMySQL==0.10.1
  sqlparse==0.4.1
  </code></pre>
  <p>Outputlog for application:</p>
  <pre><code>pdb_1  | Watching for file changes with StatReloader
  pdb_1  | Exception in thread django-main-thread:
  pdb_1  | Traceback (most recent call last):
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
  pdb_1  |     self.connect()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
  pdb_1  |     return func(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 200, in connect
  pdb_1  |     self.connection = self.get_new_connection(conn_params)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
  pdb_1  |     return func(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/mysql/base.py&quot;, line 234, in get_new_connection
  pdb_1  |     return Database.connect(**conn_params)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py&quot;, line 130, in Connect
  pdb_1  |     return Connection(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py&quot;, line 185, in __init__
  pdb_1  |     super().__init__(*args, **kwargs2)
  pdb_1  | MySQLdb._exceptions.OperationalError: (2002, &quot;Can't connect to MySQL server on '&lt;remote IP&gt;' (115)&quot;)
  pdb_1  |
  pdb_1  | The above exception was the direct cause of the following exception:
  pdb_1  |
  pdb_1  | Traceback (most recent call last):
  pdb_1  |   File &quot;/usr/local/lib/python3.8/threading.py&quot;, line 932, in _bootstrap_inner
  pdb_1  |     self.run()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/threading.py&quot;, line 870, in run
  pdb_1  |     self._target(*self._args, **self._kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py&quot;, line 53, in wrapper
  pdb_1  |     fn(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py&quot;, line 121, in inner_run
  pdb_1  |     self.check_migrations()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/core/management/base.py&quot;, line 459, in check_migrations
  pdb_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py&quot;, line 18, in __init__
  pdb_1  |     self.loader = MigrationLoader(self.connection)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py&quot;, line 53, in __init__
  pdb_1  |     self.build_graph()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py&quot;, line 216, in build_graph
  pdb_1  |     self.applied_migrations = recorder.applied_migrations()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py&quot;, line 77, in applied_migrations
  pdb_1  |     if self.has_table():
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py&quot;, line 55, in has_table
  pdb_1  |     with self.connection.cursor() as cursor:
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
  pdb_1  |     return func(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 259, in cursor
  pdb_1  |     return self._cursor()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 235, in _cursor
  pdb_1  |     self.ensure_connection()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
  pdb_1  |     return func(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
  pdb_1  |     self.connect()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/utils.py&quot;, line 90, in __exit__
  pdb_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 219, in ensure_connection
  pdb_1  |     self.connect()
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
  pdb_1  |     return func(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py&quot;, line 200, in connect
  pdb_1  |     self.connection = self.get_new_connection(conn_params)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
  pdb_1  |     return func(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/django/db/backends/mysql/base.py&quot;, line 234, in get_new_connection
  pdb_1  |     return Database.connect(**conn_params)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py&quot;, line 130, in Connect
  pdb_1  |     return Connection(*args, **kwargs)
  pdb_1  |   File &quot;/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py&quot;, line 185, in __init__
  pdb_1  |     super().__init__(*args, **kwargs2)
  pdb_1  | django.db.utils.OperationalError: (2002, &quot;Can't connect to MySQL server on '&lt;remote IP&gt;' (115)&quot;)
  </code></pre>