Skip to content Skip to sidebar Skip to footer

Run Java In Docker-compose

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/

Solution 1:

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

Solution 2:

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:

@ClassRulepublicstaticDockerComposeContainerenvironment=newDockerComposeContainer(newFile("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/

Post a Comment for "Run Java In Docker-compose"