-1

I often run dockerised PHP apps, which have a standard password handling mechanism. A hash must be calculated:

<?php
$hash = password_hash("my_secret", PASSWORD_DEFAULT);
var_dump($hash);

I find it tedious to research how to do it each time, then create a throwaway .php file, load it into a PHP container, etc., just to get that hash.

I want to run that inside the PHP container, inline in bash, something like this:

docker run --rm php:8.2-cli php '$hash = password_hash("my_secret", PASSWORD_DEFAULT); var_dump($hash);'

But that presumably expects a php file rather than inline php code, so produces error:

Could not open input file

I am not a PHP dev. How do I correct that docker run command, so I don't have to get knee-deep in PHP?


UPDATE

This question was closed so I can't add an answer. For non-PHP devs like me, who wouldn't have thought to search for that linked answer (which doesn't mention the docker use case), but would have thought to search for this question (which does), the answer is is to use the -r switch:

docker run --rm php:8.2-cli php -r '$hash = password_hash("my_secret", PASSWORD_DEFAULT); var_dump($hash);'

Thanks to @Nick for the solution in the comments below.

lonix
  • 14,255
  • 23
  • 85
  • 176
  • 2
    You need the `-r` option to run PHP code from the command line – Nick Aug 31 '23 at 03:00
  • That linked question does not mention docker. It's not helpful to non-php users. – lonix Aug 31 '23 at 03:01
  • 1
    It shouldn't matter that you're running inside docker – Nick Aug 31 '23 at 03:02
  • 1
    btw I did not downvote, I don't really know why other's have – Nick Aug 31 '23 at 03:02
  • People who want a quick php script via docker will search for docker, they won't have the knowledge required to search for that question. Reading that answer though, it makes sense to me now (thanks)... but only because I was directed to it by someone like you who actually knows (I wouldn't know what to search for). Oh well, SO at work. – lonix Aug 31 '23 at 03:03
  • BTW, quickest question close I've ever seen. You PHP guys are enthusiastic. :-) – lonix Aug 31 '23 at 03:06
  • 1
    Hopefully this question won't get deleted (I'll help that) and then it will stay on the site and provide a signpost. As far as quick closes, if you saw the number of crap answers this tag gets, you'd understand :) – Nick Aug 31 '23 at 04:00

0 Answers0