devdev
devdev

Reputation: 27

Using MySQL in a Docker Container

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:

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

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

Upvotes: 2

Views: 1905

Answers (1)

denvaar
denvaar

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

Related Questions