I'm new to docker and laravel.
I'm trying to dockerize a laravel application, the source code is not mine.
I used Sail for local tests, but now I want to build a production image. So I write a dockerfile with my deps:
FROM php:8.1.4-fpm-alpine3.15 as laravel-deps
RUN apk add --no-cache \
freetype-dev \
libpng-dev \
libjpeg-turbo-dev \
libtool \
libwebp-dev \
libzip-dev \
mariadb-dev \
pcre-dev ${PHPIZE_DEPS} \
zip \
&& pecl install redis \
&& docker-php-ext-configure gd --with-freetype \
&& docker-php-ext-install \
bcmath \
mysqli \
pcntl \
pdo_mysql \
zip \
-j$(nproc) gd \
&& docker-php-ext-enable \
bcmath \
pcntl \
redis \
zip \
&& apk del pcre-dev ${PHPIZE_DEPS} \
&& rm -rf /tmp/pear
#install composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
WORKDIR /srv/app
#delete when change logs in .env file
COPY --chown=www-data:www-data storage storage
COPY artisan .
COPY public/vendor/js-localization public/vendor/js-localization
COPY bootstrap bootstrap
COPY database database
COPY routes routes
COPY config config
COPY composer.json .
COPY composer.lock .
COPY app app
RUN composer install --optimize-autoloader --no-dev \
&& php artisan js-localization:export
FROM node:alpine as node-modules
WORKDIR /usr/app
COPY --from=laravel-deps /srv/app/public/vendor/js-localization public/vendor/js-localization
COPY .npmrc .
COPY resources resources
COPY package.json .
COPY package-lock.json .
COPY webpack.mix.js .
#isntalling node modules
RUN npm ci \
&& npm run prod
FROM laravel-deps as laravel-app
WORKDIR /srv/app
COPY .env .env
COPY public public
COPY resources resources
COPY --from=node-modules --chown=www-data:www-data /usr/app/public public
My problem comes out when I try to do php artisan migrate in my PHP container:
Call to undefined function App\Actions\Myfunct\myfunct()
Also, when I access my app via browse I get a similar error, but in my view:
Call to undefined function brand_assets()
The crashed line in my view is:
<link rel="apple-touch-icon" sizes="57x57" href="<?php echo e(brand_assets('favicon/apple-icon-57x57.png')); ?>">
It is some lib or something I missing?
Update
I think my autoload is not working. I ran the dump-autoload, and despite that I sill get the same error.
I found my error. I didn't COPY the helper dir into my Img. That's why my helpers didn't show up.
Related
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.
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.
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
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
I have an symfony application, and i want to create docker container, that contains all needed files. The purpose is to spin up/down containers for scaling, without any other deploying processes. Just with given DB connection.
Building means, pretty much just git clone and running composer install, but some of symfony's post install scripts fail without configured DB.
Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
Can i not run these scripts when i'm building container? Or what the proper way to do symfony deployment?
Using Symfony 3.2, Doctrine2, PHP 7.1
The proper way to build a container without running the db script is by not running there scripts in the build,
Here is the one we use in production for api-platform:
FROM php:7.1-fpm-alpine
RUN apk add --no-cache --virtual .persistent-deps \
git \
icu-libs \
zlib \
postgresql-client
ENV APCU_VERSION 5.1.8
RUN set -xe \
&& apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
icu-dev \
zlib-dev \
postgresql-dev \
sqlite-dev \
pcre-dev \
&& docker-php-ext-install \
intl \
mbstring \
pdo_mysql \
pdo_pgsql \
pdo \
pgsql \
zip \
pdo_sqlite \
&& pecl install \
apcu-${APCU_VERSION} \
&& docker-php-ext-enable --ini-name 20-apcu.ini apcu \
&& docker-php-ext-enable --ini-name 05-opcache.ini opcache \
&& apk del .build-deps
COPY docker/php/php.ini /usr/local/etc/php/php.ini
COPY docker/php/install-composer.sh /usr/local/bin/docker-app-install-composer
RUN chmod +x /usr/local/bin/docker-app-install-composer
RUN set -xe \
&& apk add --no-cache --virtual .fetch-deps \
openssl \
&& docker-app-install-composer \
&& mv composer.phar /usr/local/bin/composer \
&& apk del .fetch-deps
ARG SYMFONY_ENV=dev
ENV SYMFONY_ENV=dev
RUN if [ "$SYMFONY_ENV" -ne "dev" ]; then \
sed -i '1 a xdebug.remote_enable=1' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
sed -i '1 a xdebug.remote_handler=dbgp' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
sed -i '1 a xdebug.remote_autostart=0' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
sed -i '1 a xdebug.remote_connect_back=1 ' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
sed -i '1 a xdebug.remote_port=9001' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
sed -i '1 a xdebug.remote_log=/var/log/xdebug_remote.log' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
pecl install \
xdebug \
&& docker-php-ext-enable xdebug; \
fi;
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER 1
WORKDIR /srv/quotatis
COPY composer.json ./
COPY composer.lock ./
RUN mkdir -p \
var/cache \
var/logs \
var/sessions \
&& composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest -o -a \
&& composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress --no-suggest \
&& composer clear-cache \
# Permissions hack because setfacl does not work on Mac and Windows
&& chown -R www-data var
COPY app app/
COPY bin bin/
COPY src src/
COPY web web/
COPY etc etc/
RUN composer dump-autoload --optimize --classmap-authoritative --no-dev
COPY docker/php/start.sh /usr/local/bin/docker-app-start
RUN chmod +x /usr/local/bin/docker-app-start
CMD ["docker-app-start"]
You could see the whole stack (nginx + fpm + varnish on github)
The line that you need in here is this one
composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress --no-suggest