So i have a problem ai can’t seem to fix nor i can find a solution..
I am making a project using react, sass and laravel with docker and docker won’t serve my laravel online.. It keeps infinitely loading and i don’t know what to do anymore.
This is my directory tree, i know it’s scuffed but it works π
my-project/
βββ backend/
β βββ (Laravel files here)
βββ frontend/
β βββ (React files here)
βββ docker/
β βββ backend/
β βββ Dockerfile
β βββ frontend/
β βββ Dockerfile
βββ docker-compose.yml
This is my docker-compose.yml file:
version: '3.8'
services:
backend:
build:
context: ../ # Root of the project
dockerfile: docker/backend/Dockerfile # Path to the Dockerfile
image: lerenlerentool-backend
container_name: lerenlerentool_backend
restart: unless-stopped
working_dir: /var/www
entrypoint: php artisan serve --host=0.0.0.0
volumes:
- ../backend:/var/www # Maps the backend folder into the container
- ./backend/php-local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- "8000:8000"
networks:
- app-network
environment:
- APP_ENV=local
- APP_DEBUG=true
- APP_KEY=base64:ertrdSfHv01dD/4R6N6acsRQW+hLn3MTNmk84ypaW4I=
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_DATABASE=lerenlerentool
- DB_USERNAME=test
- DB_PASSWORD=1234
depends_on:
- db
frontend:
build:
context: ../
dockerfile: docker/frontend/Dockerfile
image: lerenlerentool-frontend
container_name: lerenlerentool_frontend
restart: unless-stopped
ports:
- "3000:80"
volumes:
- ../frontend:/app
- /app/node_modules
networks:
- app-network
db:
image: mysql:8.0
container_name: lerenlerentool_db
restart: unless-stopped
environment:
MYSQL_DATABASE: lerenlerentool
MYSQL_USER: test
MYSQL_PASSWORD: 1234
MYSQL_ROOT_PASSWORD: 1234
volumes:
- dbdata:/var/lib/mysql
ports:
- "3306:3306"
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: lerenlerentool_phpmyadmin
restart: unless-stopped
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: 1234
ports:
- "8080:80"
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
And here is my dockerfile:
# Use a PHP image with FPM support
FROM php:8.2-fpm
# Install necessary PHP extensions
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev zip git unzip
# Install Composer (for Laravel dependency management)
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
# Set the working directory to where the Laravel project is
WORKDIR /var/www/backend
RUN apt-get update && apt-get install -y
libpng-dev libjpeg-dev libfreetype6-dev
libonig-dev libzip-dev zip unzip git curl
&& docker-php-ext-install pdo pdo_mysql bcmath mbstring
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql
# Copy the Laravel project files into the container
COPY backend /var/www/backend
# Install Laravel dependencies via Composer
RUN composer install --optimize-autoloader --no-dev
# Install Node.js and npm (if necessary for any assets)
RUN curl -sL https://deb.nodesource.com/setup_22.x | bash -
&& apt-get install -y nodejs
# Run Laravel migrations or other setup commands if needed
CMD php artisan serve --host=0.0.0.0 --port=8000
# Expose the correct port (if needed for API)
EXPOSE 8000
I tried to edit my dockerfile with this:
CMD php artisan serve --host=0.0.0.0 --port=8000
and my compose with this:
entrypoint: php artisan serve --host=0.0.0.0
But i still have to go into the folder where my laravel project is and manually input the serve command into the cmd of that folder for the laravel to work.
2
Answers
I have found the answer! It had something to do with the .env file from my laravel... The file wasn't correctly configured and i changed the APP_URL to: APP_URL=http://localhost:8000. and now with some time loading the backend loads on my chrome.
The fact that you need to manually run
php artisan serve
suggests that the entrypoint or CMD in your Docker container isn’t properly configured to automatically run the Laravel server.docker-compose.yml
, remove the entrypoint field, since you’realready handling that in the Dockerfile.
php artisan serve --host=0.0.0.0 --port=8000
. You may want to make sure that this command is being executed properly, especially aftercomposer install
.