1

I am attempting to create a dev environment for my company's app using docker. For some reason I am able to connect to the database if I use docker run web "bash" then run mysql from the shell, but Django wont connect. The Dockerfile for the web/worker nodes looks like:

FROM python:2.7 ENV PYTHONUNBUFFERED 1 ENV DOCKER 1 ENV REDIS_HOST "redis" ENV CASSANDRA_HOST "cassandra" RUN mkdir /code WORKDIR /code ADD . /code/ RUN pip install --default-timeout=100 numpy==1.9.1 RUN pip install --default-timeout=100 scipy==0.15.1 RUN pip install --default-timeout=100 -r requirements_docker.txt #RUN python /code/app/manage.py migrate 

And the docker-compose file looks like this:

version: "2" services: cassandra: image: cassandra:latest redis: image: redis:latest db: image: mysql environment: MYSQL_DATABASE: appdb MYSQL_USER: dbuser MYSQL_PASSWORD: dbpass web: build: . command: python app/manage.py runserver 0.0.0.0:8000 environment: DOCKER: 1 DATABASE_HOST: db DATABASE_NAME: appdb DATABASE_USER: dbuser DATABASE_PASSWORD: dbpass REDIS_HOST: redis CASSANDRA_HOST: cassandra volumes: - .:/code ports: - "8000:8000" depends_on: - db - redis - cassandra worker: build: . environment: DOCKER: 1 DATABASE_HOST: db DATABASE_NAME: appdb DATABASE_USER: dbuser DATABASE_PASSWORD: dbpass REDIS_HOST: redis CASSANDRA_HOST: cassandra command: python /code/app/manage.py celery worker -Q celery,offline,periodic --broker=redis://redis:6379/4 depends_on: - db - redis - cassandra 

Finally the database settings are (consolidated from multiple settings files):

if not os.environ.has_key('DOCKER'): DATABASES = { "default": { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS': { 'read_default_file': os.path.join(PROJECT_ROOT, 'mysql.cnf'), "init_command": "SET foreign_key_checks = 0;", }, } } else: # Using Docker DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': os.environ['DATABASE_NAME'], 'USER': os.environ['DATABASE_USER'], 'PASSWORD': os.environ['DATABASE_PASSWORD'], 'HOST': os.environ["DATABASE_HOST"], 'PORT': '5432', 'OPTIONS': { 'init_command': "SET foreign_key_checks = 0;" } } } if 'test' in sys.argv: SLAVE_DATABASES = [] elif os.environ.has_key('DOCKER'): DATABASES['replica_1'] = { 'ENGINE': 'django.db.backends.mysql', 'NAME': os.environ['DATABASE_NAME'], 'USER': os.environ['DATABASE_USER'], 'PASSWORD': os.environ['DATABASE_PASSWORD'], 'HOST': os.environ["DATABASE_HOST"], 'PORT': '5432', 'OPTIONS': { "init_command": "SET foreign_key_checks = 0;" } } SLAVE_DATABASES = ['replica_1', ] DATABASE_ROUTERS = ('multidb.PinningMasterSlaveRouter',) MIDDLEWARE_CLASSES = ['multidb.middleware.PinningRouterMiddleware', ] + settings.MIDDLEWARE_CLASSES else: DATABASES["replica_1"] = { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS': { 'read_default_file': os.path.join(settings.PROJECT_ROOT, 'mysql.cnf'), "init_command": "SET foreign_key_checks = 0;", }, 'TEST': { 'MIRROR': 'default', }, } SLAVE_DATABASES = ['replica_1', ] DATABASE_ROUTERS = ('multidb.PinningMasterSlaveRouter',) MIDDLEWARE_CLASSES = ['multidb.middleware.PinningRouterMiddleware', ] + settings.MIDDLEWARE_CLASSES if os.environ.has_key("DOCKER"): from cassandra import ConsistencyLevel DATABASES['cassandra'] = { 'ENGINE': 'django_cassandra_engine', 'NAME': 'verificient_local', 'TEST_NAME': 'test_verificient_local', 'HOST': os.environ['CASSANDRA_HOST'], 'OPTIONS': { 'replication': { 'strategy_class': 'SimpleStrategy', 'replication_factor': 1 }, 'connection': { 'consistency': ConsistencyLevel.LOCAL_ONE, 'retry_connect': True }, 'session': { 'default_timeout': 30, 'default_fetch_size': 10000 } } } 

The weird part is that even though running python manage.py migrate returns an error (django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'db' (111)")) I am able to connect to the database with no issue using the command mysql -h$DATABASE_HOST -u$DATABASE_USER -p$DATABASE_PASSWORD $DATABASE_NAME

What could be going wrong in my django setup?

2
  • Have you tried to set HOST as db directly without using env variable? Commented May 23, 2017 at 17:20
  • 1
    As an aside... Slam all those ENV's into one statement to avoid useless layers. Same thing with the RUN's. Commented May 23, 2017 at 18:08

1 Answer 1

2

MySQL default port is 3306, so change:

'PORT': '5432', 

to

'PORT': '3306', 

It does work manually with your mysql command because that command uses the correct port by default

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.