Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

swagger's avatar

Laravel API in Docker is returning HTML instead of JSON?

I'm dockerzing my Laravel API and going quite crazy.

Locally with either php artisan serve or symfony serve everything works well and has been working for years.

Now I'm finally dockerzing (not an expert in Docker) Laravel for production and having some problems.

The main one is that all the api routes return text/html as content type and not application/json, but locally it returns application/json.

The code base obviously is the same one. And the return of each api route is like this response()->json($data);

This is my dockerfile and it's being used in a docker-compose file. Anyone has any idea why?

It seems that all the laravel headers are being replaced, so it's probably a Docker problem rather than Laravel

0 likes
5 replies
martinbean's avatar

@swagger There’s a couple of issues I can see from skim-reading your Dockerfile:

  • You should be combining RUN calls as much as possible, as each invocation creates a new layer in your Docker image, and you want as few layers as possible for file size optimization.
  • You’re configuring a lot of things in this Dockerfile that don’t pertain to a PHP application: Supervisor, crontab, Python, etc. If you need things like those, and y ou are using Docker Compose, then those things should be there own services in your docker-compose.yml file.
  • You absolutely do not want to be using the php artisan serve command to serve your application in production. You should be using something like nginx (again, in a separate service).

If you Google “Docker Compose for production PHP” then I’m sure there will be examples showing how to set up Docker Compose for such, using a web server (either Apache or nginx) and then your PHP application in its own container, as well as building assets, or having additional services (i.e. for Python) to do… whatever it is you actually need to do with Python for a PHP application.

swagger's avatar

@martinbean Thanks for the suggestions. This a first version of the docker file and slowly fixing things, for example I'm fully aware to not use php artisan serve for production, but I'm focusing on one thing at the time :)

Currently just trying to figure out why the headers are being overridden in Docker while when working locally I have the correct headers.

martinbean's avatar

@swagger Well what is “production”? Where are you trying to run this Docker image?

swagger's avatar

@martinbean I have a small project which currently runs on a server without docker, everything is installed directly on the server "manually". It runs with Nginx and PHP-FPM.

I'm trying to replicate the same setup using multiple Docker containers, and eventually replace the "manual" setup with a Dockerized solution. Like I said, this is an early version of the Dockerfile to test things out, I do have a working solution already with NGINX and PHPFPM with Docker, but I do still have this issue of the API returning HTML rather than JSON, and it seems that all headers are being replaced.

swagger's avatar
swagger
OP
Best Answer
Level 1

Found the issue... PHP doesn't like white spaces near the <?php tag.

Went through all the files to look for tabs/spaces/newline near the <?php and deleted them. I had an empty line in my web.php file.

That was painful to figure out.

Still do not know why with Docker it was failing while locally it was returning a JSON

Please or to participate in this conversation.