My local docker environment consists of app
and web
.
services:
app:
build:
context: ./docker/services/app
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./src:/var/www
networks:
- internal
web:
build:
context: ./docker/services/web
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./src:/var/www
ports:
- "8080:80"
depends_on:
- app
networks:
- internal
networks:
internal:
driver: bridge
The app
is Laravel based on FROM php:8.3-fpm
and the web
is Nginx on FROM nginx:1.25
with vhost.conf
of:
server {
listen 80;
index index.php index.html;
root /var/www/public;
location / {
try_files $uri /index.php?$args;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
The only route I have is:
<?php
use AppServicesTenantService;
use IlluminateSupportFacadesRoute;
Route::get('/', function (IlluminateHttpRequest $request) {
/** @var TenantService $service */
$service = app()->make(TenantService::class);
dd($service->getTenantName($request));
});
My service class is:
<?php
namespace AppServices;
use IlluminateHttpRequest;
class TenantService
{
public function getTenantName(Request $request): string
{
$chunks = explode('.', $request->getHttpHost());
$subdomain = $chunks[0] ?? null;
dd($chunks, 'the subdomain is: ' . $subdomain);
}
}
Finally, how is it possible that I can provide any (gibberish) subdomain into the URL and both Docker and Laravel will understand it, while I did not (!) add such a subdomain into /etc/hosts
?
cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
#0.0.0.0 ocsp.apple.com
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
The following dummy URLs work with no issues whatsoever.
It is exactly what I want, but I don’t understand how it works out of box. Especially after reading this.
Normally if I’m not mistaken I would have to add entry like:
127.0.0.1 acme.localhost
to be able to reach acme
tenant, but I didn’t have to do this. Why?