2

I am kinda new to Docker and I am trying to build an image for a Django App using MySQL. The problem that I am having is, after running my image I get the following error : django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)") . As a base for the image I am using FROM django:python2 , I have installed the server using the following commands:

  • RUN echo "mysql-server mysql-server/root_password password X" | debconf-set-selections
  • RUN echo "mysql-server mysql-server/root_password_again password X" | debconf-set-selections
  • RUN apt-get update && apt-get install -y mysql-server

To fix the problem I tried multiple solutions, which I found on the internet such as:

  • RUN touch /var/run/mysqld/mysqld.sock
  • RUN chmod -R 755 /var/run/mysqld/mysqld.sock
  • EXPOSE 3306

Sadly, nothing worked. I also made sure the server is running, yet the problem is still there.

6
  • Let's see your Dockerfile and docker-compose.yml files. The database settings in settings.py could also help provide some insight. Commented Oct 10, 2016 at 21:42
  • As I am kinda new to stackoverflow as well I am not sure how to post a whole file. Honestly,I have written most of the Dockerfile in the post except the apt-get for different modules etc. As for the docker-compose I wrote something briefly and it failed with the same error, so I decided to first get my Dockerfile running. Commented Oct 10, 2016 at 21:50
  • Are you able to get into the running container? docker exec -it [your container name here] /bin/bash will give you a shell where you can do some debugging. From inside the container you can try to see if mysql is even running (it doens't look like it is): /etc/init.d/mysql status Commented Oct 10, 2016 at 21:57
  • MySQL should be running as I have RUN service mysql start Commented Oct 10, 2016 at 21:58
  • What have you set HOST to in your database config ? Commented Oct 10, 2016 at 21:59

1 Answer 1

2

Here is an example config that works really well.

Dockerfile

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

Make sure to change the path to your requirements.txt file (if exists.)

docker-compose.yml

db:
  image: mysql
web:
  build: .
  environment:
    - PYTHONPATH=/code/server/
    - DJANGO_SETTINGS_MODULE=path.to.your.settings.file
  command: python server/manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

Make sure to replace the path to DJANGO_SETTINGS_MODULE with the correct dotted-path to your settings file.

docker-compose build then docker-compose up

EDIT

If you use this config, change the value of HOST to db in your settings.py

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

2 Comments

I get the same error again but instead of (111) at the end its a (2) : django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
I have a quick question. Why did you write environment: - PYTHONPATH=/code/server/ And afterwards followed by command: python server/manage.py If i leave server before manage.py I get an error that it does not exist.

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.