Not able to connect with MySQL server to Spring Application in Docker? - docker

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!

Related

getting connection refused from docker containers in a same docker compose

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

docker compose up gving error as I need to stop mysql service ERROR: for demo_db_1 Cannot start

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:

Asp.Net Core docker-compose https self signed certificate issue

I am not able to run docker-compose for an asp.net core 3 api on https with a self signed cert. I have followed the instructions on ms docs but I have given up at this point after trying everything for hours:
https://learn.microsoft.com/en-us/aspnet/core/security/docker-https?view=aspnetcore-2.2
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust
My docker compose is here:
version: '3.7'
networks:
localdev:
name: localdev
services:
main-api:
container_name: main-api
build:
context: .
dockerfile: Dockerfile
#restart: always
ports:
- "5000:5000"
- "5001:5001"
depends_on:
- db-server
networks:
- localdev
volumes:
- $USERPROFILE/.aspnet/https:/https/
environment:
ASPNETCORE_Kestrel__Certificates__Default__Password: "Passw0rd!"
ASPNETCORE_Kestrel__Certificates__Default__Path: "$USERPROFILE/.aspnet/https/aspnetapp.pfx"
db-server:
image: mariadb:latest
container_name: db-server
environment:
- MYSQL_ROOT_PASSWORD=Password!
ports:
- "13306:3306"
networks:
- localdev
docker-compose log is here:
main-api | warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
main-api | Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
main-api | crit: Microsoft.AspNetCore.Server.Kestrel[0]
main-api | Unable to start Kestrel.
main-api | Interop+Crypto+OpenSslCryptographicException: error:2006D080:BIO routines:BIO_new_file:no such file
main-api | at Interop.Crypto.CheckValidOpenSslHandle(SafeHandle handle)
main-api | at Internal.Cryptography.Pal.OpenSslX509CertificateReader.FromFile(String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
main-api | at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
main-api | at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
main-api | at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.LoadCertificate(CertificateConfig certInfo, String endpointName)
main-api | at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.LoadDefaultCert(ConfigurationReader configReader)
main-api | at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.Load()
main-api | at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.ValidateOptions()
main-api | at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
main-api exited with code 0
$ ls -l %USERPROFILE%\.aspnet\https\aspnetapp.pfx
-rw-r--r-- 1 tig28 197609 2652 Dec 17 23:07 %USERPROFILE%.aspnethttpsaspnetapp.pfx
Using Linux syntax:
$ ls -l $USERPROFILE/.aspnet/https/aspnetapp.pfx
-rw-r--r-- 1 tig28 197609 2652 Dec 17 11:15 'C:\Users\tig28/.aspnet/https/aspnetapp.pfx'
Minor change to your docker-compose file, here path will be your mounted path for the container /root/.aspnet/https/ApiHost.pfx.
environment:
ASPNETCORE_HTTPS_PORT: 6001
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_Kestrel__Certificates__Default__Path:/root/.aspnet/https/ApiHost.pfx
ASPNETCORE_Kestrel__Certificates__Default__Password: <password>
volumes:
- ${USERPROFILE}\.aspnet\https:/root/.aspnet/https
If your project uses .net 6 then local path to map to volume as shown below
Reference: Hosting ASP.NET Core images with Docker Compose over HTTPS
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust
version: '3.4'
services:
webapp:
image: mcr.microsoft.com/dotnet/core/samples:aspnetapp
ports:
- 80
- 443
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_Kestrel__Certificates__Default__Password=<password>
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
volumes:
- ~/.aspnet/https:/https:ro

Core dumped in running Mesos cluster on docker

I have a docker image called ubuntu_mesos_spark. I installed zookeeper on it. I change “zoo.cfg” file like this:
This is “zoo.cfg” in node1(150.20.11.157)
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2187
dataDir=/var/lib/zookeeper
server.1=0.0.0.0:2888:3888
server.2=150.20.11.157:2888:3888
server.3=150.20.11.137:2888:3888
This is “zoo.cfg” in node1(150.20.11.134)
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2187
dataDir=/var/lib/zookeeper
server.1=150.20.11.157:2888:3888
server.2=0.0.0.0:2888:3888
server.3=150.20.11.137:2888:3888
This is “zoo.cfg” in node1(150.20.11.137)
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2187
dataDir=/var/lib/zookeeper
server.1=150.20.11.157:2888:3888
server.2=150.20.11.134:2888:3888
server.3=0.0.0.0:2888:3888
Also I made a “myid” file in “/var/lib/zookeeper” of each node. For example for “150.20.11.157” its ID is “1” in myid file.
I installed Mesos and Spark on the docker too. I have a Mesos cluster of these three nodes too. I defined IP address of slaves nodes on this file: “spark/conf/slaves”
150.20.11.134
150.20.11.137
I added these lines in “spark/conf/spark-env.sh”:
export MESOS_NATIVE_JAVA_LIBRARY=/usr/local/lib/libmesos.so
export SPARK_EXECUTOR_URI=/home/spark/program_file/spark-2.3.2-bin-
hadoop2.7.tgz
Moreover, I added these lines in my “~/.bashrc” file:
export SPARK_HOME="/home/spark"
PYTHONPATH=$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.10.7-
src.zip:$PYTHO$
export PYSPARK_HOME=/usr/bin/python3.6
export PYSPARK_DRIVER_PYTHON=python3.6
export ZOO_LOG_DIR=/var/log/zookeeper
I want to run master code in “150.20.11.157”.My docker-compose is :
version: '3.7'
services:
zookeeper:
image: ubuntu_mesos_spark
command: /zookeeper-3.4.12/bin/zkServer.sh start
environment:
ZOOKEEPER_SERVER_ID: 1
ZOOKEEPER_CLIENT_PORT: 2187
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_INIT_LIMIT: 10
ZOOKEEPER_SYNC_LIMIT: 5
ZOOKEEPER_SERVERS:
0.0.0.0:2888:3888;150.20.11.134:2888:3888;150.20.11.137:2888:3888
network_mode: host
expose:
- 2187
- 2888
- 3888
ports:
- 2187:2187
- 2888:2888
- 3888:3888
master:
image: ubuntu_mesos_spark
command: bash -c "sleep 20; /home/mesos-1.7.0/build/bin/mesos-
master.sh --ip=150.20.11.157 --work_dir=/var/run/mesos"
restart: always
depends_on:
- zookeeper
environment:
- MESOS_HOSTNAME="150.20.11.157,150.20.11.134,150.20.11.137"
- MESOS_QUORUM=1
- MESOS_LOG_DIR=/var/log/mesos
expose:
- 5050
- 4040
- 7077
- 8080
ports:
- 5050:5050
- 4040:4040
- 7077:7077
- 8080:8080
Also, I run this compose file on slaves nodes :“150.20.11.134,150.20.11.137”:
version: '3.7'
services:
zookeeper:
image: ubuntu_mesos_spark
command: /zookeeper-3.4.12/bin/zkServer.sh start
environment:
ZOOKEEPER_SERVER_ID: 2
ZOOKEEPER_CLIENT_PORT: 2187
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_INIT_LIMIT: 10
ZOOKEEPER_SYNC_LIMIT: 5
ZOOKEEPER_SERVERS:
0.0.0.0:2888:3888;150.20.11.134:2888:3888;150.20.11.137:2888:3888
network_mode: host
expose:
- 2187
- 2888
- 3888
ports:
- 2187:2187
- 2888:2888
- 3888:3888
slave:
image: ubuntu_mesos_spark
command: bash -c "/home/mesos-1.7.0/build/bin/mesos-slave.sh --
master=150.20.11.157:5050 --work_dir=/var/run/mesos
--systemd_enable_support=false"
restart: always
privileged: true
network_mode: host
depends_on:
- zookeeper
environment:
- MESOS_HOSTNAME="150.20.11.157,150.20.11.134,150.20.11.137"
- MESOS_MASTER=150.20.11.157
- MESOS_EXECUTOR_REGISTRATION_TIMEOUT=5mins #also in Dockerfile
- MESOS_CONTAINERIZERS=docker,mesos
- MESOS_LOG_DIR=/var/log/mesos
- MESOS_LOGGING_LEVEL=INFO
expose:
- 5051
ports:
- 5051:5051
First I run "sudo docker-compose up" on Master node. Then I run it on slaves nodes. But I get this error:
On Master node, the error is:
Starting marzieh-compose_zookeeper_1 ... done
Recreating marzieh-compose_master_1 ... done
Attaching to marzieh-compose_zookeeper_1, marzieh-compose_master_1
zookeeper_1 | ZooKeeper JMX enabled by default
zookeeper_1 | Using config: /zookeeper-3.4.12/bin/../conf/zoo.cfg
zookeeper_1 | Starting zookeeper ... STARTED
marzieh-compose_zookeeper_1 exited with code 0
master_1 | I0123 11:46:59.585522 7 logging.cpp:201] INFO level logging started!
master_1 | I0123 11:46:59.586066 7 main.cpp:242] Build: 2019-01-21 05:16:39 by
master_1 | I0123 11:46:59.586097 7 main.cpp:243] Version: 1.7.0
master_1 | F0123 11:46:59.587368 7 process.cpp:1115] Failed to initialize: Failed to bind on 150.20.11.157:5050: Cannot assign requested address
master_1 | * Check failure stack trace: *
master_1 | # 0x7f505ce54b9c google::LogMessage::Fail()
master_1 | # 0x7f505ce54ae0 google::LogMessage::SendToLog()
master_1 | # 0x7f505ce544b2 google::LogMessage::Flush()
master_1 | # 0x7f505ce57770
google::LogMessageFatal::~LogMessageFatal()
master_1 | # 0x7f505cd19ed1 process::initialize()
master_1 | # 0x55fb7b12981a main
master_1 | # 0x7f504f0d0830 (unknown)
master_1 | # 0x55fb7b1288b9 _start
master_1 | bash: line 1: 7 Aborted (core dumped) /home/mesos-1.7.0/build/bin/mesos-master.sh --ip=150.20.11.157 --work_dir=/var/run/mesos
Moreover when I run "sudo docker-compose up" on slave nodes. I got this error:
slave_1 | F0123 11:40:06.878793 1 process.cpp:1115] Failed to initialize: Failed to bind on 0.0.0.0:5051: Address already in use
slave_1 | * Check failure stack trace: *
slave_1 | # 0x7fee9d319b9c google::LogMessage::Fail()
slave_1 | # 0x7fee9d319ae0 google::LogMessage::SendToLog()
slave_1 | # 0x7fee9d3194b2 google::LogMessage::Flush()
slave_1 | # 0x7fee9d31c770
google::LogMessageFatal::~LogMessageFatal()
slave_1 | # 0x7fee9d1deed1 process::initialize()
slave_1 | # 0x55e99f661784 main
slave_1 | # 0x7fee8f595830 (unknown)
slave_1 | # 0x55e99f65f139 _start
slave_1 | * Aborted at 1548243606 (unix time) try "date -d #1548243606" if you are using GNU date *
slave_1 | PC: # 0x7fee8f5ac196 (unknown)
slave_1 | * SIGSEGV (#0x0) received by PID 1 (TID 0x7fee9f9f38c0) from PID 0; stack trace: *
slave_1 | # 0x7fee8fee8390 (unknown)
slave_1 | # 0x7fee8f5ac196 (unknown)
slave_1 | # 0x7fee9d32055b google::DumpStackTraceAndExit()
slave_1 | # 0x7fee9d319b9c google::LogMessage::Fail()
slave_1 | # 0x7fee9d319ae0 google::LogMessage::SendToLog()
slave_1 | # 0x7fee9d3194b2 google::LogMessage::Flush()
slave_1 | # 0x7fee9d31c770 google::LogMessageFatal::~LogMessageFatal()
slave_1 | # 0x7fee9d1deed1 process::initialize()
slave_1 | # 0x55e99f661784 main
slave_1 | # 0x7fee8f595830 (unknown)
slave_1 | # 0x55e99f65f139 _start
slave_1 | I0123 11:41:07.818897 1 logging.cpp:201] INFO level logging started!
slave_1 | I0123 11:41:07.819437 1 main.cpp:349] Build: 2019-01-21 05:16:39 by
slave_1 | I0123 11:41:07.819470 1 main.cpp:350] Version: 1.7.0
slave_1 | I0123 11:41:07.823354 1 resolver.cpp:69] Creating default secret resolver
slave_1 | E0123 11:41:07.927773 1 main.cpp:483] EXIT with status 1: Failed to create a containerizer: Could not create
DockerContainerizer: Failed to create docker: Failed to get docker version: Failed to execute 'docker -H unix:///var/run/docker.sock --
version': exited with status 127
I searched a lot about that and I could not figure this out. Would you please guide me what the right way is to write docker compose for running Mesos and Spark cluster on docker?
Any help would be appreciated.
Thanks in advance.
Problem solved. I changed docker compose like this and Master and Slaves run without problem:
"docker-compose.yaml" in Master node is in the following:
version: '3.7'
services:
zookeeper:
image: ubuntu_mesos_spark_python3.6_client
command: /home/zookeeper-3.4.12/bin/zkServer.sh start
environment:
ZOOKEEPER_SERVER_ID: 1
ZOOKEEPER_CLIENT_PORT: 2188
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_INIT_LIMIT: 10
ZOOKEEPER_SYNC_LIMIT: 5
ZOOKEEPER_SERVERS: 0.0.0.0:2888:3888;150.20.11.157:2888:3888
network_mode: host
expose:
- 2188
- 2888
- 3888
ports:
- 2188:2188
- 2888:2888
- 3888:3888
master:
image: ubuntu_mesos_spark_python3.6_client
command: bash -c "sleep 30; /home/mesos-1.7.0/build/bin/mesos-master.sh
--ip=150.20.10.136 --work_dir=/var/run/mesos --hostname=x.x.x.x" ##hostname :
IP of the master node
restart: always
network_mode: host
depends_on:
- zookeeper
environment:
- MESOS_HOSTNAME="150.20.11.136"
- MESOS_QUORUM=1
- MESOS_LOG_DIR=/var/log/mesos
expose:
- 5050
- 4040
- 7077
- 8080
ports:
- 5050:5050
- 4040:4040
- 7077:7077
- 8080:8080
Also,"docker-compose.yaml" file in slave node is like this:
version: '3.7'
services:
zookeeper:
image: ubuntu_mesos_spark_python3.6_client
command: /home/zookeeper-3.4.12/bin/zkServer.sh start
environment:
ZOOKEEPER_SERVER_ID: 2
ZOOKEEPER_CLIENT_PORT: 2188
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_INIT_LIMIT: 10
ZOOKEEPER_SYNC_LIMIT: 5
ZOOKEEPER_SERVERS: 150.20.11.136:2888:3888;0.0.0.0:2888:3888
network_mode: host
expose:
- 2188
- 2888
- 3888
ports:
- 2188:2188
- 2888:2888
- 3888:3888
slave:
image: ubuntu_mesos_spark_python3.6_client
command: bash -c "sleep 30; /home/mesos-1.7.0/build/bin/mesos-slave.sh
--master=150.20.11.136:5050 --work_dir=/var/run/mesos
--systemd_enable_support=false"
restart: always
privileged: true
network_mode: host
depends_on:
- zookeeper
environment:
- MESOS_HOSTNAME="150.20.11.157"
#- MESOS_MASTER=172.28.10.136
#- MESOS_EXECUTOR_REGISTRATION_TIMEOUT=5mins #also in Dockerfile
#- MESOS_CONTAINERIZERS=docker,mesos
- MESOS_LOG_DIR=/var/log/mesos
- MESOS_LOGGING_LEVEL=INFO
expose:
- 5051
ports:
- 5051:5051
Then I run "docker-compose up" in each node and they run without any problems.

