Published on

Getting Two or More Docker-Compose Files to Work Together

Authors

I have been working on a dApp recently that interacts with a permissioned chain smart contract. I needed to get the contract into a certain state and run a spring app that uses web3j to interact with it.

I automated as much as I could but could not fully automate the deployment of the contract into the permissioned node, so I ended up using 2 docker-compose files.

The idea is simple:

  • Deploy the first docker-compose file which deploys the permissioned chain infrastructure locally
    • I could use Ganache in theory but I wanted to specifically use the permissioned chain infrastructure in case there are nuances I miss with Ganache
  • Migrate the contracts to the instance
  • Change the contract addresses in the environment variables passed to the spring boot app in the second compose file
  • Run the second compose file and use the dApp to test if my bug is fixed

An example of the first file which I called docker-compose.yaml is below:

version: '3.7'

services:
  chain-node:
    image: permissioned-chain-image
    ports:
      - '8545:8545'
    networks:
      - blockchain-network
    volumes:
      - 'chain-data:/path/on/permissioned/chain/instance/to/data'

volumes:
  chain-data:

networks:
  blockchain-network:

This can be started by CDing to the directory and running: docker-compose up -d

For the second file I named it dapps-docker-compose.yaml and it looked something like this:

version: '3.7'

services:
  dapp-1:
    image: my-dapp:0.1
    depends_on:
      - 'queue-1'
    ports:
      - '8080:8080'
    networks:
      - chain-network
    environment:
      # notice how I can still refer to the chain service name from the first file
      - bc.node.endpoint=http://chain-node:8545
      - bc.contract.myawesomecontract.address=0x9a3DECa554e9f6c9257aBa24010CB8377C57a17e
  dapp-2:
    image: my-dapp:0.1
    depends_on:
      - 'queue-2'
    ports:
      - '8081:8080'
    networks:
      - chain-network
    environment:
      - bc.node.endpoint=http://chain-node:8545
      - bc.contract.myawesomecontract.address=0x9a3DECa554e9f6c9257aBa24010CB8377C57a17e
  queue-1:
    image: rmohr/activemq
    networks:
      - chain-network
    ports:
      - '61616:61616'
      - '8161:8161'
  queue-2:
    image: rmohr/activemq
    networks:
      - chain-network
    ports:
      - '61617:61616'
      - '8162:8161'

networks:
  chain-network:
    external:
      name: testing_blockchain-network

It is important to note that the first compose file had been running for a few minutes before this one (it can run for as long as you want before running this one).

To run this file I CDed to the directory with this file and run: docker-compose -f ./infrastructure/testing/dapps-docker-compose.yaml up -d

While this is not the simplest setup it ended up being much faster than running this infrastructure manually. I also could have used the compose scale command but in my case, I only wanted 2 versions of the dapp running with very specific configurations. These specific configurations are very difficult or potentially impossible to tweak per instance with docker-compose.