I have a mysql server and a docker container running on my machine. In the docker container, I run a django website. I want to connect to the local mysql server from the docker container. How can I do that?
Asked
Active
Viewed 167 times
0
-
can you add your docker-compose file? – JPG Aug 12 '18 at 12:58
-
Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Aug 12 '18 at 14:04
1 Answers
-1
I usually do ( for testing purposes ) :
docker network create -d my-bridge
docker run --network my-bridge --name app-db -e MYSQL_ROOT_PASSWORD=secret -e
MYSQL_DATABASE=myapp -e MYSQL_USER=myapp_user -e MYSQL_PASSWORD=myapp_secret
mysql:latest
docker run --network my-bridge --name app -p 80:80 -e DB_HOST=app-db -e DB_USER=myapp_user -e DB_PASS=myapp_secret -e DB_NAME=myapp myapp:latest
In my app Dockefile I am using in entrypoint something like envsubst, or if code language can read from environment variables I dont need to setup this.
Forget about --link
docker parameter -> its obsolete

The Impaler
- 45,731
- 9
- 39
- 76

MUHAHA
- 1,597
- 2
- 16
- 25
-
when I tried the 2nd command, I got an error like `ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'` – Abhinav Aug 15 '18 at 16:11