Can not find autoload.php when running laravel in a docker container - php

I am trying to run laravel on a docker container. However, I created a docker file to install the required dependencies and extensions. Then, I created a docker-compose file to run the container. But, when running the container using docker-compose up the following error appears:
Warning: require(/var/www/vendor/autoload.php): failed to open stream:
No such file or directory in /var/www/artisan on line 18 main_system_1
| main_system_1 | Fatal error: require(): Failed opening required
'/var/www/vendor/autoload.php' (include_path='.:/usr/local/lib/php')
in /var/www/artisan on line 18 workspace_main_system_1 exited with
code 255
The Dockerfile:
FROM php:alpine
# Install dev dependencies
RUN apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
curl-dev \
imagemagick-dev \
libtool \
libxml2-dev \
postgresql-dev \
sqlite-dev
# Install production dependencies
RUN apk add --no-cache \
bash \
curl \
g++ \
gcc \
git \
imagemagick \
libc-dev \
libpng-dev \
make \
mysql-client \
nodejs \
nodejs-npm \
yarn \
openssh-client \
postgresql-libs \
rsync \
zlib-dev \
libzip-dev
# Install PECL and PEAR extensions
RUN pecl install \
imagick
# Install and enable php extensions
RUN docker-php-ext-enable \
imagick
RUN docker-php-ext-configure zip --with-libzip
RUN docker-php-ext-install \
curl \
iconv \
mbstring \
pdo \
pdo_mysql \
pdo_pgsql \
pdo_sqlite \
pcntl \
tokenizer \
xml \
gd \
zip \
bcmath
# Install composer
ENV COMPOSER_HOME /composer
ENV PATH ./vendor/bin:/composer/vendor/bin:$PATH
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer
# Install PHP_CodeSniffer
RUN composer global require "squizlabs/php_codesniffer=*"
# Cleanup dev dependencies
RUN apk del -f .build-deps
# Setup working directory
WORKDIR /var/www
COPY composer.json composer.json
#COPY composer.lock composer.lock
RUN composer install --no-autoloader
COPY . .
RUN composer dump-autoload
RUN php artisan key:generate
RUN php artisan jwt:secret
RUN chmod 777 -R storage
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
And this is my docker-composr.yml file:
ersion: '3.1'
services:
main_system:
build: ./main-system
ports:
- 8000:8000
env_file: ./main-system/.env
volumes:
- ./main-system:/var/www

Your dockerfile runs composer install --no-autoloader. This may be the issue.

I solved the problem by:
removing the volume from docker-compose.yml
change the order of the COPY . . command and put it before RUN composer install
removing the --no-autoloader

Related

executor failed running /bin/sh -c docker-php-ext-install

I am trying upgrade this docker file from 7.3 to 7.4 but getting executor failed error.
Detailed error:
executor failed running [/bin/sh -c docker-php-ext-install bcmath
ctype dom gd hash iconv intl mbstring
mysqli opcache pdo_mysql simplexml sockets soap
sodium xsl zip ;]: exit code: 2
FROM php:7.4-fpm as base
ENV COMPOSER_HOME=/tmp/composer
ENV APCU_VERSION=5.1.18
RUN apt-get update && apt-get install -y --no-install-recommends gnupg \
netcat \
sudo \
libicu-dev \
libfreetype6-dev \
libjpeg-dev \
libpng-dev \
libsodium-dev \
libxml2-dev \
libxslt-dev \
libzip-dev \
rsync \
unzip \
git \
openssh-client \
;
RUN pecl install apcu-${APCU_VERSION}
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install \
bcmath \
ctype \
dom \
gd \
hash \
iconv \
intl \
mbstring \
mysqli \
opcache \
pdo_mysql \
simplexml \
sockets \
soap \
sodium \
xsl \
zip \
;
RUN docker-php-ext-enable apcu
RUN echo "memory_limit=1G" >> /usr/local/etc/php/conf.d/zz-memory-limit-php.ini
RUN echo "apc.enable=1" >> /usr/local/etc/php/conf.d/zz-apcu.ini
RUN echo "apc.enable_cli=1" >> /usr/local/etc/php/conf.d/zz-apcu.ini
RUN echo "opcache.memory_consumption=512MB" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.max_accelerated_files=60000" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.consistency_checks=0" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.validate_timestamps=0" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.enable_cli=1" >> /usr/local/etc/php/conf.d/zz-opcache.conf
FROM base as build
RUN curl https://files.magerun.net/n98-magerun2.phar -o /usr/local/bin/magerun \
&& chmod 755 /usr/local/bin/magerun
RUN mkdir -p -m 0775 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN mkdir -p -m 0775 ~/.ssh && ssh-keyscan github.com >> /var/www/.ssh
# USER www-data
WORKDIR /var/www/html
ARG COMPOSER_AUTH
COPY --from=composer:1 /usr/bin/composer /usr/bin/composer
RUN composer global require hirak/prestissimo
COPY auth.json auth.json
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN --mount=type=ssh,id=id_rsa php -d memory_limit=2G $(which composer) install --no-progress --no-dev
COPY app/etc/config.php app/etc/config.php
COPY bin bin
FROM build as app
ENV MAGE_MODE=production
RUN php -d memory_limit=2G bin/magento setup:di:compile
RUN composer dump-autoload --optimize --apcu
RUN php -d memory_limit=2G bin/magento setup:static-content:deploy -f
RUN rm -rf /var/www/html/var/cache
RUN rm -rf /var/www/html/var/page_cache
RUN rm -rf /var/www/html/var/session
COPY --chown=www-data app/etc/env.docker.php app/etc/env.php
i had a similar issue today.
Seems that in php7.4 iconv his a native library.
So you should try to remove it from your Dockerfile and see if it works.
Even if it's an 6 month old issue, i hope it could help someone.
Chears.

