11

I created an image using java_image but I would like to pass arguments to my main function (i.e. String args[]). How can I do that when I use "bazel run name_of_image" command?

Adib Rastegarnia
  • 494
  • 1
  • 7
  • 21

2 Answers2

25

bazel run //your:rule -- arg1 arg2 ... argN

Everything after -- is passed to the binary.

László
  • 3,973
  • 1
  • 13
  • 26
  • 1
    thanks for your response. Does that applicable using java_image? I mean do I need to set anything in the rule itself to support args? – Adib Rastegarnia Jan 14 '19 at 15:49
  • It's worth noting too this subtlety: the role of a lone `--` is different under `bazel run` than it is under `bazel test`. For bazel test and build everything to the right of `--` is taken as a *target pattern* https://docs.bazel.build/versions/main/command-line-reference.html – jxramos Jan 10 '22 at 18:35
3

When using java_image, bazel run //my_image will only load the image into the Docker daemon, it won't run it. To run it, you should use:

docker run bazel/my_image:my_image [args]
Rodrigo Queiro
  • 1,324
  • 8
  • 15
  • Actually, when I use bazel run it compiles and runs it. – Adib Rastegarnia Jan 14 '19 at 17:40
  • If it runs the image, it should also use the arguments you specify (as per László's answer). It seems I was confused by a bug: depending on the value of the `base` attribute, `bazel run` may or may not run the Docker image after loading. – Rodrigo Queiro Jan 14 '19 at 18:39