Getting a client IP address in Laravel is pretty simple. You don’t need to know which header contains the IP address, because there is the ip() method in the Illuminate\Http\Request class. However, if your Laravel application is running in a Docker container with virtual network, the method above will always return Docker internal IP address instead of the client IP.
In this short article, I am going to explain how to fix that and continue using the ip() method even in Dockerized Laravel application.
How To Get IP Address in Dockerized Laravel
Let’s look at how Laravel application works in a docker container with virtual network. There are Apache or Nginx and PHP-FPM that handles requests. However, Nginx and PHP-FPM can’t see the client IP address because it is replaced by docker internal IP. So that, if you send a request from the browser directly to Docker, the REMOTE_ADDR header will contain an IP like 172.26.0.1 and you will not be able to get client real address. Let’s create an endpoint in the routes/api.php file, which returns the client IP and a few headers:
routes/api.phpRoute::get("/ip", function (\Illuminate\Http\Request $request) {
return response()->json([
"ip" => $request->ip(),
"remote_addr" => $_SERVER["REMOTE_ADDR"] ?? "",
"x_forwarded_for" => $_SERVER["HTTP_X_FORWARDED_FOR"] ?? "",
"x_forwarded_proto" => $_SERVER["HTTP_X_FORWARDED_PROTO"] ?? "",
]);
});
As you can see, there REMOTE_ADDR contain docker internal address:

However, you usually have multiple services on VPS, so you need a dedicated main Nginx web server that will work as a proxy. This web server handles HTTPS connection, chooses a domain, and then forwards the request to a container with the application. The web server knows the client IP, and it can forward it into the container using the X-Forwarded-For and X-Forwarded-Proto headers. For example, here is a full configuration for test_domain.local, which forwards requests to http://localhost:8080.
/etc/nginx/conf.d/test_domain.confserver {
listen 0.0.0.0:8081;
server_name test_domain.local;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Here, http://localhost:8093 is the address and port of the application web server inside a docker container. At this moment, our test endpoint will return the following response:

By default, the headers above are ignored. Although, there are TrustedProxies middleware which can fetch correct IP from X-Forwarded-For and X-Forwarded-Proto headers. This middleware is already enabled for all requests. You only need to add your proxy IP to the trusted proxies list. This list is in the $proxies array in app/Http/Middleware/TrustProxies.php. Here are possible values:
- Proxy IP. In our case it should be internal docker IP – 172.26.0.1;
- * – reflects any proxy server that is located on the same server as your Laravel application;
- ** – reflects any proxy.
For example:
app/Http/Middleware/TrustProxies.phpprotected $proxies = [
"172.26.0.1"
];
After that, you can recheck the IP address, and it will be displayed correctly this time.

Wrapping Up
In this short article, we’ve dove into how to get a client IP in Laravel when your project is running on Docker. As you can see, everything is quite simple. However, you need an external proxy server to pass the necessary data to the container. Alternatively, you can use host network driver for your docker containers, or configure docker to not override client IP address. You can read more about Docker configuration in this Github issue.