Docker git not found in PATH

I'm still learning docker, and i'm trying to Dockerize a laravel project.
I'm trying to execute laravel composer in docker, however git does not work:
[RuntimeException] git was not found in your PATH, skipping source download
Git works normal in my local machine.
My Docker file:
FROM php:7.4-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql sockets
RUN curl -sS https://getcomposer.org/installer​ | php -- \
--install-dir=/usr/local/bin --filename=composer~
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY . .
RUN composer install
I've tried to install git using dockerfile:
FROM alpine
RUN apk add --no-cache git
CMD ["git","--version"]
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
but docker build dont work:
=> ERROR [stage-0 8/8] RUN apt-get update && apt-get upgrade -y && apt-get install -y git
docker-compose.yml:
version: '3.8'
services:
php-apache-environment:
container_name: php-apache.
image: php:7.4-apache
volumes:
- ./:/var/www/html
ports:
- 8001:80
How git works in docker? I have to change my environment variables somehow or can i install git on docker?
Or perhaps both and i doing both wrong?
I believe COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer is sufficient to install composer in you docker project .
I created a dockerfile for a laravel project before and this is how it looked
FROM php:7.3-apache
# 1. development packages
RUN apt-get update && apt-get install -y \
git \
zip \
curl \
sudo \
unzip \
libzip-dev \
libicu-dev \
libbz2-dev \
libpng-dev \
libjpeg-dev \
libmcrypt-dev \
libreadline-dev \
libfreetype6-dev \
g++
# 2. apache configs + document root
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# 3. mod_rewrite for URL rewrite and mod_headers for .htaccess extra headers like Access-Control-Allow-Origin-
RUN a2enmod rewrite headers
# 4. start with base php config, then add extensions
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN docker-php-ext-install \
bz2 \
intl \
iconv \
bcmath \
opcache \
calendar \
mbstring \
pdo_mysql \
zip
#upload
RUN echo "file_uploads = On\n" \
"memory_limit = 500M\n" \
"upload_max_filesize = 500M\n" \
"post_max_size = 500M\n" \
"max_execution_time = 600\n" \
> /usr/local/etc/php/conf.d/uploads.ini
# 5. composer
COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer

Dockerfile fail to build for FROM orsolin/php:5.3-apache

I'm trying to build for legacy php5.3 inside my windows WSL2 ubuntu via latest docker desktop,
https://hub.docker.com/r/orsolin/docker-php-5.3-apache
The file content I put it this way
FROM orsolin/php:5.3-apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN apt-get update
RUN apt-get install -y \
git \
zip \
curl \
sudo \
unzip \
libicu-dev \
libbz2-dev \
libpng-dev \
libjpeg-dev \
libmcrypt-dev \
libreadline-dev \
libfreetype6-dev \
g++
RUN docker-php-ext-install \
bz2 \
intl \
bcmath \
opcache \
calendar \
pdo_mysql \
mysqli
COPY docker/apache/000-default.conf /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite headers
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
RUN chmod +x /usr/local/bin/composer
RUN composer self-update
ARG uid
RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
RUN mkdir -p /home/devuser/.composer && \
chown -R devuser:devuser /home/devuser
EXPOSE 80
It come out with such error, not sure why...
=> ERROR [internal] load metadata for docker.io/orsolin/php:5.3-apache 3.3s
=> [auth] orsolin/php:pull token for registry-1.docker.io 0.0s
------
> [internal] load metadata for docker.io/orsolin/php:5.3-apache:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
ERROR: Service 'web' failed to build : Build failed

