run java in docker-compose - java

this is my docker-compose file.yaml:
version: '3.3'
services:
db:
container_name: dbContainer
image: mysql:5.7
volumes:
- /home/crismon-01/Documenti/TESI/Docker/mysqlLogin/datas:/var/lib/mysql
ports:
- 3306
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
MYSQL_DATABASE: "schema1"
java:
container_name: loginJava
image: openjdk:7
depends_on:
- db
volumes:
- ./home/crismon-01/Documenti/TESI/Docker/mysqlLogin:/usr/src/myapp
working_dir: /usr/src/myapp
command: bash -c "java -jar LogiIn.jar"
it is a compose with two cotnainer one with mysql and one with javacode that use the db, now i need to run it, and i have this error:
crismon-01#crismon01-XPS15:~/Documenti/TESI/Docker/mysqlLogin$ docker-compose up
Starting dbContainer ... done
Starting mysqllogin_java_1 ... done
Attaching to dbContainer, mysqllogin_java_1
dbContainer | Initializing database
dbContainer | 2018-04-12T15:35:07.134004Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
dbContainer | 2018-04-12T15:35:07.135231Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
dbContainer | 2018-04-12T15:35:07.135247Z 0 [ERROR] Aborting
dbContainer |
java_1 | Error: Unable to access jarfile LogiIn.jar
dbContainer exited with code 1
mysqllogin_java_1 exited with code 1
could someone have idea of the dource of error?

The problem is that you are specifying command sections in the compose section of the java service. Only appears to be taken, which is the last one.
The solution is to group both commands into one command as such
java:
image: openjdk:7
depends_on:
- db
volumes:
- /home/crismon-01/Documenti/TESI/Docker/mysqlLogin:/usr/src/myapp
command: bash -c "cd /usr/src/myapp && java -jar LogiIn.jar"
Take a look at Using Docker-Compose, how to execute multiple commands for more info.
Alternatively, you can only set working_dir property and remove the cd command.
volumes:
- /home/crismon-01/Documenti/TESI/Docker/mysqlLogin:/usr/src/myapp
working_dir: /usr/src/myapp
command: java -jar LogiIn.jar

Testcontainers library has support for Docker Compose
Quoting official documentation
A single class rule, pointing to a docker-compose.yml file, should be sufficient to launch any number of services required by your tests:
#ClassRule public static DockerComposeContainer environment =
new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
.withExposedService("redis_1", REDIS_PORT)
.withExposedService("elasticsearch_1", ELASTICSEARCH_PORT);
In this example, compose-test.yml should have
content such as:
redis: image: redis elasticsearch: image: elasticsearch
For more details see official documentation
https://www.testcontainers.org/modules/docker_compose/

Related

IntelliJ and Docker: Create a remote debug configuration: custom command

