1

I read quite a few posts about this but still no solution...

I have a docker-compose project with, among other, a django service that I build.

On my prod environment, it is using gunicorn + nginx. All fine, working as expected.

However on my dev environment, I am using only manage.py runserver. And here the troubles begin. Somehow, manage.py uses an old version of my settings.py that has been since then deleted. In my specific case, runserver is looking for a local mysql db, which doesnt exist because it is in another container.

So, it is the same settings.py between gunicorn and manage.py, why does it work in one and not the other one???

My project structure:

mysite
|_ django_mysite/
|   |_ __init__.py
|   |_ settings.py
|   |_ urls.py
|   |_ wsgi.py
|_ myapp/
|   |...
|_ static/
|   |...
|_ manage.py
|_ uwsgi_params

My manage.py:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_mysite.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

My wsgi.py:

"""
WSGI config for django_mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os
import sys

from django.core.wsgi import get_wsgi_application


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

os.environ['DJANGO_SETTINGS_MODULE'] = 'django_mysite.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_mysite.settings")

application = get_wsgi_application()

My Dockerfile (in case it is useful):

FROM alpine:3.7
MAINTAINER XXX XXX

# Dependencies
RUN rm -rf /var/cache/apk/* \
  && rm -rf /tmp/* \
  && apk update \
  && apk --no-cache add python py-pip build-base gettext libxslt-dev jpeg-dev \
  && mkdir -p /data/web

WORKDIR /data/web

# Django mysite requirements
COPY settings /data/web/
RUN apk --no-cache add python-dev mysql-client mysql-dev \
  && pip install --no-cache-dir -r requirements.txt
  && apk del -r python-dev mysql

# Pull mysite code
# Change settings.py and requirements.txt
RUN apk --no-cache add git \
  && git clone -b xx_xxxx https://github.com/XXX/xxx \
  && apk del -r git \
  && rm /data/web/mysite/requirements.txt \
  && rm /data/web/mysite/django_mysite/settings.py \
  && mv requirements.txt mysite/requirements.txt \
  && mv settings.py mysite/django_mysite/settings.tmp.py \
  && mv settings.build.py mysite/django_mysite/settings.py \

# Collect static django files and replace right settings.py file
WORKDIR /data/web/mysite
RUN python /data/web/mysite/manage.py collectstatic --no-input \
  && rm django_mysite/settings.py \
  && mv django_mysite/settings.tmp.py django_ mysite/settings.py

If I go into the django container and run "manage.py diffsettings", I see only the old settings I used for the collectstatic in my build.

However if I directly check the settings.py file from within the container I see the right settings.py.

On dev environment, my compose launch the runserver via the command:

/usr/bin/python manage.py runserver 0.0.0.0:8000

and having the following issue:

'Can\'t connect to local MySQL server through socket \'/run/mysqld/mysqld.sock\' (2 "No such file or directory")'

(makes sense. The django container doesnt have mysql, it is the mysql container which is referenced in the host)

On my prod:

/usr/bin/gunicorn django_mysite.wsgi:application -w 2 -b :8000

All working perfectly. Gunicorn from the django container deals with the msql container and the nginx container.

Any idea? Could it be related to Docker layers?

Thanks!

E

2
  • Got a bit confused about what issue are you having ... python manage.py runserver is unable to find the mysql DB, is that it? Are you running the application from inside a docker container on your dev machine? Commented Jan 8, 2018 at 12:53
  • Sorry for the confusion. Just updated the end of my answer. Commands are launched on boot from within the django container using the "command" key in docker-compose.yml Commented Jan 8, 2018 at 13:12

1 Answer 1

0

Can't tell from the files you shared, but this seems to be a recurring issue with applications in docker. Here are some options you can try:

1) Do you have different files for setting prod and dev databases? Check if the dev database have a "hostname" option. If it doesn't, 95% sure it will point to localhost (and therefore will give you trouble on dev machine).

2) Do you have a database container for the dev machine? Try to connect with mysql client from the application container, to the database container. You can use docker exec -it container_id /bin/bash (you'll have to adapt since it's Alpine) to attach to the app container.

3) Is the database container running, but you are not able to connect to it? Check if you container door is open to reach the database.

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

1 Comment

Thanks for the suggestions. My phpmyadmin container can very well connect to the mysql container. I indeed have different django settings files for dev and prod, but both have the hostname coming from an environment variable. Gunicorn well use the right settings file, however manage.py seems to use an old settings file deleted during the build

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.