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.
Related
I have problem with my docker image for php-app and microweber/screen package (in fact with phantomjs)
I have dockerfile like:
FROM php:8.1-fpm
# 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 \
locales \
git \
unzip \
zip \
bash \
curl \
libfreetype6-dev \
nodejs \
npm \
libxml2-dev \
libzip-dev \
libssl-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install \
exif \
pcntl \
soap \
zip \
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"]
While I working with package microweber/screen (phantomjs) I have en error like that:
1 => "140611204196288:error:25066067:DSO support routines:DLFCN_LOAD:could not load the shared library:dso_dlfcn.c:185:filename(libssl_conf.so): libssl_conf.so: cannot open shared object file: No such file or directory"
2 => "140611204196288:error:25070067:DSO support routines:DSO_load:could not load the shared library:dso_lib.c:244:"
3 => "140611204196288:error:0E07506E:configuration file routines:MODULE_LOAD_DSO:error loading dso:conf_mod.c:285:module=ssl_conf, path=ssl_conf"
4 => "140611204196288:error:0E076071:configuration file routines:MODULE_RUN:unknown module name:conf_mod.c:222:module=ssl_conf"
Could anyone help with that?
Thanks!
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.
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"]
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..
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"]
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