i have an akka http application and one keycloak docker container
i used sbt native docker plugin to create a an image of my app and then i wrote a docker compose file but my app container is not discovering keycloak container
here is my build.sbt file
enablePlugins(DockerPlugin)
enablePlugins(JavaAppPackaging)
dockerExposedPorts := Seq(8083)
then i created a docker image of my project
>docker:publishLocal
then i created a docker-compose file
version: '3.3'
services:
keycloak:
image: jboss/keycloak
container_name: docker-keycloak-container
ports:
- "8080:8080"
environment:
- KEYCLOAK_USER=admin
- KEYCLOAK_PASSWORD=admin
akkahttpservice:
image: myproject-auth:0.0.1
container_name: docker-myproject-auth-container
ports:
- "8083:8083"
depends_on:
- keycloak
docker ps shows
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
181522f78d22 myproject-auth:0.0.1 "/opt/docker/bin/int…" 45 seconds ago Up 40 seconds 0.0.0.0:8083->8083/tcp docker-myproject-auth-container
d92d9c4f0a19 jboss/keycloak "/opt/jboss/tools/do…" 5 days ago Up 46 seconds 0.0.0.0:8080->8080/tcp, 8443/tcp docker-keycloak-container
but when i hit my applications route which talks to keycloak i got
14:13:32.020 [scala-execution-context-global-45] ERROR com.ifkaar.lufz.authentication.actors.worker.TokenManagerActor - Actor TokenManager: exception in fetching token
docker-myproject-auth-container | akka.stream.StreamTcpException: Tcp command [Connect(0.0.0.0:8080,None,List(),Some(10 seconds),true)] failed because of java.net.ConnectException: Connection refused
docker-myproject-auth-container | Caused by: java.net.ConnectException: Connection refused
docker-myproject-auth-container | at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
docker-myproject-auth-container | at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:714)
docker-myproject-auth-container | at akka.io.TcpOutgoingConnection$$anonfun$connecting$1.$anonfun$applyOrElse$4(TcpOutgoingConnection.scala:111)
docker-myproject-auth-container | at akka.io.TcpOutgoingConnection.akka$io$TcpOutgoingConnection$$reportConnectFailure(TcpOutgoingConnection.scala:53)
docker-myproject-auth-container | at akka.io.TcpOutgoingConnection$$anonfun$connecting$1.applyOrElse(TcpOutgoingConnection.scala:111)
docker-myproject-auth-container | at akka.actor.Actor.aroundReceive(Actor.scala:537)
docker-myproject-auth-container | at akka.actor.Actor.aroundReceive$(Actor.scala:535)
docker-myproject-auth-container | at akka.io.TcpConnection.aroundReceive(TcpConnection.scala:33)
docker-myproject-auth-container | at akka.actor.ActorCell.receiveMessage(ActorCell.scala:577)
docker-myproject-auth-container | at akka.actor.ActorCell.invoke(ActorCell.scala:547)
docker-myproject-auth-container | at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:270)
docker-myproject-auth-container | at akka.dispatch.Mailbox.run(Mailbox.scala:231)
docker-myproject-auth-container | at akka.dispatch.Mailbox.exec(Mailbox.scala:243)
docker-myproject-auth-container | at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
docker-myproject-auth-container | at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
docker-myproject-auth-container | at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
docker-myproject-auth-container | at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
but everything works when i run my app from sbt and use docker keycloak container why is that so ?
also i am running my akka http project on http://0.0.0.0:8083
kindly guide me on this
You need to inject address of keycloak into your service via environment variable. For this you can use service name as keycloak address (Compose docs)
services:
keycloak:
image: jboss/keycloak
container_name: docker-keycloak-container
ports:
- "8080:8080"
environment:
- KEYCLOAK_USER=admin
- KEYCLOAK_PASSWORD=admin
akkahttpservice:
image: myproject-auth:0.0.1
container_name: docker-myproject-auth-container
environment:
- KEYCLOAK_URL: "http://keycloak:8080"
ports:
- "8083:8083"
depends_on:
- keycloak
Related
I'm running ksqldb-server from a docker-compor found here https://ksqldb.io/quickstart.html#quickstart-content
My kafka bootstrap server is running on the same VM in standard alone mode.
I can see the messages in one topic with a console consumer:
sudo kafka-avro-console-consumer --from-beginning --bootstrap-server localhost:9092 --topic source-air-input --property print.key=true --max-messages 2
Unfortunatly running ksql from docker gives me this error.
ksqldb-server | [2021-07-15 23:12:58,772] ERROR Failed to start KSQL (io.confluent.ksql.rest.server.KsqlServerMain:66)
ksqldb-server | java.lang.RuntimeException: Failed to get Kafka cluster information
ksqldb-server | at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:107)
ksqldb-server | at io.confluent.ksql.rest.server.KsqlRestApplication.buildApplication(KsqlRestApplication.java:624)
ksqldb-server | at io.confluent.ksql.rest.server.KsqlServerMain.createExecutable(KsqlServerMain.java:152)
ksqldb-server | at io.confluent.ksql.rest.server.KsqlServerMain.main(KsqlServerMain.java:59)
ksqldb-server | Caused by: java.util.concurrent.TimeoutException
ksqldb-server | at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:108)
ksqldb-server | at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:272)
ksqldb-server | at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:105)
My docker-compose.yml is the following.
---
version: '3.9'
services:
ksqldb-server:
image: confluentinc/ksqldb-server:0.18.0
hostname: ksqldb-server
container_name: ksqldb-server
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "8088:8088"
environment:
KSQL_LISTENERS: http://0.0.0.0:8088
KSQL_BOOTSTRAP_SERVERS: host.docker.internal:9092
KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
ksqldb-cli:
image: confluentinc/ksqldb-cli:0.18.0
container_name: ksqldb-cli
depends_on:
- ksqldb-server
entrypoint: /bin/sh
tty: true
I tried many possible configurations for the address without success.
What might be wrong?
I tried the suggestions from this question From inside of a Docker container, how do I connect to the localhost of the machine? without success.
Modify Kafka's server.properties
listener.security.protocol.map=PLAINTEXT_DOCKER:PLAINTEXT,PLAINTEXT_LOCAL:PLAINTEXT
listeners=PLAINTEXT_DOCKER://:29092,PLAINTEXT_LOCAL://localhost:9092
advertised.listeners=PLAINTEXT_DOCKER://host.docker.internal:29092,PLAINTEXT_LOCAL://localhost:9092
inter.broker.listener.name=PLAINTEXT_LOCAL
Update your Compose like so to point at the host rather than itself
version: '3.9'
services:
# TODO: add schema-registry
# environment:
# SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: host.docker.internal:29092
# extra_hosts:
# - "host.docker.internal:host-gateway"
# or any other Kafka client
ksqldb-server:
image: confluentinc/ksqldb-server:0.18.0
hostname: ksqldb-server
container_name: ksqldb-server
ports:
- "8088:8088"
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
KSQL_BOOTSTRAP_SERVERS: host.docker.internal:29092
...
(Tested on Mac), Getting /info endpoint of KSQL
http :8088/info
HTTP/1.1 200 OK
content-length: 133
content-type: application/json
{
"KsqlServerInfo": {
"kafkaClusterId": "ZH2-h1W_SaivCW0qa8DQGA",
"ksqlServiceId": "default_",
"serverStatus": "RUNNING",
"version": "0.18.0"
}
}
Replace all host.docker.internal above with the external hostname/IP of the machine, if Kafka is a remote server
We are using the curator service discovery in docker and kubernetes environments. We setup the connection string using the DNS names of the containers/pods. The problem I am seeing is that it seems to interpret these down to the IP address. The container or pod can change IP addresses and curator does not seem to pickup the change.
The behavior I see if it I standup a 3 node zookeeper cluster and stand up 1 or more agents. I then roll the zookeeper nodes 1 at a time and they each change their IP address, when I bounce the third zookeeper instance all the client lose their connection.
Is there a way to force it to always use the DNS names for connection?
Here is my compose example
version: '2.4'
x-zookeeper:
&zookeeper-env
JVMFLAGS: -Dzookeeper.4lw.commands.whitelist=ruok
ZOO_ADMINSERVER_ENABLED: 'true'
ZOO_STANDALONE_ENABLED: 'false'
ZOO_SERVERS: server.1=zookeeper1:2888:3888;2181 server.2=zookeeper2:2888:3888;2181 server.3=zookeeper3:2888:3888;2181
x-agent:
&agent-env
ZK_CONNECTION: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181
SERVICE_NAME: myservice
services:
zookeeper1:
image: artifactory.rd2.thingworx.io/zookeeper:${ZOOKEEPER_IMAGE_VERSION}
restart: always
ports:
- 2181
- 8080
healthcheck:
test: echo ruok | nc localhost 2181 | grep imok
interval: 15s
environment:
<<: *zookeeper-env
ZOO_MY_ID: 1
zookeeper2:
image: artifactory.rd2.thingworx.io/zookeeper:${ZOOKEEPER_IMAGE_VERSION}
restart: always
ports:
- 2181
- 8080
healthcheck:
test: echo ruok | nc localhost 2181 | grep imok
interval: 15s
environment:
<<: *zookeeper-env
ZOO_MY_ID: 2
zookeeper3:
image: artifactory.rd2.thingworx.io/zookeeper:${ZOOKEEPER_IMAGE_VERSION}
restart: always
ports:
- 2181
- 8080
healthcheck:
test: echo ruok | nc localhost 2181 | grep imok
interval: 15s
environment:
<<: *zookeeper-env
ZOO_MY_ID: 3
agent1:
image: artifactory.rd2.thingworx.io/twxdevops/discovery-tool:latest
environment:
<<: *agent-env
GLOBAL_ID: AGENT1
agent2:
image: artifactory.rd2.thingworx.io/twxdevops/discovery-tool:latest
environment:
<<: *agent-env
GLOBAL_ID: AGENT2
agent3:
image: artifactory.rd2.thingworx.io/twxdevops/discovery-tool:latest
environment:
<<: *agent-env
GLOBAL_ID: AGENT3
agent4:
image: artifactory.rd2.thingworx.io/twxdevops/discovery-tool:latest
environment:
<<: *agent-env
GLOBAL_ID: AGENT4
agent5:
image: artifactory.rd2.thingworx.io/twxdevops/discovery-tool:latest
environment:
<<: *agent-env
GLOBAL_ID: AGENT5
The run steps are
docker-compose up -d zookeeper1 zookeeper2 zookeeper3 agent1
docker-compose rm -sf zookeeper3
docker-compose up -d agent2
docker-compose up -d zookeeper3
docker-compose rm -sf zookeeper2
docker-compose up -d agent3
docker-compose up -d zookeeper2
docker-compose rm -sf zookeeper1
docker-compose up -d agent5
docker-compose up -d zookeeper1
After I kill the last zookeeper node the agent gets the following error and does not recover. You can see it is referencing an IP address
Path:null finished:false header:: 5923,4 replyHeader:: 5923,8589934594,0 request:: '/services/myservice/cc1996fb-cca5-4108-bd06-567b45f594d7,F response:: #7b226e616d65223a226d7973657276696365222c226964223a2263633139393666622d636361352d343130382d626430362d353637623435663539346437222c2261646472657373223a223137322e32312e302e33222c22706f7274223a383038302c2273736c506f7274223a6e756c6c2c227061796c6f6164223a7b2240636c617373223a22636f6d2e7468696e67776f72782e646973636f766572792e7a6b2e53657276696365496e7374616e636544657461696c73222c2261747472696275746573223a7b22474c4f42414c4944223a224147454e5433227d7d2c22726567697374726174696f6e54696d65555443223a313634393739313735353936322c227365727669636554797065223a2244594e414d4943222c2275726953706563223a7b227061727473223a5b7b2276616c7565223a2261646472657373222c227661726961626c65223a747275657d2c7b2276616c7565223a223a222c227661726961626c65223a66616c73657d2c7b2276616c7565223a22706f7274222c227661726961626c65223a747275657d5d7d7d,s{4294967301,4294967301,1649791757073,1649791757073,0,0,0,144117976615550976,404,0,4294967301}
agent1_1 | 19:48:46.438 [ServiceEventWatcher-myservice] DEBUG com.thingworx.discovery.zk.ZookeeperProvider - ZooKeeper resolved addresses for service myservice: [ServiceDefinition [serviceName=myservice, host=172.21.0.7, port=8080, tags={GLOBALID=AGENT2}], ServiceDefinition [serviceName=myservice, host=172.21.0.4, port=8080, tags={GLOBALID=AGENT1}], ServiceDefinition [serviceName=myservice, host=172.21.0.3, port=8080, tags={GLOBALID=AGENT3}]]
agent1_1 | 19:48:47.070 [main-SendThread(172.21.0.5:2181)] WARN org.apache.zookeeper.ClientCnxn - Session 0x200028941eb0001 for sever service-discovery-docker-tests_zookeeper2_1.service-discovery-docker-tests_default/172.21.0.5:2181, Closing socket connection. Attempting reconnect except it is a SessionExpiredException.
agent1_1 | org.apache.zookeeper.ClientCnxn$EndOfStreamException: Unable to read additional data from server sessionid 0x200028941eb0001, likely server has closed socket
agent1_1 | at org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:77)
agent1_1 | at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:350)
agent1_1 | at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1275)
agent1_1 | 19:48:47.171 [main-EventThread] INFO org.apache.curator.framework.state.ConnectionStateManager - State change: SUSPENDED
agent1_1 | 19:48:47.363 [main-SendThread(172.21.0.9:2181)] DEBUG org.apache.zookeeper.SaslServerPrincipal - Canonicalized address to 172.21.0.9
agent1_1 | 19:48:47.363 [main-SendThread(172.21.0.9:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server 172.21.0.9/172.21.0.9:2181.
agent1_1 | 19:48:47.363 [main-SendThread(172.21.0.9:2181)] INFO org.apache.zookeeper.ClientCnxn - SASL config status: Will not attempt to authenticate using SASL (unknown error)
agent1_1 | 19:48:47.430 [ServiceEventWatcher-myservice] DEBUG com.thingworx.discovery.zk.ZookeeperProvider - Getting registered addresses from ZooKeeper for service myservice
Zookeeper cluster is happy and fine. So the main question is there a way to have it use the DNS names instead of the IP addresses? Should also mention that service discovery uses ephemeral nodes so disconnect and reconnect is bad.
I'm running ksqldb-server from a docker-compor found here https://ksqldb.io/quickstart.html#quickstart-content
My kafka bootstrap server is running on the same VM in standard alone mode.
I can see the messages in one topic with a console consumer:
sudo kafka-avro-console-consumer --from-beginning --bootstrap-server localhost:9092 --topic source-air-input --property print.key=true --max-messages 2
Unfortunatly running ksql from docker gives me this error.
ksqldb-server | [2021-07-15 23:12:58,772] ERROR Failed to start KSQL (io.confluent.ksql.rest.server.KsqlServerMain:66)
ksqldb-server | java.lang.RuntimeException: Failed to get Kafka cluster information
ksqldb-server | at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:107)
ksqldb-server | at io.confluent.ksql.rest.server.KsqlRestApplication.buildApplication(KsqlRestApplication.java:624)
ksqldb-server | at io.confluent.ksql.rest.server.KsqlServerMain.createExecutable(KsqlServerMain.java:152)
ksqldb-server | at io.confluent.ksql.rest.server.KsqlServerMain.main(KsqlServerMain.java:59)
ksqldb-server | Caused by: java.util.concurrent.TimeoutException
ksqldb-server | at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:108)
ksqldb-server | at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:272)
ksqldb-server | at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:105)
My docker-compose.yml is the following.
---
version: '3.9'
services:
ksqldb-server:
image: confluentinc/ksqldb-server:0.18.0
hostname: ksqldb-server
container_name: ksqldb-server
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "8088:8088"
environment:
KSQL_LISTENERS: http://0.0.0.0:8088
KSQL_BOOTSTRAP_SERVERS: host.docker.internal:9092
KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
ksqldb-cli:
image: confluentinc/ksqldb-cli:0.18.0
container_name: ksqldb-cli
depends_on:
- ksqldb-server
entrypoint: /bin/sh
tty: true
I tried many possible configurations for the address without success.
What might be wrong?
I tried the suggestions from this question From inside of a Docker container, how do I connect to the localhost of the machine? without success.
Modify Kafka's server.properties
listener.security.protocol.map=PLAINTEXT_DOCKER:PLAINTEXT,PLAINTEXT_LOCAL:PLAINTEXT
listeners=PLAINTEXT_DOCKER://:29092,PLAINTEXT_LOCAL://localhost:9092
advertised.listeners=PLAINTEXT_DOCKER://host.docker.internal:29092,PLAINTEXT_LOCAL://localhost:9092
inter.broker.listener.name=PLAINTEXT_LOCAL
Update your Compose like so to point at the host rather than itself
version: '3.9'
services:
# TODO: add schema-registry
# environment:
# SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: host.docker.internal:29092
# extra_hosts:
# - "host.docker.internal:host-gateway"
# or any other Kafka client
ksqldb-server:
image: confluentinc/ksqldb-server:0.18.0
hostname: ksqldb-server
container_name: ksqldb-server
ports:
- "8088:8088"
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
KSQL_BOOTSTRAP_SERVERS: host.docker.internal:29092
...
(Tested on Mac), Getting /info endpoint of KSQL
http :8088/info
HTTP/1.1 200 OK
content-length: 133
content-type: application/json
{
"KsqlServerInfo": {
"kafkaClusterId": "ZH2-h1W_SaivCW0qa8DQGA",
"ksqlServiceId": "default_",
"serverStatus": "RUNNING",
"version": "0.18.0"
}
}
Replace all host.docker.internal above with the external hostname/IP of the machine, if Kafka is a remote server
i'm new to Docker, as a part of dockerization of spring mvc application , i'm not able to connect my application to MySQL server
Dockerfile :this is Spring mvc application so need to copy my war to
tomcat container
FROM tomcat:8.0.20-jre8
COPY /target/CTH.war /usr/local/tomcat/webapps/
docker-compose.yml
version: '3'
services:
mysql-standalone:
image: 'mysql:5.7'
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD= root
- MYSQL_USER= root
- MYSQL_PASSWORD= root
- MYSQL_DATABASE= CTH
- MYSQL_DATABASE= CTH_CHAT
ports:
- "3307:3306"
cth-docker-container:
image: cth-docker-container
ports:
- "8082:8082"
environment:
CTH_DATASOURCE_DATABASE: CTH
CTH_DATASOURCE_SERVERNAME: mysql-standalone
CTH_DATASOURCE_USERNAME: root
CTH_DATASOURCE_PASSWORD: root
CTH_DATASOURCE_PORT: 3307
CTH_CHAT_DATASOURCE_DATABASE: CTH_CHAT
CTH_CHAT_DATASOURCE_SERVERNAME: mysql-standalone
CTH_CHAT_DATASOURCE_USERNAME: root
CTH_CHAT_DATASOURCE_PASSWORD: root
CTH_CHAT_DATASOURCE_PORT: 3307
build:
context: "./"
dockerfile: "Dockerfile"
depends_on:
- mysql-standalone
application.properties :this is spring mvc application and application uses MySQL db with two databases
1st db
dbuser.local=${CTH_DATASOURCE_USERNAME:root}
dbpassword.local=${CTH_DATASOURCE_PASSWORD:root}
dbdatabaseName.local=${CTH_DATASOURCE_DATABASE:CTH}
dbserverName.local=${CTH_DATASOURCE_SERVERNAME:localhost}
dbportNumber.local=${CTH_DATASOURCE_PORT:3306}
2nd db
dbuser.cth.chat.local=${CTH_CHAT_DATASOURCE_USERNAME:root}
dbpassword.cth.chat.local=${CTH_CHAT_DATASOURCE_PASSWORD:root}
dbdatabaseName.cth.chat.local=${CTH_CHAT_DATASOURCE_USERNAME:CTH_CHAT}
dbserverName.cth.chat.local=${CTH_CHAT_DATASOURCE_SERVERNAME:localhost}
dbportNumber.cth.chat.local=${CTH_CHAT_DATASOURCE_PORT:3306}
i read articles from which i created dockerfile and docker-compose file https://medium.com/#asce4s/dockerize-spring-mvc-application-a9ffbd11eadb https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases/docker/provision/mysql/init
but i'm getting following erros when i execute
docker-compose -f docker-compose.yml up
Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure
cth-docker-container_1 |
cth-docker-container_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
cth-docker-container_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
cth-docker-container_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
cth-docker-container_1 | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
cth-docker-container_1 | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
cth-docker-container_1 | at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
cth-docker-container_1 | at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:122)
cth-docker-container_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1754)
cth-docker-container_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1624)
cth-docker-container_1 | ... 125 common frames omitted
cth-docker-container_1 | Caused by: java.net.UnknownHostException: mysql-standalone
cth-docker-container_1 | at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
cth-docker-container_1 | at com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:179)
cth-docker-container_1 | at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
cth-docker-container_1 | ... 128 common frames omitted
i'm stuck and not getting if i have set wrong environment or my way is not correct your any suggestion i'll definitely help me
thanks!
After reading lots of articles, I got success to up my application with MySQL db in a docker container and access in Mysql workbench.
here is my docker-compose file
version: '3'
services:
app:
container_name: cth-app
build:
context: .
dockerfile: Dockerfile
ports:
- '8080:8080'
environment:
##db1
CTH_DATASOURCE_DATABASE: CTH
CTH_DATASOURCE_HOST: mysqldb
CTH_DATASOURCE_USERNAME: cth
CTH_DATASOURCE_PASSWORD: root
CTH_DATASOURCE_PORT: 3306
##db2
CTH_CHAT_DATASOURCE_DATABASENAME: CTH_CHAT
CTH_CHAT_DATASOURCE_HOST: mysqldb
CTH_CHAT_DATASOURCE_USERNAME: cth
CTH_CHAT_DATASOURCE_PASSWORD: root
CTH_CHAT_DATASOURCE_PORT: 3306
depends_on:
- mysqldb
mysqldb:
image: mysql/mysql-server:5.7
container_name: mysqldb
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
##here i am able to access dump file which I'll create two databases
with user created for me.
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
ports:
- '3308:3306'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: cth
MYSQL_PASSWORD: root
you need to create a dump file refer the article [ https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases]
when you'll docker-compose up -d --build
your application is up running on port 8080, with MySQL DB running inside a host machine
Thanks!
I have spring boot app with below Dockerfile
FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/demo-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} demo.jar
ENTRYPOINT ["java","-jar","/demo.jar"]
I can able to build docker image successfully
and this below is my docker-compose
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3.7'
# Define services
services:
# App backend service
app-server:
# Configuration for building the docker image for the backend service
build:
context: . # Use an image built from the specified dockerfile in the `polling-app-server` directory.
dockerfile: ./Dockerfile
container_name: empserver
ports:
- "3000:3000" # Forward the exposed port 8080 on the container to port 8080 on the host machine
restart: always
depends_on:
- db # This service depends on mysql. Start that first.
environment: # Pass environment variables to the service
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/employee_entries?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
# Database Service (Mysql)
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: employee_entries
MYSQL_HOST: 127.0.0.1
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_USER: root
Now I did
docker-compose up
Starting demo_db_1 ... error
ERROR: for demo_db_1 Cannot start service db: failed to create endpoint demo_db_1 on network demo_default: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
ERROR: for db Cannot start service db: failed to create endpoint demo_db_1 on network demo_default: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
ERROR: Encountered errors while bringing up the project.
Now as per one SO solution they said they need to stop services so I stop mysql service.
Then it run the docker-compose up successfully.
but it is not able to send data into mysql from my spring app
it gave below error as service stopped
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
empserver | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
empserver | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
empserver | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
empserver | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
empserver | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | ... 56 common frames omitted
empserver | Caused by: java.net.ConnectException: Connection refused (Connection refused)
empserver | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
empserver | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
empserver | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
empserver | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
empserver | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
empserver | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
empserver | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver | ... 59 common frames omitted
It seems that application is booting up before the database server is available. You need to wait for db to be available and then start the application server. There is a utility available to just do this kind of synchronization.
[https://github.com/ufoscout/docker-compose-wait]
Modify your dockerfile to include this script in it
FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/demo-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} demo.jar
## Add the wait script to the image
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
RUN chmod +x /wait
ENTRYPOINT ["java","-jar","/demo.jar"]
and with this you will have the wait script now part of your image and can be used as shown below:
Please pay attention to command and environment vars added to app-server section. I am currently using this exact set up where i have 4 services and 2 database running in docker compose and all services need to wait for both db to be available and 2 of the 4 services need to wait for another service to be available.
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3.7'
# Define services
services:
# App backend service
app-server:
# Configuration for building the docker image for the backend service
build:
context: . # Use an image built from the specified dockerfile in the `polling-app-server` directory.
dockerfile: ./Dockerfile
command: sh -c "/wait && java -jar /demo.jar"
container_name: empserver
ports:
- "3000:3000" # Forward the exposed port 8080 on the container to port 8080 on the host machine
restart: always
depends_on:
- db # This service depends on mysql. Start that first.
environment: # Pass environment variables to the service
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/employee_entries?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
WAIT_HOSTS: db:3306
# Database Service (Mysql)
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: employee_entries
MYSQL_HOST: 127.0.0.1
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_USER: root
network:
my-network: