algorandfoundation / algokit-cli

The Algorand AlgoKit CLI is the one-stop shop tool for developers building on the Algorand network.
MIT License
167 stars 44 forks source link

[Localnet] add support for starting localnet onto an existing external docker network #581

Closed ScovottoDavide closed 11 minutes ago

ScovottoDavide commented 21 hours ago

Problem

It would be nice to have the possibility to deploy the localnet instance onto an already existing external docker network. This could be helpful when locally running/testing microservices that are all on the same docker network

Solution

Add a new click option to the localnet start and localnet reset commands that accept the name of the external_network. I've forked the project and propose a solution that can be found here: Proposal

Proposal

If the external_network option is provided:

  1. build the ComposeSandbox by passing the provided network and modify the docker_compose_yaml accordingly. The yaml file is modified using the PyYAML library.
  2. check that the docker network actually exists (added ComposeSandbox::_run_docker_command)
  3. if the ComposeFileStatus == OUT_OF_DATE, write to file the new yaml file
  4. sandbox.up()

Pros and Cons

Pros: possibility to don't rely only on the default docker network created by default, but also leverage already existing ones

Dependencies

None

Best regards.

neilcampbell commented 6 hours ago

Thanks @ScovottoDavide for putting together a super detailed example! We actually have a feature already existing that should support the outcome you're trying to achieve.

You can supply either a name or config directory to the start command, which allows you to obtain a copy of the LocalNet setup that can be customise in any way that you desire. Here's the docs for it. https://github.com/algorandfoundation/algokit-cli/blob/main/docs/features/localnet.md#named-localnet-configuration-directory

In practice what you'd do is run algokit localnet start --name custom-localnet (use whatever name you like). That will clone the localnet configuration and dump it to disk in the default configuration directory. You can then customise the configuration (including the compose file) to do anything you'd like. You can then restart the containers and everything should work as you'd like. To start that named localnet again, you run the above command again.

Keen to hear if this works for your scenario and shout if you have any questions/issues.

ScovottoDavide commented 4 hours ago

@neilcampbell thanks for the quick and detailed response.

I was testing stuff on various networks, and being a lazy person I just added a new option to the localnet command so that I could do it quicker than manually modifying the compose yml :).

I apologize for bothering, and thanks again for your time.

neilcampbell commented 11 minutes ago

@ScovottoDavide No worries at all, happy to help!