Specifying JVM Options in docker-compose File

Currently I am trying to pass JVM options to my docker-compose.yml file. And this JVM_OPTS part in 'environment:' doesn't seem to be working. Is there another way to pass JVM options to docker-compose.yml file?

And also my DockerFile image is FROM openjdk:8-jre-alpine.

And my docker-compose.yml file is like this.

version: '3.1'
services:
  service:
    image: registry.gitlab.com/project/service/${BRANCH}:${TAG}
    container_name: serviceApp
    env_file: docker-compose.env
    environment:
      - JVM_OPTS=-XX:NativeMemoryTracking=summary
                 -XX:+StartAttachListener
                 -XX:+UseSerialGC
                 -Xss512k
                 -Dcom.sun.management.jmxremote.rmi.port=8088
                 -Dcom.sun.management.jmxremote=true
                 -Dcom.sun.management.jmxremote.port=8088
                 -Dcom.sun.management.jmxremote.ssl=false
                 -Dcom.sun.management.jmxremote.authenticate=false
                 -Dcom.sun.management.jmxremote.local.only=false
                 -Djava.rmi.server.hostname=localhost
    ports:
      - 8088:8088
    networks:
      - services
    working_dir: /opt/app
    command: ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/service.jar""]

networks:
  services:
    external:
      name: services

And If you ask of these arguments, I am trying to connect VisualVM to a local docker container.

Upvotes: 10

Views: 16127

Answers (1)

Metin
Metin

Reputation: 741

Switching the environment declaration from sequence style to value-map style allows to use the YAML multiline string operator '>'. It will merge all lines to a single line.

version: '3.1'
services:
  service:
    image: registry.gitlab.com/project/service/${BRANCH}:${TAG}
    container_name: serviceApp
    env_file: docker-compose.env
    environment:
      JVM_OPTS: >
        -XX:NativeMemoryTracking=summary
        -XX:+StartAttachListener
        -XX:+UseSerialGC
        -Xss512k
        -Dcom.sun.management.jmxremote.rmi.port=8088
        -Dcom.sun.management.jmxremote=true
        -Dcom.sun.management.jmxremote.port=8088
        -Dcom.sun.management.jmxremote.ssl=false
        -Dcom.sun.management.jmxremote.authenticate=false
        -Dcom.sun.management.jmxremote.local.only=false
        -Djava.rmi.server.hostname=localhost

    ports:
        - 8088:8088
    networks:
        - services
    working_dir: /opt/app
    command: ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/service.jar""]

networks:
  services:
    external:
    name: services

Upvotes: 8

Related Questions