Laravel Dockerfile > 1GB

I'm building a Docker Image for my Laravel application but it's getting bigger than 1GB.
This is the Dockerfile:
#
# PHP Dependencies
#
FROM composer as vendor
COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--no-scripts \
--prefer-dist
#
# Application
#
FROM php:fpm-alpine
RUN apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
curl \
libtool \
libxml2-dev \
&& apk add --no-cache \
curl \
git \
mysql-client \
&& pecl install redis \
&& docker-php-ext-install \
pdo \
pdo_mysql \
tokenizer \
bcmath \
opcache \
xml \
&& apk del -f .build-deps \
&& docker-php-ext-enable \
pdo_mysql \
redis
WORKDIR /var/www/html
COPY . /var/www/html
COPY --from=vendor /app/vendor/ /var/www/html/vendor/
COPY .env.staging /var/www/html/.env
RUN chown -R root:www-data .
EXPOSE 9000
CMD ["php-fpm"]
I use Vue as front-end and Redis as caching provider.
I am used to images around the 200-300 MBs but this is ridiculous.
What can I do to reduce the image size?
Thanks in advance.
Since docker filesystem is OverlayFS, you need a special tool to dive into FS layers.
There are many tools to explore images.
For your case I'd suggest wagoodman/dive: A tool for exploring each layer in a docker image
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock \
wagoodman/dive:latest \
<image_name|image_id>
A tool for exploring a docker image, layer contents, and discovering ways to shrink your Docker image size.
As you can see on the screenshot - Layers table has Size column.
So, you can find - which layer of your docker image has the greatest contribution to the volume of the final image.

Docker | Build getting exiting on Interactive Shell

I am trying to run a version of Swoole with php7.3-alpine image.
When running, everything builds correctly and all of the extensions get installed correctly. However, when it comes to doing docker-compose up I get stuck in Interactive shell and then exits with code 0 so the container doesn't actually boot up correctly.
Is there anything I can do to stop this issue and stop it from running the interactive shell?
FROM composer:latest as builder
WORKDIR /app
RUN composer global require hirak/prestissimo
COPY . /app/
RUN composer install \
--no-ansi \
--no-dev \
--no-interaction \
--no-progress \
--optimize-autoloader \
--ignore-platform-reqs
RUN rm -rf docker/ composer.json composer.lock && \
touch /app/storage/logs/lumen.log
FROM php:7.3-alpine
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS=0 \
PHP_OPCACHE_MAX_ACCELERATED_FILES=7963 \
PHP_OPCACHE_MEMORY_CONSUMPTION=192
RUN set -ex \
&& apk update \
&& apk add --no-cache libffi-dev icu libsodium \
&& apk add --no-cache --virtual build-dependencies icu-dev g++ make autoconf libsodium-dev \
&& docker-php-source extract \
&& pecl install swoole redis sodium \
&& docker-php-ext-enable redis swoole sodium \
&& docker-php-source delete \
&& docker-php-ext-install -j$(nproc) pdo_mysql intl \
&& cd / && rm -fr /src \
&& apk del build-dependencies \
&& rm -rf /tmp/*
COPY --from=builder --chown=www-data:www-data /app /var/www
COPY docker/php.ini /usr/local/etc/php/php.ini
USER www-data
WORKDIR /var/www
EXPOSE 1215
docker-compose.yml
web:
build:
context: .
dockerfile: docker/Dockerfile
ports:
- "80:1215"
env_file:
- .env
output
web_1 | Interactive shell
web_1 |
web_1 exited with code 0
You need to define a CMD at the end of your dockerfile the last stage which will be used as a starting point for the container that you will run it. you can check the following URL
The Interactive Shell is there because of the original CMD of php:7.3-alpine which is php -a that gives:
Interactive shell
php >
You need to define your own CMD that starts your application and check the logs if it was not working

Categories