Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start network docker container with cli #1107

Merged
merged 60 commits into from
Mar 4, 2024

Conversation

elizabethengelman
Copy link
Contributor

@elizabethengelman elizabethengelman commented Dec 4, 2023

What

This PR adds the network start and network stop subcommands to allow users to start and stop the stellar docker container using the CLI.

Start help output

$ soroban network start --help
Start network

This command allows for starting a stellar quickstart container. To run it, you can use the following command: `soroban network start <NETWORK> [OPTIONS]`

By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable-soroban-rpc

Usage: soroban network start [OPTIONS] <NETWORK>

Arguments:
  <NETWORK>
          Network to start

          [possible values: local, testnet, futurenet, pubnet]

Options:
  -d, --docker-socket-path <DOCKER_SOCKET_PATH>
          Optional argument to override the default docker socket path. This is useful when you are using a non-standard docker socket path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock

  -l, --limit <LIMIT>
          Optional argument to specify the limits for the local network only

  -p, --ports-mapping <PORTS_MAPPING>...
          Argument to specify the HOST_PORT:CONTAINER_PORT mapping

          [default: 8000:8000]

  -r, --disable-soroban-rpc
          Optional argument to turn off soroban rpc

  -t, --image-tag-override <IMAGE_TAG_OVERRIDE>
          Optional argument to override the default docker image tag for the given network

  -v, --protocol-version <PROTOCOL_VERSION>
          Optional argument to specify the protocol version for the local network only

  -h, --help
          Print help (see a summary with '-h')

Stop help output

$ soroban network stop --help
Stop network

Usage: soroban network stop [OPTIONS] <NETWORK>

Arguments:
  <NETWORK>  Network to stop [possible values: local, testnet, futurenet, pubnet]

Options:
  -d, --docker-socket-path <DOCKER_SOCKET_PATH>
          Optional argument to override the default docker socket path. This is useful when you are using a non-standard docker socket path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock
  -h, --help
          Print help

Output when the command is not able to connect to the docker daemon

$ soroban network start testnet
ℹ️  Starting testnet network
⛔️ Failed to connect to the Docker daemon at "/var/run/docker.sock". Is the docker daemon running?
ℹ️  Running a local Stellar network requires a Docker-compatible container runtime.
ℹ️  Please note that if you are using Docker Desktop, you may need to utilize the `--docker-socket-path` flag to pass in the location of the docker socket on your machine.

error: ⛔ ️Failed to start container: error trying to connect: No such file or directory (os error 2)

Output when the command starts the container successfully

ℹ️  Starting testnet network
✅ Container started: stellar-testnet
ℹ️  To stop this container run: soroban network stop testnet --docker-socket-path /Users/elizabethengelman/.docker/run/docker.sock

Why

To make it easier for users to run a stellar node for soroban development.

Known limitations

I learned that when using Docker Desktop, it starts the docker daemon on a non-standard socket 😵‍💫 . The Docker Desktop UI allows a user to overwrite this, in an advanced setting. But I figured allowing the user to do this all via the CLI would probably be easier, so I added the --docker-socket-path flag. I think that it may be helpful to allow the user to set an env var for this, instead of always passing a flag, but I'll plan to allow for this in a followup PR.

resources that helped identify this:
- fussybeaver/bollard#345 (suggestion of using DOCKER_HOST env var did not work)
- https://docs.docker.com/desktop/mac/permission-requirements/
- https://devops.stackexchange.com/questions/17905/ctop-does-not-work-with-latest-version-of-docker-get-http-unix-sock-info-d#:~:text=This%20is%20probably%20because%20Docker,sock%20anymore.&text=If%20you%20want%20to%20have,used%20and%20press%20Apply%20%26%20restart%20.

Todo

To handle in a followup PR

@willemneal
Copy link
Member

Looks great! Just one nit about using expect vs using an error. The error lets you capture the failed command.

You can use .map_error(|_| Error::CommandFailed(docker_command.clone()))?. This let's you use your own error and the inner anonymous function only executes if there was an error.

@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch 2 times, most recently from 80d10a4 to 3983924 Compare December 7, 2023 15:52
@mollykarcher mollykarcher added the cli Related to Soroban CLI label Dec 14, 2023
@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch from 3983924 to 1ab08a7 Compare January 5, 2024 17:38
@elizabethengelman elizabethengelman marked this pull request as ready for review January 12, 2024 22:10
@elizabethengelman elizabethengelman changed the title Feat/start network docker container with cli feat: start network docker container with cli Jan 17, 2024
Copy link
Contributor

