1

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?

Cristian Monti
  • 145
  • 1
  • 2
  • 12

2 Answers2

3

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
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • it say: starting container process caused "exec: \"cd\": executable file not found in $PATH": like it don't see the folder in the container filesystem – Cristian Monti Apr 12 '18 at 15:46
  • @CristianMonti Updated answer. Try wrapping the command using bash: `bash -c "cd /usr/src/myapp && java -jar LogiIn.jar"`. Alternatively use `working_dir` property. – yamenk Apr 12 '18 at 15:47
  • i have this, could be the db? Starting dbContainer ... done Recreating mysqllogin_java_1 ... done Attaching to dbContainer, mysqllogin_java_1 . Please use --explicit_defaults_for_timestamp server option (see documentation for more details). dbContainer | 2018-04-12T15:53:25.475417Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. dbContainer | 2018-04-12T15:53:25.475433Z 0 [ERROR] Aborting dbContainer | dbContainer exited with code 1 mysqllogin_java_1 exited with code 1 – Cristian Monti Apr 12 '18 at 15:53
  • > --initialize specified but the data directory has files in it. The db is complaining that the data directory is not empty. This is due to the volume '/var/lib/mysql' already existing. Try cleaning the folder: `/home/crismon-01/Documenti/TESI/Docker/mysqlLogin` and then retry – yamenk Apr 12 '18 at 16:02
  • yes, now it do some more stuff, but it close all: dbContainer | 2018-04-12T16:06:57.332074Z 0 [Warning] InnoDB: New log files created, LSN=45790 java_1 | Error: Unable to access jarfile LogiIn.jar mysqllogin_java_1 exited with code 1 ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%' this is the error i see in the stacktrace of the shell – Cristian Monti Apr 12 '18 at 16:08
0

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/

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113