How to stop a container to start before a dependency is "really" done?

I am trying to hold a container from start (ready to accept connection since it's Apache+PHP) before a dependency has finished. This is how my docker-compose.yml looks like:
version: '3'
services:
webserver:
image: reynierpm/docker-lamp
dns:
- 8.8.8.8
- 8.8.4.4
ports:
- "80:80"
volumes:
- f:/sources:/var/www/html
depends_on:
- mysqldb
- mongodb
restart: on-failure
environment:
SERVER_URL: 'localhost'
SERVER_SCHEME: 'http'
HTTP_PORT: '80'
mysqldb:
image: mysql:latest
healthcheck:
test: "exit 0"
interval: 1m30s
timeout: 10s
retries: 3
env_file: .env
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- f:/sources/db_dump:/docker-entrypoint-initdb.d
ports:
- "3306:3306"
restart: on-failure
mongodb:
image: mongo:latest
env_file: .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
ports:
- "27017:27017"
restart: on-failure
But the result from docker-compose up is as follow:
mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldb_1 |
mysqldb_1 |
mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/language.sql
mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldb_1 |
mysqldb_1 |
mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/license.sql
mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
webserver_1 | Enabling module rewrite.
webserver_1 | Enabling module expires.
webserver_1 | To activate the new configuration, you need to run:
webserver_1 | service apache2 restart
webserver_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.4. Set the 'ServerName' directive globally to suppress this message
mysqldb_1 |
mysqldb_1 |
mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/license_agreement.sql
mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldb_1 |
mysqldb_1 |
mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/license_forcast.sql
mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure.
As you can see in between of MySQL import process the webserver_1 container has started already and mysqldb_1 hasn't. If I try to access the application it will fails (blank page) because MySQL isn't ready to accept connections or read some needed tables because they're not created|imported yet.
How do I avoid this? What changes do I need to make in my docker-compose.yml to achieve it?
In your 'webserver' start-up script / entry point you could pass the sleep command or you could use: WAIT command as described here:
https://docs.docker.com/engine/reference/commandline/wait/

Resources