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?
FROM php:8.3-fpm-alpine3.20
RUN apk update && apk upgrade
# Essentials
RUN echo "Europe/London" > /etc/timezone
RUN apk add git zip unzip curl sqlite supervisor
# Install Python
RUN apk add python3 py3-pip
RUN apk add nodejs npm
RUN apk add nano
RUN apk add php83-gd
php83-imap
php83-redis
php83-cgi
php83-bcmath
php83-mysqli
php83-zlib
php83-curl
php83-zip
php83-mbstring
php83-iconv
gmp-dev
# dependencies required for running "phpize"
# these get automatically installed and removed by "docker-php-ext-*" (unless they're already installed)
ENV PHPIZE_DEPS
autoconf
dpkg-dev
dpkg
file
g++
gcc
libc-dev
make
pkgconf
re2c
zlib
wget
# Install packages
RUN set -eux;
# Packages needed only for build
apk add --virtual .build-deps
$PHPIZE_DEPS
RUN apk add --no-cache linux-headers
# Packages to install
RUN apk add curl
gettext-dev
libmcrypt-dev
icu-dev
libpng
libpng-dev
libressl-dev
libtool
libxml2-dev
libzip-dev
libjpeg-turbo-dev
libwebp-dev
freetype-dev
oniguruma-dev
unzip
# pecl PHP extensions
RUN pecl install
# imagick-3.4.4
mongodb
redis
# Configure PHP extensions
RUN docker-php-ext-configure
# ref: https://github.com/docker-library/php/issues/920#issuecomment-562864296
gd --enable-gd --with-freetype --with-jpeg --with-webp
# Install PHP extensions
RUN docker-php-ext-install
bcmath
bz2
exif
ftp
gettext
gd
# iconv
intl
gmp
mbstring
opcache
pdo
pdo_mysql
shmop
sockets
sysvmsg
sysvsem
sysvshm
zip
&&
# Enable PHP extensions
docker-php-ext-enable
# imagick
mongodb
redis
&&
# Remove the build deps
apk del .build-deps
RUN apk cache clean
# fix work iconv library with alphine for PHP 8.1 broken
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so
# # Installing bash
# RUN apk add bash
# RUN sed -i 's/bin/ash/bin/bash/g' /etc/passwd
# Installing composer
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN rm -rf composer-setup.php
# Configure supervisor
RUN mkdir -p /etc/supervisord/conf.d/
RUN touch /run/supervisord.sock
COPY ./docker-ops/backend/supervisord/laravel-worker.conf /etc/supervisord/conf.d/laravel-worker.conf
COPY ./docker-ops/backend/supervisord/supervisord.ini /etc/supervisord/supervisord.ini
# Cron Config
COPY ./docker-ops/backend/crontab /etc/crontabs/root
# Config PHP
COPY ./docker-ops/backend/php/local.ini /usr/local/etc/php/php.ini
# COPY ./ /var/www
COPY --chown=www-data:www-data ./my-app /var/www
COPY ./docker-ops/backend/scripts.env /var/www/resources/scripts/.env
COPY ./docker-ops/backend/.env.laravel /var/www/.env
# Install Python packages
ENV PIP_BREAK_SYSTEM_PACKAGES 1
RUN pip install -r /var/www/resources/scripts/requirements.txt
USER root
WORKDIR /var/www
EXPOSE 8000
CMD ["php", "artisan", "serve", "--host", "0.0.0.0", "--port=8000"]```