2

I'm trying to dockerize my Django app with Docker, but having some trouble connecting with the mysql database since i'm not really experienced with Docker. I have a couple questions:

  1. I have installed mysql and set up the database locally on my computer, do i have to use a mysql image and create a db service in my docker-compose too ? Shouldn't Django connect to mysql normally like it did when it wasn't dockerized ?

  2. If a database is hosted on a server then is mysql installed on that server ? Do i have to use a mysql image to connect to the database ?

Here is my docker-compose.yml:

version: '3.7'

services:
  web:
    build: ./appchat
    command: python appchat/manage.py runserver
    ports:
      - 8000:8000
      - "3306"
    network_mode: "host"

And here is the database settings in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': 'root',
        'NAME': 'company',
        'PORT': '3306',
    }
}

Here is the error when i ran docker-compose file:

django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Any help would be appreciated. Thank you.

Fuih
  • 33
  • 1
  • 5
  • https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Enix Oct 06 '19 at 09:24
  • try to use `host.docker.internal` to connect mysql inside django container – Enix Oct 06 '19 at 09:43
  • 2
    i tried 'HOST': 'host.docker.internal' in settings.py, still got the same error. – Fuih Oct 06 '19 at 10:06

1 Answers1

1

The application and its database could be running on the same host or different hosts, and in Docker or not.

If the application and its database are running on different hosts, there is nothing unusual about setting this up in Docker. Configure your application with the DNS name of the database server. (I would recommend passing this via environment variables rather than modifying the settings.py file.)

Docker Compose syntax:

environment:
  MYSQL_HOST: mysql.example.com

If both are running in the same Docker setup, then Docker provides an internal DNS setup for one to reach the other. In Docker Compose, you can use the services: key as a host name; in plain Docker, you need to manually docker network create but then this trick works.

Plain Docker example:

docker network create app
docker run --net app --name mysql -v $PWD/mysql:/var/lib/mysql/data mysql
docker run --net app --name app -e MYSQL_HOST=mysql myapp

If the database is running on the same host as the application, but outside Docker, and the host is a Mac or Windows system running the Docker Desktop application, then there is a special host.docker.internal hostname

docker run -e MYSQL_HOST=host.docker.internal myapp

For a native-Linux host this shortcut doesn't exist and you need to find out the host's IP address, but then you can treat this like the first case.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thank you for the explanation. At first when i tried 'host.docker.internal', i encountered an error about 'caching_sha2_password' so i thought it didn't work. But turn out 'host.docker.internal' is correct, the error is at mysql side and i resolved it now. – Fuih Oct 07 '19 at 07:57