skip to Main Content

Hi i am new to docker.

I have cloned my laravel code from

https://github.com/laravel/laravel/tree/8.x

and followed the steps provided in given below link.

https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose

But still, mbstring is not installed in my project with php 8 version as my laravel project is compatible with a higher version than 7.2 but when i tried mbstring with 7.2 version it was easyly installed. i want to install mbstring with php 8 version.

Given below is my docker file. please have a look at it.

FROM php:8.0.3-fpm-buster

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# 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 
    vim 
    unzip 
    git 
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

2

Answers


  1. FROM php:8-fpm
    
    # Copy composer.lock and composer.json
    COPY composer.lock composer.json /var/www/
    
    # Set working directory
    WORKDIR /var/www
    
        ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
    
    RUN chmod +x /usr/local/bin/install-php-extensions && sync && 
        install-php-extensions mbstring pdo_mysql zip exif pcntl gd
    
    
    #previous code
    # 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 
        vim 
        unzip 
        git 
        curl
    
    
    # Clear cache
    RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    
    # Install extensions
    #RUN docker-php-ext-install 
    #RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
    
    # Install composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    
    # Add user for laravel application
    RUN groupadd -g 1000 www
    RUN useradd -u 1000 -ms /bin/bash -g www www
    
    RUN php artisan key:generate
    
    # Copy existing application directory contents
    COPY . /var/www
    
    # Copy existing application directory permissions
    COPY --chown=www:www . /var/www
    
    # Change current user to www
    USER www
    
    # Expose port 9000 and start php-fpm server
    EXPOSE 9000
    CMD ["php-fpm"]
    

    Try this..

    Login or Signup to reply.
  2. What follows is more appropriate as a comment, but I have an insufficient reputation to comment.

    Adarsh Hiwrale’s excellent suggestion mostly worked for me, but I had to rearrange a few lines to avoid a race condition.

    That is, the reordered lines are necessary for anyone starting from scratch who is building for the first time because they will not yet have a partial Docker build that can execute RUN php artisan key:generate.

    My revision follows with comments indicating what was modified and why.

    FROM php:8-fpm
    
    # Copy composer.lock and composer.json
    COPY composer.lock composer.json /var/www/
    
    # Set working directory
    WORKDIR /var/www
    
    ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
    
    RUN chmod +x /usr/local/bin/install-php-extensions && sync && 
        install-php-extensions mbstring pdo_mysql zip exif pcntl gd
    
    
    #previous code
    # 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 
        vim 
        unzip 
        git 
        curl
    
    
    # Clear cache
    RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    
    # Install extensions
    #RUN docker-php-ext-install 
    #RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
    
    # Install composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    
    # Add user for laravel application
    RUN groupadd -g 1000 www
    RUN useradd -u 1000 -ms /bin/bash -g www www
    
    # BEGIN MODIFICATIONS to Adarsh Hiwrate's suggested code
    # Copy existing application directory contents
    # The following line is redundant (predundant?) because of the line which copies the required items with the correct permissions.
    # COPY . /var/www
    
    # Copy existing application directory permissions
    COPY --chown=www:www . /var/www
    
    # This has to come after the above copy, otherwise the code will not yet be available in the container.
    RUN php artisan key:generate
    
    # END MODIFICATIONS to Adarsh Hiwrate's suggested code
    
    # Change current user to www
    USER www
    
    # Expose port 9000 and start php-fpm server
    EXPOSE 9000
    CMD ["php-fpm"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search