I want to dockerize my laravel learning app and have been unable to find simple tutorial that runs without errors. Each one is different and fails so I created my own. Here is how my Dockerfile look like, my first attempt
# stage 1: start from composer
FROM composer:2.6.5 as build
WORKDIR /app
# copy all the code
COPY . .
# run composer to update
RUN composer update --prefer-dist --no-dev --no-interaction --ignore-platform-req=ext-http --ignore-platform-req=ext-xdebug
# stage 2: get the php latest image
FROM php:8.3.0RC6-zts-bullseye as production
# update the image
RUN apt-get update && apt-get install -y curl libpng-dev libonig-dev libxml2-dev
# add necessary libraries
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# clean the apt cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
WORKDIR /app
# copy all code from stage 1
COPY --from=build /app /var/www/html
# move the production file to the image
COPY .env.production .env
# RUN a2enmod rewrite
RUN php artisan config:cache &&
php artisan route:cache &&
chmod 777 -R /var/www/html/storage/ &&
chown -R www-data:www-data /var/www/ &&
ENTRYPOINT ["php", "artisan", "serve", "-host 0.0.0.0", "-port 8080" ]
Needsless to say it failed at various steps specially when setting permissions for directories in container. Also libraries could not be installed for instance
- mbstring
- gd
So i gave up on the idea of creating it myself and used ChatGPT to generate one for me, I had to modify it though as before the default one failed again.
# Stage 1: Build the application
FROM php:8.3.0RC6-cli as builder
# Set working directory
WORKDIR /app
# Install dependencies
RUN apt-get update && apt-get install -y
git
unzip
&& rm -rf /var/lib/apt/lists/*
# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Copy Laravel application files
COPY . .
# Install dependencies using composer
RUN composer install --no-dev --no-interaction --no-progress --no-suggest --ignore-platform-req=ext-http --ignore-platform-req=ext-xdebug
RUN docker-php-ext-install pdo_mysql
#RUN docker-php-ext-install mbstring
RUN docker-php-ext-install exif
RUN docker-php-ext-install pcntl
RUN docker-php-ext-install bcmath
#RUN docker-php-ext-install gd
# Generate an optimized autoloader
RUN composer dump-autoload --optimize --classmap-authoritative
# Generate the application key
RUN php artisan key:generate
# Build the application
RUN php artisan config:cache
RUN php artisan route:cache
#RUN php artisan view:cache
# Stage 2: Create final image
FROM php:8.3.0RC6-cli
# Set working directory
WORKDIR /app
# Copy built application from the builder stage
COPY --from=builder /app .
# Expose the application port (if needed)
EXPOSE 8000
# Run the application
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
With this, although with changes, the app ran but was unable to do much. it kept on giving error:
{
"code": 500,
"message": "could not find driver (Connection: storage, SQL: select exists(select * from `role` where `roleid` = 7) as `exists`)"
}
I ran bash inside container and this is what I got when i did php –ini
root@c0c440c243bb:/app# php --ini
PHP Warning: PHP Startup: Unable to load dynamic library 'mysqli' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20230831/mysqli (/usr/local/lib/php/extensions/no-debug-non-zts-20230831/mysqli: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20230831/mysqli.so (/usr/local/lib/php/extensions/no-debug-non-zts-20230831/mysqli.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_mysql' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20230831/pdo_mysql (/usr/local/lib/php/extensions/no-debug-non-zts-20230831/pdo_mysql: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20230831/pdo_mysql.so (/usr/local/lib/php/extensions/no-debug-non-zts-20230831/pdo_mysql.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File: /usr/local/etc/php/php.ini
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed: /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
As you can see, it’s not able to access pdo-mysql, although it was defined. infact, there was no php.ini inside container, only php.ini.development and php.ini.production files in /usr/local/etc/php. I cp’ied the php.ini.production to php.ini in the same folder i.e. /usr/local/etc/php but again it didn’t work. and I don’t know if this error is fixed, what other errors would be there.
can anyone help me run the laraval app inside please?
dockerizing the laravel app and it didn’t work
2
Answers
I create a basic dockerfile to run your app you can learn from this.
Dockerfile
docker/entrypoint.sh
docker-compose.yaml