0

I have a Spring boot app that makes jdbc connections to a data base in a different VPC. From my local I don't have access to this data base, so when I deploy my app locally I have to open a ssh tunnel in order to connect to the data base. This is how the tunnel is being set:

ssh -L 61616:data_base_host:data_base_port myuser@tunnel_server

And then my app connects using:

jdbc:sqlserver://localhost:61616....

Now I need to put my app inside a Docker container. This is how I run my docker image:

docker run -p 9018:9018 myapp

9018 is the http port. When the spring boot app starts inside Docker it can't connect to the data base. The error is:

Failed to initialize pool: The TCP/IP connection to the host localhost, port 61616 has failed.

So I need to bind the port 61616 inside the container to the port 61616 in the host. I was able to fix my problem using this jdbc URL:

jdbc:sqlserver://docker.for.mac.localhost:61616

Using the alias docker.for.mac.localhost makes the trick but it is a solution oriented to Mac platforms. I also tried:

docker run -p 9018:9018 -p 61616:61616 myapp

And didn't work.

Any ideas?

Perimosh
  • 2,304
  • 3
  • 20
  • 38
  • Docker does not provide a simple way to get hosts ip. According to the docker methodology if you're running container with the app there is no need to run db on the host, you should also containerize it. It is very easy to connect to DB if it is inside a container. Nevertheless you can try some workarounds, take a look on the second part of this answer https://stackoverflow.com/a/24326540/2065796 You can also consider an option to establish ssh tunnel from the inside of the docker container. – Sasha Shpota Oct 18 '17 at 17:50

1 Answers1

0

I don't know how you are running your application with in docker. I have a Dockerfile running for me.

NOTE : Change the ports according to your requirement

FROM openjdk:8-jre-alpine

ENV SPRING_OUTPUT_ANSI_ENABLED=ALWAYS \
    APP_SLEEP=0 \
    JAVA_OPTS=""

ADD *.war /app.war

EXPOSE 8080
CMD echo "The application will start in ${APP_SLEEP}s..." && \
    sleep ${APP_SLEEP} && \
    java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.war

Create a docker-compose.yml file

version: '2'
services:
    sample-app:
        image: sample
        environment:
            - SPRING_PROFILES_ACTIVE=prod,swagger
            - SPRING_DATASOURCE_URL=jdbc:sqlserver://sample-mssql:1433;database=testdb
            - APP_SLEEP=10 # gives time for the database to boot before the application
        ports:
            - 8080:8080
    sample-mssql:
        image: microsoft/mssql-server-linux:latest
        # volumes are not supported on macOS
        # volumes:
        #     - ~/volumes/jhipster/testdb/mssql/:/var/opt/mssql/data/
        environment:
            - ACCEPT_EULA=Y
            - SA_PASSWORD=yourStrong(!)Password
        ports:
            - 1433:1433

Create a docker images

docker build -t sample .

RUN

docker-compose up -d

Jinna Balu
  • 6,747
  • 38
  • 47
  • It is a spring boot app, it is a jar file. This is my CMD: CMD java -jar lq-rateloader.jar -Denv1=value1... I have a lot of -D variables. – Perimosh Oct 17 '17 at 18:55
  • you can change in your dockerfile, compose file works for you for any case. – Jinna Balu Oct 17 '17 at 19:05
  • 1
    I think you don't understand my problem. From my host I set up a ssh tunnel through port 61616. I need to make my Docker image send jdbc connections through the host. – Perimosh Oct 17 '17 at 19:24