How do I connect my docker to remote database? - java

I want to access mongo db running on url: "xyz" (remote host), and in my spring boot application properties, I have mentioned the mongodb url to "xyz".
Now when i run this application inside a docker container, it can not connect to remote url, and shows the connection refused error.
How do We access the remote database from inside a container ?
Below is my DockerFile
FROM openjdk:8-jdk-alpine
RUN apk add --no-cache bash
RUN apk add --no-cache curl
EXPOSE 8090
COPY target/<jar file> /application.jar
RUN mkdir /logs
RUN /bin/sh -c "apk add --no-cache bash"
ENTRYPOINT ["/usr/bin/java"]
CMD ["-DLOG_DIR=/logs", "-DLOG_FILE=application.log", "-jar", "-Dspring.profiles.active=local", "-Xmx1g", "/application.jar", "&"]
My Application.properties:
spring.data.mongodb.uri = <mongodb-url>
I build my docker image as:
docker build -t app:app .
I run the docker image as:
docker run -d <imageId>

Following command gets the job done.
docker run --net=host <imageId>
Be careful with this command because --net=host makes the container share network with the host. The network of the container will no longer be isolated.

Related

How to reach its URL using mysql configuration in a Dockerfile

I have one war file with the following MySQL setup; I wanted to compile it using Dockerfile so that I could utilise the mysql docker image.
However, I am missing/not properly configuring something, and as a result, I am receiving connection errors.
url: jdbc:mysql://localhost:3307/test-db
username: root
password: yum-hai-hum
DockerFile
FROM mysql:5.7
ADD test-db.sql /docker-entrypoint-initdb.d
EXPOSE 3307
ENV MYSQL_ROOT_PASSWORD=yum-hai-hum
ENV MYSQL_DATABASE=test-db
ENV MYSQL_USER=root
ENV MYSQL_PASSWORD=yum-hai-hum
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.war
COPY ${JAR_FILE} app.war
ENTRYPOINT ["java","-jar","/app.war"]
Error:
Build and run command for image and container:
docker build -t="navin/java-app" .
docker run -e "SPRING_PROFILES_ACTIVE=dev" -p 8080:8084 -t navin/java-app
url: jdbc:mysql://localhost:3307/test-db
correct me if i`m wrong,
The host of Mysql server is container1-ip:3307
the war app is on container2, and it is trying to connect to localhost:3307
probably change host of url from localhost to something like 192.168.100.19. You can get the actual ip using command ifconfig on the first container
The default Docker container access host IP is 172.17.0.1 , If you have a MySQL port 3306 that maps to the host, you can use the
url: jdbc:mysql://172.17.0.1:3306/test-db

Spring boot jar docker deployment throwing Connection Exception with MySQL Container

I just started exploring docker today and I've been trying to spin-up Spring Boot Web app backed by MySQL DB.
I pulled a MySQL container and ran it using:
docker run -t --name mysql-docker-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=**** -e MYSQL_DATABASE=spring_app_db -e MYSQL_USER=app_user -e MYSQL_PASSWORD=**** -d mysql
My application.properties file:
spring.datasource.url=jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username=app_user
spring.datasource.password=test123
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
with this, when I run my spring boot in IDE it's able to connect and perform CRUD on db table.
However, when I try to deploy it on docker and link with MySQL container it throws Connection Refused error.
My Dockerfile:
FROM openjdk:11
LABEL maintainer="baljinder#gmail.com"
VOLUME /tmp
EXPOSE 8080
ADD target/demo-0.0.1-SNAPSHOT.jar bootmysql.jar
ENTRYPOINT ["java", "-jar", "bootmysql.jar"]
The command I'm using to run docker image of the boot:
docker run -t --name spring-jpa-container -p 8080:8080 --link mysql-docker-container:mysql spring-jpa-app
Can someone please help with this. I've tried deploying both on the same container network by creating a docker network (docker network create -d bridge sql_spring_network) but the error persists.
When using the legacy linking of containers using the --link flag. The "linked" container is available as a host in the running container with it's container name. So in your case, the mysql container is available in your app container with hostname mysql.
Your database url, therefore should be: jdbc:mysql://mysql:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
use:
jdbc:mysql://mysql-docker-container:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
replace:
jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10

how to access mysql docker container in spring boot docker container

i have 2 docker file
1. mysql-dockerfile
FROM mysql:5.5
EXPOSE 3306
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE ToDoList
command used to build dockerfiles are as below
sudo docker build -t mysql-img -f mysql-dockerfile .
sudo docker run -d --name mysqlcontainer -p 3030:3306 mysql-img
2. java-dockerfile
FROM openjdk:8-jre-alpine
EXPOSE 9090
WORKDIR /usr/src
COPY target/*.war todoApp.war
CMD ["java","-jar","todoApp.war"]
command used to build dockerfiles are as below
sudo docker build -t java-img -f java-dockerfile .
docker run --name javacontainer -d -p 4040:9090 java-img
spring boot application consist jdbc url as follow
spring.datasource.url=jdbc:mysql://localhost:3030/ToDoList
i am not able to start project because spring boot application in docker is not able to connect mysql db which is in another container.
one solution i found is to bring two docker container in one docker network or link docker container.
can anyone please suggest good solution, modified docker run command and modified jdbc url.
Put them into one network and use container names as hostnames:
docker network create foo
docker run --network=foo --name mysqlcontainer -d mysql-img
docker run --network=foo --name javacontainer -d java-img
Dont expose ports - they are exposed automatically between containers inside network.
To connect inside, use mysqlcontainer:3306 and javacontainer:9090.
To connect from host, you will need port exposing.

docker microservice err_connection_refused

I'm trying to test a microservice running from docker.
Without docker I can access it on http://localhost:9999/
When I build the image, run it and try the same address http://localhost:9999/ I get err_connection_refused
In my dockerfile I
EXPOSE 9999
And when running the image I map ports
docker run -i -t 6bcb62617b00 -p 9999:9999
But this dosn't help.
`Docker-machine` ls returns `tcp://192.168.99.100:2376`
I am using docker quickstart terminal for testing.
This is the message I get when running the image Tomcat started on port(s): 9999
docker ps
Seems like it had something to do with the way I run it.
Here
docker run -d --network=name -p 9999:9999 beeline/dim_source_taxon
Instead of image id I use repository to start microservice and when I try
docker ps
I get
Also, instead of localhost I have to use the host provided by comand docker-machine ls

