10

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.

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

How do you do this if there are already sequential options in environment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.