Reputation: 2331
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.
Upvotes: 2
Views: 1628
Reputation: 1
I think you can use "host.docker.internal" host if you are using mac. But this is only for development in your machine only
Upvotes: 0
Reputation: 74
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'
}
}
Upvotes: 2