2

I have a Django application that uses Python 2.7.10 and Django 1.10.3. My Dockerfile contains the following:

   FROM python:2.7.10
   ENV PYTHONUNBUFFERED 1
   RUN mkdir /code
   WORKDIR /code
   ADD requirements.txt /code/
   RUN pip install -r requirements.txt
   ADD . /code/ 

My requirements.txt contains the following:

   Django>=1.10,<2.0
   Mysql-python

My docker-compose.yml contains the following:

   version: '2'

   services:
     localhost:
       image: mysql
       restart: always
       environment:
         MYSQL_DATABASE: 'test'
         MYSQL_USER: 'test'
         MYSQL_PASSWORD: 'password'
     web:
       build: .
       command: python manage.py runserver 0.0.0.0:8000
       volumes:
         - .:/code
       ports:
         - "8000:8000"
       depends_on:
         - localhost

settings.py contains the following for the database:

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

I first do a "% docker-compose build", then I do a "% docker-compose up" and get the following error:

web_1 | django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

I've looked and tried the solutions available on Stackoverflow with no luck. Any help would be much appreciated.

Foobar
  • 843
  • 1
  • 10
  • 23
  • what is in your settings.py? – jozo Jul 23 '18 at 17:52
  • You need to link mysql container with django containers: https://stackoverflow.com/questions/31035887/linking-django-and-mysql-containers-using-docker-compose – Adelina Jul 23 '18 at 17:53
  • MySQL is not running in a container though. Does MySQL also have to dockerized to be used with a dockerized Django app? – Foobar Jul 23 '18 at 18:28
  • I've added the snippet for database settings from settings.py. – Foobar Jul 23 '18 at 18:36

1 Answers1

2

Any help would be much appreciated.

In regards to MySql and Django App relation you have three possible cases:

  • MySql is residing on same container as Django App (localhost)
  • MySql is residing on separate container from Django App (hostname is different, can be handled on different topology - same subnet or different etc)
  • MySql is residing outside of docker ecosystem (bare metal, cloud, whatever...)

Judging from your settings.py and docker-compose.yml you are mixing first two approaches. Now, since you can reference external MySql server from settings.py I'll consider that case trivial, next, cramming MySql on same container as Django App is usually bad idea (if you ever want to scale them separately) so this answer will focus on second case (MySql in separate container) and, for simplicity, on same docker-compose file (you can separate them as well).

Important excerpt from docker-compose.yml is:

version: '2'

services:

  # The Database
  database:
    image: mysql:5.6
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=test_db"
      - "MYSQL_USER=test_user"
      - "MYSQL_PASSWORD=some_password"
      - "MYSQL_ROOT_PASSWORD=some_root_password"
    ports:
      - "33061:3306"
  container_name: "mysql_database"

  # Django App (taken from your example, added env vars as hint)
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - database

volumes:
  dbdata:

With such docker-compose you would have following setting.py config (note that HOST is taking value of docker-compose service entry for database container, port is internal to container not external to docker-host):

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test_db',
    'USER': 'test_user',
    'PASSWORD': 'some_password',
    'HOST': 'database',
    'PORT': '3306',
}

As a sidenote, we usually run database with separate docker-compose.yml from Django App file since in usual dev cycle it is far more frequent to tweak/restart/crash something on Django App container than on database one and we can handle it separately that way, plus such a separately defined database can handle several different DjangoApps each with its own container.

Const
  • 6,237
  • 3
  • 22
  • 27