2

I am trying to connect to my host mysql from docker container but I don't know how to connect. I have a django project for that I followed Django Docker I am using this tutorial this one is working fine for djnago and postgres.

I am using mac and I am using mamp server I want to connect my djnago app to my host mysql.

My docker file code is:

 FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/ 

And my docker compose file is:

 version: '3' services: db: image: postgres web: build: . command: python3 manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db 

This one is working fine but I am trying to connect my django app to my host mysql.

I don't know how to connect host mysql from my docker.

5
  • I am asking different question I followed tutorial and it is working my I want to connect this docker to my host mysql. Commented Sep 18, 2018 at 10:07
  • may be this will help you stackoverflow.com/questions/36367100/… Commented Sep 18, 2018 at 10:10
  • You just need to change the name db to your host ip address. Commented Sep 18, 2018 at 10:12
  • That is the issue I added my host ip it needs some configuration. Commented Sep 18, 2018 at 10:42
  • Possible duplicate of Access host database from a docker container Commented Sep 18, 2018 at 11:01

2 Answers 2

2

first with bellow command create a network named hostnetwork to have fixed host ip inside containers

docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 hostnetwork 

then change your docker-compose config to connect your containers to hostnetwork

services: db: image: image_name networks: - hostnetwork networks: hostnetwork: external: true 

now you can set django admin db config to your host mysql engine like bellow sample:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'db_name', 'USER': 'username', 'PASSWORD': '', 'HOST': '192.168.0.1', 'PORT': '3306' } } 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use "host.docker.internal" host if you are using mac. But this is only for development in your machine only

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.