Corda in Docker OutOfMemoryError: Java heap space

When I spin up my docker containers using docker-compose, I get the following error:

partyb     | Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
partyb     |    at java.util.Arrays.copyOf(Arrays.java:3236)
partyb     |    at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:191)
partyb     |    at kotlin.io.ByteStreamsKt.readBytes(IOStreams.kt:123)
partyb     |    at kotlin.io.ByteStreamsKt.readBytes$default(IOStreams.kt:120)
partyb     |    at net.corda.core.internal.InternalUtils.readFully(InternalUtils.kt:122)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader.getJarHash(JarScanningCordappLoader.kt:224)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader.toCordapp(JarScanningCordappLoader.kt:151)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader.loadCordapps(JarScanningCordappLoader.kt:104)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader.access$loadCordapps(JarScanningCordappLoader.kt:42)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader$cordapps$2.invoke(JarScanningCordappLoader.kt:54)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader$cordapps$2.invoke(JarScanningCordappLoader.kt:42)
partyb     |    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
partyb     |    at net.corda.node.internal.cordapp.JarScanningCordappLoader.getCordapps(JarScanningCordappLoader.kt)
partyb     |    at net.corda.node.internal.cordapp.CordappLoaderTemplate$cordappSchemas$2.invoke(JarScanningCordappLoader.kt:400)
partyb     |    at net.corda.node.internal.cordapp.CordappLoaderTemplate$cordappSchemas$2.invoke(JarScanningCordappLoader.kt:386)
partyb     |    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
partyb     |    at net.corda.node.internal.cordapp.CordappLoaderTemplate.getCordappSchemas(JarScanningCordappLoader.kt)
partyb     |    at net.corda.node.internal.AbstractNode.<init>(AbstractNode.kt:153)
partyb     |    at net.corda.node.internal.AbstractNode.<init>(AbstractNode.kt:126)
partyb     |    at net.corda.node.internal.Node.<init>(Node.kt:97)
partyb     |    at net.corda.node.internal.Node.<init>(Node.kt:96)
partyb     |    at net.corda.node.internal.NodeStartup.createNode(NodeStartup.kt:170)
partyb     |    at net.corda.node.internal.NodeStartup$initialiseAndRun$5.invoke(NodeStartup.kt:162)
partyb     |    at net.corda.node.internal.NodeStartup$initialiseAndRun$5.invoke(NodeStartup.kt:117)
partyb     |    at net.corda.node.internal.NodeStartupLogging$DefaultImpls.attempt(NodeStartup.kt:450)
partyb     |    at net.corda.node.internal.NodeStartup.attempt(NodeStartup.kt:117)
partyb     |    at net.corda.node.internal.NodeStartup.initialiseAndRun(NodeStartup.kt:160)
partyb     |    at net.corda.node.internal.NodeStartupCli.runProgram(NodeStartup.kt:108)
partyb     |    at net.corda.cliutils.CordaCliWrapper.call(CordaCliWrapper.kt:184)
partyb     |    at net.corda.cliutils.CordaCliWrapper.call(CordaCliWrapper.kt:152)
partyb     |    at picocli.CommandLine.execute(CommandLine.java:1056)
partyb     |    at picocli.CommandLine.access$900(CommandLine.java:142)

(the error is shown for partyb, but it is the same error for all nodes but the notary). Obviously my CorDapp is too big.

I read some articles on Java in docker container and tried to configure them accordingly, yet unsuccessfully so far.

I tried to access the Java option UseCGroupMemoryLimitForHeap and initialized docker swarm to use the resources configuration, but it didn’t work out.

Here is the compose file, without any swarm configuration:

version: '3.7'
services:
  notary:
    image: corda/corda-zulu-4.0:latest
    container_name: notary
    networks:
      - corda
    volumes:
      - ./nodes/notary_node.conf:/etc/corda/node.conf
      - ./nodes/notary/certificates:/opt/corda/certificates
      - ./nodes/notary/network-parameters:/opt/corda/network-parameters
      - ./nodes/notary/additional-node-infos:/opt/corda/additional-node-infos

  partya:
    image: corda/corda-zulu-4.0:latest
    container_name: partya
    networks:
      - corda
    volumes:
      - ./nodes/partya_node.conf:/etc/corda/node.conf
      - ./nodes/partya/certificates:/opt/corda/certificates
      - ./nodes/partya/network-parameters:/opt/corda/network-parameters
      - ./build/libs/:/opt/corda/cordapps
      - ./nodes/partya/additional-node-infos:/opt/corda/additional-node-infos

  partyb:
    image: corda/corda-zulu-4.0:latest
    container_name: partyb
    networks:
      - corda
    volumes:
      - ./nodes/partyb_node.conf:/etc/corda/node.conf
      - ./nodes/partyb/certificates:/opt/corda/certificates
      - ./nodes/partyb/network-parameters:/opt/corda/network-parameters
      - ./build/libs/:/opt/corda/cordapps
      - ./nodes/partyb/additional-node-infos:/opt/corda/additional-node-infos

  oracle:
    image: corda/corda-zulu-4.0:latest
    container_name: oracle
    networks:
      - corda
    volumes:
      - ./nodes/oracle_node.conf:/etc/corda/node.conf
      - ./nodes/oracle/certificates:/opt/corda/certificates
      - ./nodes/oracle/network-parameters:/opt/corda/network-parameters
      - ./build/libs/:/opt/corda/cordapps
      - ./nodes/oracle/additional-node-infos:/opt/corda/additional-node-infos

  clientA:
    image: cordaclient:latest
    networks:
      - corda
    container_name: clientA
    environment:
      - USER=XXX
      - PASSWORD=XXX
      - NETWORK_HOST_AND_RPC_ADDRESS=XXX
    depends_on:
      - partya
    ports:
      - "8090:8080"

  clientB:
    image: cordaclient:latest
    networks:
      - corda
    container_name: clientB
    environment:
      - USER=XXX
      - PASSWORD=XXX
      - NETWORK_HOST_AND_RPC_ADDRESS=XXX
    depends_on:
      - partyb
    ports:
      - "8070:8080"

networks:
  corda:

Thank you in advance for any help!

Source: StackOverflow