I am trying to run my java application (a project at my uni) in debug mode in IntelliJ with a dockerfile.
I found this tutorial:
https://www.jetbrains.com/help/idea/debug-a-java-application-using-a-dockerfile.html#create-remote-debug-config
I tried to follow each step (#create-remote-debug-configuration), but at 4:
Select the Docker configuration that runs your app (MyPassApp) and
specify the command to use when running your app in the Custom Command
field. The remote debug configuration will use this custom command
instead of the one defined in the Dockerfile. This command should
contain the -agentlib option to let the debugger attach to the
process:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 -jar JavaPassFromConsole.jar
I don't know what any of those commands mean, so I dont know what to put in the custom command section.
This is my docker-compose.yml file:
version: '3'
volumes: mysql_data: {}
networks:
back:
services:
backend:
build:
context: '.'
dockerfile: 'docker_config/backend/Dockerfile'
depends_on:
- db
links:
- db
ports:
- 8080:8080
- 9990:9990
environment:
CONTACT_USERNAME: <XXX>
CONTACT_PASSWORD: <XXX>
networks:
- back db:
container_name: db
image: mysql:5.7
restart: always
volumes:
- mysql_data:/var/lib/mysql
command: --lower_case_table_names=1
environment:
MYSQL_DATABASE: <XXX>
MYSQL_ROOT_PASSWORD: root
networks:
- back
ports:
- 3306:3306
- 5005:5005 phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 4000:80
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- back
Any help is appreciated!!

How do I create a docker container with two Java applications and Mysql server?

I have a Java application consuming binlogs from Mysql database and interacting with another service which I have hosted on my localhost. How do I create a Docker image with all these services so that I can run them all at once?
From what I've heard, I need to use docker-compose. But I don't really know how to?
Do I create separate Docker containers and link their ports?
Do I create one container?
I have created one Dockerfile for the Java application:
FROM maslick/minimalka:jdk11
WORKDIR /app
EXPOSE 8080
COPY target/foster-maxwell-uber-jar.jar foster-maxwell.jar
COPY config.properties config.properties
CMD java -jar foster-maxwell.jar --config_type MAXWELL_UNSIGNED_KAAS --kaas_config config.yml --zk_timeout 1000
How do I go about it? Sorry for the noob question.
You should read docker-compose documentation: https://docs.docker.com/compose/compose-file/
For example, try with a docker-compose.yml file like this
version: '3.8'
services:
your-java-app1:
build:
context: . <-- (dir where you have your Dockerfile)
dockerfile: Dockerfile
image: yourjavaapp1image:1.0
container_name: your-java-app1
depends_on:
- db
your-java-app2:
build:
context: . <-- (dir where you have your Dockerfile_for_app2)
dockerfile: Dockerfile_for_app2
image: yourjavaapp2image:1.0
container_name: your-java-app2
depends_on:
- db
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: [put here your root pass]
MYSQL_DATABASE: [put here your db name]
MYSQL_USER: [put here your user]
MYSQL_PASSWORD: [put here your pass]
Once you have this defined your Dockerfiles, first, build images with your apps:
docker-compose build
Then, run images (container creations)
docker-compose up -d

Connect local container docker

I have a docker-compose configuration file with the following settings:
version: '3.6'
services:
mongo:
image: mongo
restart: always
ports:
- "27017:27017"
networks:
- devnetwork
discovery:
image: discovery
ports:
- "8761:8761"
networks:
- devnetwork
environment:
- SPRING_PROFILES_ACTIVE=dev
- DISCOVERY_SERVER=discovery
- DISCOVERY_PORT=8761
networks:
devnetwork:
After up compose I build and run the following dockerfile:
FROM openjdk:12
#RUN echo $(grep $(hostname) /etc/hosts | cut -f1) api >> /etc/hosts
ADD gubee-middleware/target/*.jar /usr/share/middlewareservice/middleware-service.jar
EXPOSE 9670
ENTRYPOINT ["/usr/bin/java", "-XX:+UnlockExperimentalVMOptions","-XX:+UseContainerSupport","-Dspring.profiles.active=dev", "-jar", "/usr/share/middlewareservice/middleware-service.jar"]
How do I make my port 9670 service dockerfile see other services that I start with docker-compose?
You need to attach the container to the network created by docker compose when you start it.
This can be done by passing --network=<network-name> (obtain by running docker network ls ) when executing your docker run command.
Once attached, your app will be able to reach the containers started by compose using their names as the host. Eg: http://mongo:27017

docker-compose java application connection to mongodb

2 Containers, one Java application and the second mongodb.
If I run my java app locally and mongodb in a container, it connects but if both run inside a container, java app can't connect to mongodb.
docker-compose file is as follows, am I missing something
version: "3"
services:
user:
image: jboss/wildfly
container_name: "user"
restart: always
ports:
- 8081:8080
- 65194:65193
volumes:
- ./User/target/User.war:/opt/jboss/wildfly/standalone/deployments/User.war
environment:
- JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:65193,suspend=n,server=y -Djava.net.preferIPv4Stack=true
- MONGO_HOST=localhost
- MONGO_PORT=27017
- MONGO_USERNAME=myuser
- MONGO_PASSWORD=mypass
- MONGO_DATABASE=mydb
- MONGO_AUTHDB=admin
command: >
bash -c "/opt/jboss/wildfly/bin/add-user.sh admin Admin#007 --silent && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"
links:
- mongo
mongo:
image: mongo:4.0.10
container_name: mongo
restart: always
volumes:
- ./assets:/docker-entrypoint-initdb.d/
environment:
- MONGO_INITDB_ROOT_USERNAME=myuser
- MONGO_INITDB_ROOT_PASSWORD=mypass
ports:
- 27017:27017
- 27018:27018
- 27019:27019
Edit
I'm also confused about the following.
links:
- mongo
depends_on:
- mongo
At 2019 July, official docker documentation :
Source: https://docs.docker.com/compose/compose-file/#links
Solution #1 : environment file before start
Basically We centralize all configurations in a file with environment variables and execute it before docker-compose up
The following approach helped me in these scenarios:
Your docker-compose.yml has several containers with complex dependencies between them
Some of your services in your docker-compose needs to connect to another process in the same machine. This process could be a docker container or not.
You need to share variables between several docker-compose files like host, passwords, etc
Steps
1.- Create one file to centralize configurations
This file could be named: /env/company_environments with extension or not.
export MACHINE_HOST=$(hostname -I | awk '{print $1}')
export GLOBAL_LOG_PATH=/my/org/log
export MONGO_PASSWORD=mypass
export MY_TOKEN=123456
2.- Use the env variables in your docker-compose.yml
container A
app_who_needs_mongo:
environment:
- MONGO_HOST=$MACHINE_HOST
- MONGO_PASSWORD=$MONGO_PASSWORD
- TOKEN=$MY_TOKEN
- LOG_PATH=$GLOBAL_LOG_PATH/app1
container B
app_who_needs_another_db_in_same_host:
environment:
- POSTGRESS_HOST=$MACHINE_HOST
- LOG_PATH=$GLOBAL_LOG_PATH/app1
3.- Startup your containers
Just add source before docker-compose commands:
source /env/company_environments
docker-compose up -d
Solution #2 : host.docker.internal
https://stackoverflow.com/a/63207679/3957754
Basically use a feature of docker in which host.docker.internal could be used as the ip of the server in which your docker-compose has started several containers
You probably cant connect because you set the MONGO_HOST as localhost and mongo is a linked service.
In order to use linked services network, you must specify the MONGO_HOST as the name of the service - mongo, like that:
MONGO_HOST=mongo

Docker not catching env vars

I'm trying to run 3 containers through docker-compose, with postgres, cassandra and my webapp, which has a embedded tomcat server with some dependencies as ARP/Native. This libraries are located in a folder called "lib" at jar's same level.
I'm running a PoC on Windows 10 (using Linux containers) before moving it to a CentOS server, if it works on the PoC. I searched over the net and seems like is not an isolated problem but or I have no find the solution, or the solution showed didn't work for me. Here is my docker-compose.yml, with all the related files/folders stored at same level:
version: '3.1'
services:
fulmar-webapp:
container_name: "my-webapp"
image: openjdk:11-jre-slim
hostname: mywebapp
volumes:
- ./lib:/home/lib
- ./fulmar-1.0.1-SNAPSHOT-exec.jar:/home/mywebapp-1.0.1-SNAPSHOT-exec.jar
entrypoint:
- java
- -jar
- /home/mywebapp-1.0.1-SNAPSHOT-exec.jar
environment:
- LD_LIBRARY_PATH:/home/lib
- spring.datasource.url=jdbc:postgresql://postgresql:5432/mydb
- spring.datasource.username=postgres
- spring.datasource.password=postgres
- spring.jpa.hibernate.ddlAuto=update
network_mode: bridge
ports:
- 8443:8443
- 8080:8080
links:
- postgresql
- cassandra
postgresql:
container_name: "mydb"
image: postgres:11.1-alpine
restart: always
environment:
POSTGRES_DB: mydb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql
- postgresdata:/var/lib/postgresql/data
ports:
- 5432:5432
network_mode: bridge
cassandra:
container_name: "cassandra"
image: cassandra
ports:
- 9042:9042
network_mode: bridge
volumes:
postgresdata:
Not sure if is not properly mapping the folder with the libraries, or is not actually mounting the volume. This is exactly the Environment var I need to put in there:
Environment=LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/libraries/lib
Two results I have encountered:
1-Tomcat exception cause it can not find the libraries:
[ERROR][org.apache.catalina.util.LifecycleBase#log(175)] Failed to
initialize component [Connector[org.apache.coyote.http11.Http11AprProtocol-
8080]] | org.apache.catalina.LifecycleException: The configured protocol
[org.apache.coyote.http11.Http11AprProtocol] requires the APR/native library
which is not available
2-WARNING: The LD_LIBRARY_PATH variable is not set. Defaulting to a blank string.
Thanks you all in advance
EDIT: just let you know the once I run docker-compose up, and my app throws this exception, the container is no longer available so I'm unable to run any commands in it
You have a wrong syntax, it should be like this:
environment:
- LD_LIBRARY_PATH=/home/lib

Categories

Resources