Closed schneems closed 1 month ago
Use
set -euo
on the process type. I believe this will complain loudly that the variable doesn't exist i.e.
Using -u
is one option, although the error message (a) might not be super clear to beginners, (b) can't be customised.
Another option would be to use the ${parameter:?word}
bash shell parameter expansion feature:
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
For example:
$ bash -c 'echo "PORT=${PORT:?Error: PORT env var is not set!}"'
bash: line 1: PORT: Error: PORT env var is not set!
Also one option would be to set a default port instead of erroring, eg:
$ bash -c 'echo "PORT=${PORT:-5001}"'
PORT=5001
Also one option would be to set a default port instead of erroring, eg:
If we set that as a default it would look like this to the user:
$ docker run -it --rm -e PORT=5001 ruby-getting-started
[1] Puma starting in cluster mode...
[1] * Puma version: 6.4.2 (ruby 3.2.4-p170) ("The Eagle of Durango")
[1] * Min threads: 5
[1] * Max threads: 5
[1] * Environment: production
[1] * Master PID: 1
[1] * Workers: 1
[1] * Restarts: (✔) hot (✖) phased
[1] * Preloading application
[1] * Listening on http://0.0.0.0:5001
[1] Use Ctrl-C to stop
[1] ! WARNING: Detected running cluster mode with 1 worker.
[1] ! Running Puma in cluster mode with a single worker is often a misconfiguration.
[1] ! Consider running Puma in single-mode (workers = 0) in order to reduce memory overhead.
[1] ! Set the `silence_single_worker_warning` option to silence this warning message.
[1] - Worker 0 (PID: 14) booted in 0.0s, phase: 0
Then when they visit localhost:5001
they mysteriously see nothing because there's no bound port. At least if we force them to add the -e
flag to the env var, it might hint to them that they also need to bind the port as well.
Wondering out loud: Does docker expose "here's the externally mapped ports" internally? Ideally we would be able to give good error/warning messages here. I.e. "you for got -e" and you forgot "-p`
Another option would be to use the ${parameter:?word} bash shell parameter expansion feature:
I like this option the best for the short term. In the long term, if we can get more info about docker from inside of docker then maybe we can have more sophisticated logic.
I believe Docker does not inform the user about what ports internally are exposed
This is failing because this is the command we're trying to run:
It's looking for a
$PORT
but cannot find one. But the error doesn't say that.Options
set -euo
on the process type. I believe this will complain loudly that the variable doesn't exist i.e.Considerations
When this goes on Heroku we've talked about hiding the
bash -c
but it will be weird to see a command in the dashboard likebin/check_env_vars && rails server --port "$PORT"
. We could probably strip outset -eu
the same way we strip outbash -c
but it would be harder to strip out multiple invocations. Confusing too as to where the error is coming from.