docker network not working without link - java

I have a mysql container and a service container that needs to connect to the mysql container.
I created a networking with docker network create chrisbolton
I am spinning up the mysql container with
docker run --name chrisbolton-mysql -v /Users/Bolton/chrisbolton-data:/var/lib/mysql --network chrisbolton -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=chrisbolton -d mysql:latest
I am spinning up the service container with
docker run -p 8080:8080 --name chrisbolton-service --network chrisbolton --link chrisbolton-mysql:mysql -d chrisbolton-service
However, I know that link is deprecated and I need to move to only using networks. But if I remove the link here the two containers cannot communicate.
I am connecting to mysql with the following config:
#disbale Spring banner
spring.main.banner-mode=off
spring.datasource.url=jdbc:mysql://mysql:3306/chrisbolton
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
I have done a docker network inspect chrisbolton to get the IPAddresses and have tried changing my config file to point to that directly. But still not sure why it won't connect.

datasource.url is not correct, you should specify the name of the container to which you want to connect. There are better options than using the name of service. You can use aliases or hostname.
While in this case, as you indicated in comments, datasource.url needs to be jdbc:mysql://chrisbolton-mysql:3306/chrisbolton

Related

Not able to access the docker containerised java web app on exposed port

I have Java web app in docker container.
Now container is exposed on host's port 8080 from container's port 8080 by running docker run command.
docker run -d -p 8080:8080 --name myTomcat -v $(pwd)/out/artifacts/DockerJavaWebAppWarExploded:/usr/local/tomcat/webapps/ tomcat:latest
There is no other process running on 8080 port ,but i am not able to access the application from browser http://localhost:8080/.
Container logs :
Please help.
It would be appreciated.
Check if http://0.0.0.0:8080 returns values. If yes, you may add an enrty for 0.0.0.0 in your hosts file.

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

How to connect to Postgresql service inside Docker Swarm?

I have a Docker Swarm cluster setup with 3 servers(1 manager and 2 workers).
I started a Postgresql service using the command below:
docker service create --name postgresql \
--mount src=pg_data,dst=/var/lib/postgresql/data/pgdata \
-p 6542:5432 \
-e POSTGRES_USER="user" \
-e POSTGRES_DB="db" \
-e POSTGRES_PASSWORD="pass" \
-e PGDATA=/var/lib/postgresql/data/pgdata \
--constraint 'node.role == manager' \
postgres
I also created the data volume previously:
docker volume create pg_data
Now, I have another service that I want to start, which is basically a Java application that I bundled into a Docker image and I want to connect it to the postgresql service. I tried the following combinations for the url:
jdbc:postgresql://172.18.0.1:5432/db (docker_gwbridge)
jdbc:postgresql://172.17.0.1:5432/db (docker0)
jdbc:postgresql://localhost:5432/db
jdbc:postgresql://postgresql:5432/db
Any idea what could work?
You shall use a swarm overlay network to connect to your database inside swarm mode.
First create the overlay network:
docker network create -d overlay mynet
Then make your postgresql service use this network:
docker service create --name postgresql \
--mount ... \
--network mynet \
postgres
Then, don't forget to use the same network for your Java application container:
docker service create --name myjavaapp \
--network mynet \
myjavaapp
Then you can connect to your postgresql through DNS name like:
jdbc:postgresql://postgresql:5432/db
All service's containers in the mynet network (you can call it as you want, it's just a name reference), have DNS entries corresponding to service name. This is easier than having to retrieve container's IP through docker inspect before launching your java application.
You can even avoid the publish port -p 6542:5432 in your postgresql docker service as your probably don't want to expose this to others.
You can have a look at the official doc to better understand networks in swarm mode.
This SO QA also talks about overlay network.
Instead of dst=/var/lib/postgresql/data/pgdata try target target=/var/lib/postgresql/data

Categories

Resources