I have a need to combine the php:7.2 alpine with nginx in one dockerfile( in one docker image ) for production deployment of laravel app.
So I tried my way and I can only add nginx package. I can't find any nginx conf file in my image. I found many someone's images in docker hub. I tried a lot with this images and not working well.
Here is my docker file.
FROM php:7.2-alpine
RUN apk upgrade --update -q \
&& apk --no-cache -q add openssl zip unzip git mysql-client vim coreutils freetype-dev libpng-dev libjpeg-turbo-dev freetype libpng libjpeg-turbo libltdl libmcrypt-dev \
&& docker-php-ext-configure gd \
--with-gd \
--with-freetype-dir=/usr/include/ \
--with-png-dir=/usr/include/ \
--with-jpeg-dir=/usr/include/ && \
NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \
&& docker-php-ext-install -j$(nproc) gd pdo pdo_mysql opcache zip calendar \
&& apk del --no-cache -q freetype-dev libpng-dev libjpeg-turbo-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apk --update add \
supervisor
RUN apk add --update nginx && rm -rf /var/cache/apk/*
RUN mkdir -p /tmp/nginx/client-body
COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
#COPY website /usr/share/nginx/html
WORKDIR /var/www
COPY ./workspace-api /var/www
COPY ./workspace-api/supervisord.conf /etc/supervisord.conf
ADD ./workspace-api/root /etc/crontabs/
ADD ./workspace-api/php.ini /usr/local/etc/php
RUN composer install
RUN chmod -R 755 /var/www
RUN chmod +x /var/www/supervisor.sh
RUN /var/www/supervisor.sh
CMD ["nginx", "-g", "daemon off;"]
PS: I have docker-compose file for multi container app. But in this case, I only need to build all in one image for laravel.
Related
I try to install supervisor on my local docker container where I run php8.1 and laravel 9.
But I have following error:
'PermissionError: [Errno 13] Permission denied: '/var/log/supervisor/supervisord.log'
This is my config:
supervisor.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
stopwaitsecs=3600
stdout_logfile_maxbytes=5MB
php dockerfile:
FROM php:8.1.13-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
nano \
libxslt1.1 \
libxslt1-dev \
unzip \
git \
gnupg \
libpq-dev \
libzip-dev \
iputils-ping \
supervisor \
&& docker-php-ext-install zip \
gd \
mysqli pdo pdo_mysql \
&& docker-php-ext-enable pdo_mysql \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-configure intl \
RUN echo "memory_limit='512M'" >> /usr/local/etc/php/conf.d/php-extra.ini;
ENV PHP_IDE_CONFIG "serverName=new_app"
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer --version=2.3.8 && chmod +x /usr/bin/composer
# Set timezone
RUN rm /etc/localtime \
&& ln -s /usr/share/zoneinfo/Europe/Warsaw /etc/localtime \
&& "date" \
&& printf '[PHP]\ndate.timezone = "Europe/Warsaw"\n' > /usr/local/etc/php/conf.d/tzone.ini \
RUN usermod -aG docker $USER
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
WORKDIR /var/www/html
How can I fix it?
Regards.
1.
i saw these in your dockerfile, your file does not match,
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
i suggest to change to below:
COPY supervisord.conf /etc/supervisor/supervisord.conf
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
2.
Add this to your dockerfile, to replace the original line, and try again. Good luck
RUN usermod -aG docker $USER &&\
mkdir -p /var/www/html/storage/logs &&\
touch /var/www/html/storage/logs/worker.log
That's my dockerfile setup. When I use Laravel Dompdf the error will show "iconv(): Wrong charset, conversion from utf-8' to us-ascii//TRANSLIT' is not allowed"
And I have been checked the PHP ini, the iconv has been enabled. In my docker file also added the iconv installation command. It still doesn't work. Any solutions for my docker setting?
FROM php:7.3.33-fpm-alpine
# Fix: iconv(): Wrong charset, conversion from UTF-8 to UTF-8//IGNORE is not allowed in Command line code on line 1
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
# Install php extensions
RUN apk update \
&& apk add --no-cache libzip-dev libmcrypt libmcrypt-dev zlib-dev \
&& docker-php-ext-install exif zip bcmath mysqli pdo pdo_mysql ctype json
# Install GD extensions
RUN apk add --no-cache freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev && \
docker-php-ext-configure gd \
--with-gd \
--with-freetype-dir=/usr/include/ \
--with-png-dir=/usr/include/ \
--with-jpeg-dir=/usr/include/ && \
NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \
docker-php-ext-install -j${NPROC} gd && \
apk del --no-cache freetype-dev libpng-dev libjpeg-turbo-dev
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN apk --no-cache update \
&& apk --no-cache add make bash g++ zlib-dev libpng-dev \
&& rm -fr /var/cache/apk/*
# Install npm for Laravel Mix
RUN apk add npm
RUN apk add nodejs-lts --update
RUN npm install -g npm
WORKDIR /application
EXPOSE 9000
# Start services
CMD ["php-fpm"]
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.12/community/ --allow-untrusted gnu-libiconv=1.15-r2
that's work to me
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"]
any idea how can i install the php Ldap extension in dockerfile
FROM php:7.2-fpm-alpine
i tried the following
RUN docker-php-ext-configure ldap --prefix=/usr/local/php --with-ldap=/usr/lib/i386-linux-gnu
RUN docker-php-ext-install ldap
but when i build docker , i get the error message
configure: error: Cannot find ldap.h
ERROR: Service 'php' failed to build: The command '/bin/sh -c docker-php-ext-install ldap' returned a non-zero code: 1
PS: it is alpine so 'apt-get' wont work here, instead 'apk add'
If you are experiencing configure: error: Cannot find ldap.h
try to add this line in your Alpine based Dockerfile
RUN apk add ldb-dev libldap openldap-dev
You need to use openldap-dev to have the ldap.h file to install ldap with the docker-php-ext-install script.
Something like this works:
FROM php:8.0.2-fpm-alpine
RUN apk update \
&& apk add --no-cache --virtual .build-dependencies-in-virtual-world openldap-dev \
&& docker-php-ext-install ldap \
&& docker-php-ext-enable ldap \
&& apk del .build-dependencies-in-virtual-world
For me, I used this my Laravel project:
My Dockerfile for a Laravel app
I had to use line 5 because id get this if I don't:
Undefined constant "LdapRecord\Models\Attributes\LDAP_ESCAPE_FILTER"
This is working for me:
FROM php:7.4.9-fpm-alpine3.12
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
# Install PHP extensions
RUN install-php-extensions ldap
fixed with the following dockerfile :
FROM php:7.2-fpm-alpine
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# lumen packages
RUN apk add openldap-back-mdb
RUN apk add --update --virtual .build-deps autoconf g++ make zlib-dev curl-dev libidn2-dev libevent-dev icu-dev libidn-dev openldap libxml2-dev
RUN docker-php-ext-install intl soap
RUN docker-php-ext-install mbstring tokenizer mysqli pdo_mysql json hash iconv
RUN apk --update --no-cache add php7-ldap libldap php-ldap openldap-clients openldap openldap-back-mdb
RUN apk add --no-cache ldb-dev
RUN ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so \
&& ln -s /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so
#RUN docker-php-ext-configure ldap --prefix=/usr/local/php --with-ldap=/usr/lib/libldap.so
#RUN docker-php-ext-install ldap
ARG DOCKER_PHP_ENABLE_LDAP
RUN echo -n "With ldap support: " ; if [[ "${DOCKER_PHP_ENABLE_LDAP}" = "on" ]] ; then echo "Yes"; else echo "No" ; fi && \
if [ -z ${DOCKER_USER_UID+x} ]; then echo "DOCKER_USER_UID is unset"; DOCKER_USER_UID=1000; else echo "DOCKER_USER_UID is set to '$DOCKER_USER_UID'"; fi && \
if [ -z ${DOCKER_USER_GID+x} ]; then echo "DOCKER_USER_GID is unset"; DOCKER_USER_GID=1000; else echo "DOCKER_USER_GID is set to '$DOCKER_USER_GID'"; fi
# Enable LDAP
RUN if [ "${DOCKER_PHP_ENABLE_LDAP}" != "off" ]; then \
# Dependancy for ldap \
apk add --update --no-cache \
libldap && \
# Build dependancy for ldap \
apk add --update --no-cache --virtual .docker-php-ldap-dependancies \
openldap-dev && \
docker-php-ext-configure ldap && \
docker-php-ext-install ldap && \
apk del .docker-php-ldap-dependancies && \
php -m; \
else \
echo "Skip ldap support"; \
fi
RUN pecl install raphf propro
RUN docker-php-ext-enable raphf propro
RUN pecl install pecl_http
RUN echo -e "extension=raphf.so\nextension=propro.so\nextension=iconv.so\nextension=http.so" > /usr/local/etc/php/conf.d/docker-php-ext-http.ini
RUN rm -rf /usr/local/etc/php/conf.d/docker-php-ext-raphf.ini
RUN rm -rf /usr/local/etc/php/conf.d/docker-php-ext-propro.ini
RUN rm -rf /tmp/*
COPY ./app /var/www/html/
RUN chown -R www-data:www-data /var/www/html/
RUN chmod -R 755 /var/www/html/
WORKDIR /var/www/html/
RUN composer install
You can try the ldb-dev alpine package.`
FROM php:7.2-fpm-alpine
RUN apk add --no-cache ldb-dev
PHP configure not finding LDAP header libraries
How can I automatically run php-fpm command when I start the docker container?
I have this Dockerfile:
FROM composer:1.6.5 as build
WORKDIR /var/www
COPY . /var/www
RUN composer install
FROM php:7.2-fpm
USER root
RUN apt-get update && apt-get install -y \
build-essential \
mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
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
RUN apt-get update -y \
&& apt-get install -y nginx
RUN rm -rf /var/www/html
COPY --chown=www-data:www-data --from=build /var/www /var/www
COPY nginx.conf /etc/nginx/sites-enabled/default
EXPOSE 80
RUN service nginx restart
CMD ["nginx", "-g", "daemon off;"]
I know I should run php-fpm because when I access 127.0.0.1:8080 it doesn't show my webpage. But when I ssh into the container and run php-fpm then visit 127.0.0.1:8080 again it will show the webpage.
Should I add CMD ["php-fpm"] below? Is it okay to have two CMD in the Dockerfile?
It suggest that you create a start_service.sh script or whatever. to start both php_fpm and nginx when you start the container.
If the script its a good idea to both processes as daemons or in the background. And then just wait forever.
#!/bin/bash
"start php_fpm"
"start_iginx"
sleep infinite
In the CMD in the Dockerfile you just call your script instead of nginx.