jn-jairo / laravel-ngrok

Share Laravel application with ngrok.
MIT License
89 stars 12 forks source link

Replace default address localhost for Docker container #7

Closed k1s3l closed 2 years ago

k1s3l commented 2 years ago

Describe the solution you'd like Option --host-header (bool). To install custom address (container name if use docker) without host-header. Simply put, replacing the default localhost

current

public function buildProcess(string $host = '', string $port = '80') : Process
{
    $command = ['ngrok', 'http', '--log', 'stdout'];

    if ($host !== '') {
        $command[] = '--host-header';
        $command[] = $host;
    }

    $command[] = $port ?: 80;

    return new Process($command, $this->getWorkingDirectory(), null, null, null);
}

feature

public function buildProcess(string $host = '', string $port = '80', $hostHeader = false) : Process
{
    $command = ['ngrok', 'http', '--log', 'stdout'];

    if ($host !== '' && !$hostHeader) {
        $command[] = '--host-header';
        $command[] = $host;
        $command[] = $port;
    } elseif ($host !== '' && $hostHeader) {
        $command[] = "{$host}:{$port}";
    }

    return new Process($command, $this->getWorkingDirectory(), null, null, null);
}

fix for NgrokCommand

    60    public function handle() : int
    61    {
    62       $host = $this->argument('host');
    63       $port = $this->option('port');
    64       $hostHeader = $this->option('host-header');
    ...
   100       $process = $this->processBuilder->buildProcess($host, $port, (bool) $hostHeader);

Describe alternatives you've considered As quickly solve conside php artisan ngrok --port={container_name}:{port}. This hack solve my needs

UPD: sorry for my broken english

jn-jairo commented 2 years ago

It is not supposed to run inside docker. If you run it in the host it should work (without the container name), I use it in that way along with docker.

I wasn't able to simulate, even running the ngrok command directly it didn't work for me using the container name, either inside and outside docker.

If you could explain your docker structure I can try to simulate it again.

k1s3l commented 2 years ago

I have checked my solution mentioned above, it works) although it is quite crooked, I think it can be refactored. As a host, we specify a link to the web server container (in my case, this is nginx). At first I was looking for ngrok under docker. And then I remembered a simple hack using the container name instead of the usual url, it works. Here is my docker-compose.yml: https://github.com/k1s3l/shipout-queue/blob/UPDATE-DOCKER/docker-compose-base.yml Native ngrok command for my case (execute in laravel container): ngrok http --log stdout nginx:80

jn-jairo commented 2 years ago

I added some options, please update to the new release v2.0.3 and use:

php artisan ngrok --host=nginx

See the advanced usage to know more about the new options.

k1s3l commented 2 years ago

it's very cool, I can't believe how cool it is! thanks!!! $respect++