skip to Main Content

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


  1. I create a basic dockerfile to run your app you can learn from this.

    Dockerfile

    FROM php:8.2.0 as php
    
    RUN apt-get update -y
    RUN apt-get install -y unzip libpq-dev libcurl4-gnutls-dev
    RUN docker-php-ext-install pdo pdo_mysql bc math
    RUN pecl install -o -f redis && rm -rf /tmp/pear && docker-php-ext-enable redis
    
    COPY --from=composer:2.3.5 /usr/bin/composer /usr/bin/composer 
    
    WORKDIR /var/www/app
    COPY . .
    
    ENTRYPOINT [ "docker/entrypoint.sh" ]
    

    docker/entrypoint.sh

    if [ ! -f "vendor/autoload.php" ]; then
      composer install
    fi
    
    if [ ! -f ".env" ]; then
       cp .env.example .env
    fi
    
    php artisan migrate
    php artisan key:generate
    php artisan cache:clear
    php artisan config:clear
    php artisan route:clear
    php artisan serve --port=8000 --host=0.0.0.0 --env=.env
    exec docker-php-entrypoint "$@"
    

    docker-compose.yaml

    version: '3.8'
    
    services:
      app:
       container_name: app
       build: #
        context: .
        target: php 
       working_dir: /var/www/app 
       volumes: 
        - ./:/var/www/app
       ports: 
        - 8000:8000
    
    Login or Signup to reply.
  2. FROM php:8.1-fpm
    WORKDIR /var/www
    ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
    # Install php extensions
    RUN chmod +x /usr/local/bin/install-php-extensions && sync && 
        install-php-extensions mbstring pdo_mysql zip exif pcntl gd memcached
    
    # Install dependencies
    RUN apt-get update && apt-get install -y 
        build-essential 
        libpng-dev 
        libjpeg62-turbo-dev 
        libfreetype6-dev 
        locales 
        zip 
        jpegoptim optipng pngquant gifsicle 
        unzip 
    #    git 
        curl 
        lua-zlib-dev 
        libmemcached-dev 
        nginx 
        cron 
        supervisor
    
    # Install composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    
    # Clear cache
    RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    # Add user for laravel application
    RUN groupadd -g 1000 www
    RUN useradd -u 1000 -ms /bin/bash -g www www
    
    # PHP Error Log Files
    RUN mkdir /var/log/php
    RUN touch /var/log/php/errors.log && chmod 777 /var/log/php/errors.log
    
    COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
    COPY --from=node /usr/local/bin/node /usr/local/bin/node
    COPY --from=node /app/node_modules /var/www/node_modules
    RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm
    COPY docker/run.sh /var/www/docker/run.sh
    
    # Copy code to /var/www
    COPY --chown=www:www-data . /var/www
    COPY .env.example .env
    
    # add root to www group
    RUN chmod -R ug+w /var/www/storage
    # Copy nginx/php/supervisor configs
    RUN cp docker/supervisor.conf /etc/supervisord.conf
    # RUN cp docker/ssl_certs.crt  /etc/ssl/certs/ssl_certs.crt
    # RUN cp docker/ssl_key.key  /etc/ssl/certs/ssl_key.key
    RUN cp docker/php.ini /usr/local/etc/php/conf.d/app.ini
    #RUN cp docker/nginx.conf /etc/nginx/sites-enabled/default
    RUN cp docker/default /etc/nginx/sites-available/default
    
    
    # Deployment steps
    RUN composer install --optimize-autoloader --no-dev
    RUN chmod +x /var/www/docker/run.sh
    
    ### These commands will only execute once in production
    ### For development if db data is not important then we can execute these commands on every build.
    RUN php artisan key:generate
    # RUN yes | php artisan migrate:fresh
    # RUN php artisan migrate
    # RUN yes | php artisan db:seed
    RUN php artisan passport:install --force
    RUN php artisan queue:restart
    #RUN php artisan tenants:artisan migrate
    ####
    RUN touch /var/www/storage/logs/laravel-$(date +%Y-%m-%d).log
    # RUN chmod 777 /var/www/storage/logs/laravel-*
    RUN chmod -R 777 /var/www/storage/logs
    RUN chgrp -R www-data /var/www/storage /var/www/bootstrap/cache
    RUN chmod -R ug+rwx /var/www/storage /var/www/bootstrap/cache
    #COPY CRONtab
    ADD docker/root /etc/cron.d/log-cron
    RUN chmod 0644 /etc/cron.d/log-cron
    RUN touch /var/log/cron.log
    RUN touch /var/www/storage/logs/cron.log
    RUN chmod 777 /var/www/storage/logs/cron.log
    RUN chmod 777 /var/www/storage
    RUN php artisan storage:link
    RUN echo "request_terminate_timeout = 180" >> /usr/local/etc/php-fpm.d/www.conf
    #RUN apt-get install cron -y
    RUN service cron start
    RUN crontab /etc/cron.d/log-cron
    # EXPOSE 80
    # ENTRYPOINT ["/var/www/docker/run.sh"]
    EXPOSE 82
    ENTRYPOINT php artisan serve --host=0.0.0.0    ```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search