title: Dockerize Django + mySql app: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)") error
body: <p>I'm trying to dockerize my Django app which uses mySql but I'm facing some problems. It's the first time I use Docker so there may be something basic which I don't understand.</p>
  <p>Following some online tutorials, this is my Dockerfile:</p>
  <pre><code>FROM python:3.8
  ENV PYTHONUNBUFFERED 1
  RUN mkdir /code
  WORKDIR /code
  ADD . /code/
  RUN pip install --upgrade pip &amp;&amp; pip install -r requirements.txt
  RUN pip install mysqlclient
  COPY . /code/
  </code></pre>
  <p>And this is my docker-compose:</p>
  <pre><code>version: '3'
  
  services: 
    db:
      image: mysql:8
      ports:
        - '3306:3306'
      environment:
         MYSQL_DATABASE: 'database_name'
         MYSQL_USER: 'django'
         MYSQL_PASSWORD: 'django-password'
         MYSQL_ROOT_PASSWORD: 'password'
      volumes:
        - .setup.sql:/docker-entrypoint-initbd.d/setup.sql
  
    web:
      build: .
      command: python manage.py runserver 0.0.0.0:8000
      volumes:
        - .:/code
      ports:
        - &quot;8000:8000&quot;
      depends_on:
        - db
      links: 
        - db 
  </code></pre>
  <p>settings.py:</p>
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': os.environ.get('MYSQL_DATABASE', 'mysql-db'),
          'USER': os.environ.get('MYSQL_USER', 'mysql-user'),
          'PASSWORD': os.environ.get('MYSQL_PASSWORD', 'mysql-password'),
          'HOST': os.environ.get('MYSQL_DATABASE_HOST', 'db'),
          'PORT': os.environ.get('MYSQL_DATABASE_PORT', 3306),
      }
  }
  </code></pre>
  <p>And .env:</p>
  <pre><code>MYSQL_DATABASE=database_name
  MYSQL_USER=django
  MYSQL_PASSWORD=django-password
  MYSQL_ROOT_PASSWORD=password
  MYSQL_DATABASE_HOST=db
  MYSQL_DATABASE_PORT=3306
  </code></pre>
  <p>To dockerize the app, I'm using Docker desktop in Windows 10.</p>
  <p>After I run:</p>
  <pre><code>docker-compose up -d
  </code></pre>
  <p>I see both containers (db and web) are running, however in the web container I see that django is not able to connect to mySql, as it gives me the error:</p>
  <pre><code>django.db.utils.OperationalError: (2002, &quot;Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)&quot;)
  </code></pre>
  <p>I have seem some similar threads around but they didn't help me understand where the problem is, or how to solve it. Please help me to shed some light on the issue, thank you!</p>