Dockerizing Spring Boot App with Postgresql

I am trying to create a Docker container for a Spring Boot application which uses PostgreSQL as database. My aim is build a container which runs app as well as PostgreSQL. I created a Dockerfile as below:
FROM ubuntu:15.10
LABEL version="1"
ADD app.jar app.jar
RUN bash -c 'touch /app.jar'
# Install Java, Postgresql
......
USER postgres
RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER test WITH SUPERUSER PASSWORD 'test';" &&\
createdb -O test test
USER root
RUN echo "local all all trust" >> /etc/postgresql/9.4/main/pg_hba.conf
RUN echo "listen_addresses='127.0.0.1'" >> /etc/postgresql/9.4/main/postgresql.conf
RUN echo "localhost vspyfe-db" >> /etc/hosts
#Define working directory.
WORKDIR /data
# Add VOLUMEs to allow backup of config, logs and databases
VOLUME ["/tmp", "/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
# Define commonly used JAVA_HOME variable
ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
COPY ./entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["bash", "/usr/lib/postgresql/9.4/bin/postgres", "-D", "/var/lib/postgresql/9.4/main", "-c", "config_file=/etc/postgresql/9.4/main/postgresql.conf"]
and entrypoint.sh is :
#!/bin/sh
/etc/init.d/postgresql start
java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
I build with this command docker build --rm=true -t vspyfe/base:v1 ., and
when I run the image with this command docker run -i -t vspyfe/base:v1 I got exception which says
Caused by: org.postgresql.util.PSQLException: FATAL: role "test" does not exist
at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:471)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:112)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
Even if I try to create test db with default username and password (postgres, postgres), then I got exception says test db does not exist. I do not understand where I am doing wrong. In Dockerfile I specified each parameter that I used in application.properties file.
Have you checked your connection properties? I have a working example of a spring-boot app working nicely with postgres. Both are running in different containers. Please refer to:
Spring Boot + Postgres example, using the fabric8.io plugin
To run the example:
mvn clean package -DskipTests docker:stop docker:build docker:run

Categories

Resources