@chadoh chadoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid start! Marking as "request changes" for the various doc comment stuff. The "start in detached by default with more complex health checks" thing (see comment below) would be awesome, but we could also do it in a follow-up to this one. It's already v useful to have these commands, so it might not be worth holding it up. lmk if you think we should do it at all, and if so, if we should do it now or wait for later.

cmd/soroban-cli/src/commands/network/start.rs Outdated Show resolved Hide resolved
cmd/soroban-cli/src/commands/network/start.rs Outdated Show resolved Hide resolved
cmd/soroban-cli/src/commands/network/start.rs Outdated Show resolved Hide resolved
@chadoh
Copy link
Contributor

chadoh commented Jan 17, 2024

I also think it would be really cool if we checked if Docker Daemon is running and, if not, rather than allowing Docker's error message to bubble through:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.

...we could instruct them on how to install colima. OR, we could automatically install it for them? (We could offer, and if they say y, then we could go ahead with it. But even then it seems error-prone, doesn't it?) If colima is already installed, then maybe we just automatically start a colima network called stellar? Not sure if we would also want to stop the colima network when we soroba network stop the last network—probably not?

I don't think we need to be all-in on colima, though. If there are other alternatives to Docker Desktop/Daemon like it, then we could check for those, too. And in any case, rather than bubbling the cannot connect to docker daemon error, we could throw a message that's more instructive & actionable. Maybe something along the lines of:

Running a local Stellar network requires a Docker-compatible container runtime. You can:
- install/open Docker Desktop: https://www.docker.com/products/docker-desktop/
- install/run colima: https://github.com/abiosoft/colima

Or maybe we sniff their OS and recommend colima only if they're on a Mac, podman if linux, and containerd if Windows, which are the suggestions in this blog post. I don't know. But I want to make sure we make this feel doable to total Docker & Stellar newbies who try to start a local dev environment with soroban network start.

@leighmcculloch
Copy link
Member

leighmcculloch commented Jan 17, 2024

Rather than hardcoding specific recommendations into the CLI, I think we should have a message similar to what's displayed above, but link to the soroban.stellar.org docs and place a page there with links out to docker/container runtimes folks can try, with recs based on mac, linux, windows. It means we can update the list as needed.

I'd also keep the original error. Never know what type of useful information might be found by passing through errors:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
Running a local Stellar network requires a docker-compatible container runtime.
See this url for a list of some common container runtimes to install: https://soroban.stellar.org/container-runtimes

@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch 2 times, most recently from 977be20 to 8c542a5 Compare February 9, 2024 22:28
@elizabethengelman
Copy link
Contributor Author

@chadoh @leighmcculloch Regarding the error case when the docker daemon can’t be found - I realized that when using bollard (and the underlying Docker API) it doesn’t seem to bubble up the original error ( docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?) but instead returns a kind of vague Hyper error: error trying to connect: No such file or directory (os error 2)

So instead, I’m priting out a message similar to Cannot connect…, and that is where we can also include a link to the docs when we have a page for that message, which I can work on in a followup.

ℹ️  Starting testnet network
⛔️ Failed to connect to the Docker daemon at "/var/run/docker.sock". Is the docker daemon running?
Running a local Stellar network requires a Docker-compatible container runtime.
Please note that if you are using Docker Desktop, you may need to utilize the `--docker-socket-path` flag to pass in the location of the docker socket on your machine.

error: ⛔ ️Failed to start container: error trying to connect: No such file or directory (os error 2)

@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch from 84cedae to 4e2a425 Compare February 13, 2024 19:33
@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch from 851ed5f to 2114d1d Compare February 21, 2024 19:58
@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch from 0e90ded to 4d246d7 Compare February 22, 2024 15:08
@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch from 94b48e5 to 9367847 Compare February 28, 2024 21:58
@elizabethengelman elizabethengelman force-pushed the feat/start-network-with-cli branch from c2d7779 to 70dccbd Compare March 4, 2024 15:56
@elizabethengelman elizabethengelman merged commit 5fe1b8d into stellar:main Mar 4, 2024
13 checks passed
@elizabethengelman elizabethengelman deleted the feat/start-network-with-cli branch March 4, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to Soroban CLI
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants