Reputation: 27
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.
Upvotes: 2
Views: 1905
Reputation: 2214
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
Upvotes: 2