6

HI I have my web app running on my local machine and connected to Mysql workbench, I am now trying to dockerize the webapp. I can't seem to get it to connect to the DB on my local dev machine (I am running Docker Desktop for Windows), can anyone tell me how I would go about this? Here is what I have so far.

`docker run -it -e "CATALINA_OPTS=-Dspring.profiles.active=dev -DPARAM1=DEV" -p 8080:8080 -p 8005:8005 -p 8009:8009 -p 3306:3306 --add-host=docker:192.168.1.7 -v C:\myapp\trunk\target\myapp.war:/usr/local/tomcat/webapps/myapp.war --name waitapp tomcat:8.0.38-jre8`

after a few second, I run docker ps -a

CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                           PORTS                                                                                            NAMES
2a1764dd9640        tomcat:8.0.38-jre8     "catalina.sh run"        2 minutes ago       Up 2 minutes                     0.0.0.0:3306->3306/tcp, 0.0.0.0:8005->8005/tcp, 0.0.0.0:8009->8009/tcp, 0.0.0.0:8080->8080/tcp   waitapp

The container seems to be running, but I get a 404 Not found when I try the rest request, this is the same as I do when running from inside spring tool suite using built in tomcat server.

NOTE I don't want to run a separate mysql container and link the two over a network, I just want to try get my newly created docker app to connect to my local DB MySQL.

veben
  • 19,637
  • 14
  • 60
  • 80
Gman
  • 2,433
  • 3
  • 26
  • 36
  • What you mean with *connected to MySQL Workbench*? Also can you provide a piece of code or how you're trying to access the container? As far as I can see you're running the application in a container how do you expect the application will use a DB outside it's network? Are you running MySQL DB on the same container? Do you want to connect to the DB inside the container using MySQL Workbench? – ReynierPM Dec 15 '16 at 16:38
  • Ok, I will try explain better, I have MySQL Workbench running on my dell laptop and my spring application connects to it when I run it through SpringToolSuite IDE. Now I am running a tomcat container with the docker run command above and mounting my war inside that container but want it to still connect to the MySQL db that is on my laptop. How can I acheive this without ruining/spinning up a MySQL container. I want the container to talk to my host machine. – Gman Dec 15 '16 at 21:54
  • I don't know exactly how to achieve this but I think it's possible. Maybe [this](https://docs.docker.com/engine/userguide/networking/default_network/container-communication/) and [this](https://docs.docker.com/engine/userguide/networking/) could help you understand how network works in Docker and from there you can achieve what you need. – ReynierPM Dec 15 '16 at 21:58
  • Ye ok thanks for links, I will post back when I find out how its done. – Gman Dec 15 '16 at 22:00

1 Answers1

0

As mentioned on this post, you can try 2 things:

  1. Add gateway and use it from containers, like

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

  1. In addition to your app container, run proxy (ngnix?) container, which will rout the calls to DB when required

This answer also show how can you obtain the host IP inside the docker container.

Community
  • 1
  • 1
evgenyl
  • 7,837
  • 2
  • 27
  • 32