0

I have been trying to get a Django project to connect to a MySQL database on my local computer using a Dockerfile. I would like to be able to do this so that I can use a container to host a website on AWS, so ideally I would like to use a Dockerfile only (not docker compose) as it makes installing it onto Elastic Beanstalk and Lightsail easy.

I have created a repository on GitHub in the following location: https://github.com/MarkyMark1000/PYTHON_MySQL_Django_Docker

I have tried all kinds of things such as using 'RUN apk update && apk add py3-mysqlclient' or trying to install python mysql connector, but I have no luck at present. If you could let me know what I need to do, I would be very grateful.

Many thanks

Mark

MarkyMark1000
  • 417
  • 4
  • 19
  • 1
    Have you tried adding an `ENV` of your docker ip to your database like [this](https://stackoverflow.com/questions/44543842/how-to-connect-locally-hosted-mysql-database-with-the-docker-container) and call it with [django-env](https://django-environ.readthedocs.io/en/latest/)at your `settings.py` ? – Klim Bim Apr 29 '21 at 15:11
  • Hi Klim, within the docker run call, I pass in an environment variable which should be set to the localhost (I use -e DB_HOST=docker.for.mac.host.internal). Your link has some interesting ideas and I will have a look and see if I can work something out from there. – MarkyMark1000 Apr 29 '21 at 17:25
  • Hi Klim, I think I have got this working thanks to the link that you sent. I couldn't get the IP address because the container wasn't running, but I found that changing the settings.py file so that it had 'HOST': 'host.docker.internal', fixed the problem. I haven't tested it thoroughly yet, but many thanks – MarkyMark1000 Apr 29 '21 at 18:13

0 Answers0