I am trying to setup a Symfony project using docker but it always return errors related to permissions in "cache" directory.
I have tried everything and I can't seem to find a solution.
The problem is somehow the cache folder is always created with "root" owner even with my server and php-fpm user set as www-data. Maybe because of the php-cli user?
I have tried:
- setfacl : Don't work with docker
- chown / chmod to www-data: also didn't work. it might changes the owner correctly in the begining but them gives an error in other place.
docker-compose.yml
app:
build: .
command: "tail -f /dev/null" # keep the application container running
links:
- mysql
volumes:
- .:/var/www
nginx:
build: docker/nginx/
ports:
- 8090:80
links:
- php-fpm
volumes_from:
- app
php-fpm:
build: docker/fpm
ports:
- 9000:9000
volumes_from:
- app
mysql:
image: mysql:5.7
volumes:
- ./docker/data/mysql:/var/lib/mysql
My app Dockerfile:
FROM php:5.6-cli
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y \
git \
vim \
curl \
php5-json \
php5-intl \
php5-mcrypt \
php5-mysql \
php5-apcu \
php5-gd
ADD /docker/fpm/php.ini /usr/local/etc/php/
# install composer.
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
RUN usermod -u 1000 www-data
ADD . /var/www
WORKDIR /var/www
PHP-fpm Dockerfile
FROM php:5.6-fpm
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y \
php5-json \
php5-intl \
php5-mcrypt \
php5-mysql \
php5-apcu \
php5-gd
RUN apt-get install -y php5-xdebug
ADD xdebug.ini /usr/local/etc/php/conf.d/
ADD php.ini /usr/local/etc/php/
EXPOSE 9000
WORKDIR /var/www
CMD ["php-fpm"]
Nginx Dockerfile
FROM nginx:latest
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y git vim
ADD nginx.conf /etc/nginx/
ADD symfony.conf /etc/nginx/sites-available/
RUN mkdir -p /etc/nginx/sites-enabled
RUN ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/
RUN usermod -u 1000 www-data
EXPOSE 80
EXPOSE 443
ENTRYPOINT ["nginx"]
I am out of ideas. Any suggestions?
Thank you.
I have an apache 2.2 setup with docker and at the end of the Dockerfile I have the following command:
ENTRYPOINT service apache2 start && sh /opt/fix_symfony_cache_and_logs_permissions.sh
and this in the fix_symfony_cache_and_logs_permissions script:
rm -rf /{route_to_app}/app/cache/* /{route_to_app}/app/logs/*
tailf /dev/null
It seems that your problem could be related to the fact that your php-fpm process is the one who should run with the right permissions.
EDIT: I just found out about this https://github.com/drgomesp/symfony-docker which I think would help you set it up correctly
I also had this problem. Nothing worked, chown, chmod, setfacl. Then ended up with the following solution. Warning, my solution has some security issues regarding the reading/writing. Use it on your own risk!
FROM php:5.6.29-apache
RUN apt-get update \
&& apt-get install -y git libssl-dev zlib1g-dev libicu-dev g++ \
&& docker-php-ext-install intl mbstring zip \
&& a2enmod rewrite
RUN pecl install mongo \
&& echo extension=mongo.so > /usr/local/etc/php/conf.d/mongo.ini \
&& docker-php-ext-enable mongo \
&& pecl install mongodb && \
docker-php-ext-enable mongodb
COPY apache2.conf /etc/apache2/apache2.conf
COPY . /var/www/html
WORKDIR /var/www/html
RUN rm -rf /var/www/html/var/cache/prod \
&& rm -rf /var/www/html/var/cache/dev
RUN chown -R www-data /var/www
RUN chmod -R 777 /var/www
USER www-data
RUN curl -sS https://getcomposer.org/installer | php
RUN php composer.phar install --no-interaction --optimize-autoloader
USER root
And apache2.conf file content is as follows.
User www-data
Group www-data
ErrorLog /proc/self/fd/2
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Listen 80
DocumentRoot /var/www/html/web
<Directory /var/www/html/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
DirectoryIndex disabled
DirectoryIndex app.php
Related
I have the following Dockerfile:
ARG ALPINE_VERSION=3.16
FROM alpine:${ALPINE_VERSION}
# Setup document root
WORKDIR /var/www/html
# Install packages and remove default server definition
RUN apk add --no-cache \
curl \
nginx \
php81 \
php81-ctype \
php81-curl \
php81-dom \
php81-fpm \
php81-gd \
php81-intl \
php81-mbstring \
php81-mysqli \
php81-opcache \
php81-openssl \
php81-phar \
php81-session \
php81-xml \
php81-xmlreader \
php81-sodium \
php81-xmlwriter \
supervisor \
php81-tokenizer \
php81-pdo \
php81-fileinfo
# Create symlink so programs depending on `php` still function
RUN ln -s /usr/bin/php81 /usr/bin/php
# Configure nginx
COPY docker/nginx.conf /etc/nginx/nginx.conf
# Configure PHP-FPM
COPY docker/fpm-pool.conf /etc/php81/php-fpm.d/www.conf
COPY docker/php.ini /etc/php81/php.ini
# Configure supervisord
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN chown -R nobody.nobody /var/www/html /run /var/lib/nginx /var/log/nginx
# Switch to use a non-root user from here on
USER nobody
# Add application
COPY --chown=nobody . /var/www/html/
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN cd /var/www/html && composer install --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs
# Expose the port nginx is reachable on
EXPOSE 8080
# Let supervisord start nginx & php-fpm
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Configure a healthcheck to validate that everything is up&running
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping
My Laravel app is being served, however when I get the following error trying to access it: could not find driver (QueryException, select * from sessionswhereid = <id> limit 1)
Here is my php.ini: https://pastebin.com/gHZevdw3 (too long for here).
I'm not too sure what needs to be done to enable PDO, or if my ini file is incorrect, but I'd appreciate some feedback on what can be improved.
Any help appreciated!
My project is throwing the next error after restarting the docker container:
Warning: require(/var/www/ /var/www/
/vendor/composer/./symfony/polyfill-php80/bootstrap.php): Failed to
open stream: No such file or directory in
'vendor/composer/autoload_real.php line 71
My Dockerfile:
FROM php:8.0-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/project/
# Set working directory
WORKDIR /var/www/project
# 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 \
libicu-dev \
libonig-dev \
libzip-dev
# install node
RUN curl -sL https://deb.nodesource.com/setup_current.x | bash -
RUN apt-get install -y nodejs
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --enable-gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN echo 'xdebug.client_port=9000' >> /usr/local/etc/php/php.ini
RUN echo 'xdebug.mode=debug' >> /usr/local/etc/php/php.ini
RUN echo 'xdebug.discover_client_host=true' >> /usr/local/etc/php/php.ini
RUN echo 'memory_limit = 4G' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;
# 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 permissions
COPY --chown=www:www . /var/www/project
RUN composer install
RUN npm install
RUN npm run dev
RUN php artisan storage:link
ENV PATH="vendor/bin:${PATH}"
# Change current user to www
USER www
RUN composer global require tightenco/tlint
ENV PATH="${PATH}:/home/www/.composer/vendor/bin"
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
My docker-compose:
...
project:
build:
context: ./project/.
dockerfile: Dockerfile
image: project:v0
restart: unless-stopped
tty: true
environment:
PHP_IDE_CONFIG: serverName=docker_project
XDEBUG_CONFIG: remote_host=172.17.0.1
SERVICE_NAME: project
SERVICE_TAGS: dev
volumes:
- /var/www/project/vendor/
- ./project:/var/www/project
- ./project/.docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- project-network
...
Inside container run ls vendor/symfony before restarting the container:
And after:
The error is solving if remove the vendor directory and run composer install.
I'm not a Jedi of the Docker.
Thanks for any help/suggestion!
In your volume section, you are mounting the host machine folder ./project to your container's /var/www/project:
volumes:
- /var/www/project/vendor/
- ./project:/var/www/project
- ./project/.docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
The vendor folder is built inside of you container by the RUN command. It's probably still there. But when you run docker-compose up, you instructed it to mask whatever inside with your host's ./project folder when running it. So the container don't see anything that you built before hand.
My advice is to only mount the folder that you need to change from time to time. For example:
volumes:
- /var/www/project/vendor/
# You're probably uploading files here
- ./project/public/assets:/var/www/project/public/assets
# Note: You might need to mount some other files / folders that changes
- ./project/.docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
Then the container will properly use the prebuilt /vendor while storing the thing that you need to keep in host.
For who are facing the same issue, just map the project folder except vendor folder:
services:
app:
volumes:
.:/var/www/html
/var/www/html/vendor
...
If you want to have all vendor files in the host machine, just copy them from the container to your host machine, so IDE (Like PHPSTORM) can identify composer packages:
$ cd /your/project/host/path
$ docker cp $(docker-compose ps -q app):/var/www/html/vendor .
I want to run my application in a docker container. I am able to run my PHP application in a docker container but I want to add mysql in my application and restore my sql dump in my database. And I want my application code to be cloned by dockerfile and httpd configuration with dockerfile itself.
here is my docker file
FROM centos:centos7
RUN yum -y update \
&& yum --setopt=tsflags=nodocs -y install \
gcc \
httpd \
mod_ssl \unzip \
&& rm -rf /var/cache/yum/* \
&& yum clean all
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
RUN yum remove php-cli php-common
RUN yum -y install php55w \
php55w-opcache \
php55w-xml \
php55w-mcrypt \
php55w-gd \
php55w-devel \
php55w-mysqlnd \
php55w-intl \
php55w-mbstring \
RUN yum -y install git
RUN git clone
https://USERNAME:PASSWORD#gitlab.com/xxxxxxxxxxx/Product/yyyyyyyyyyy.git
abc;
RUN rm -rf /var/cache/yum/*
RUN yum clean all
EXPOSE 80
#ADD ./abc/ /var/www/html/
COPY /abc/abclive/gitlab.abc/dev/ /var/www/html/
#WORKDIR /var/www/html/abc/abclive/gitlab.abc/dev/
ENV APACHE_DOCUMENT_ROOT /var/www/html/abc/abclive/gitlab.abc/dev/
WORKDIR /var/www/html/abc/abclive/gitlab.abc/dev/
CMD ["/usr/sbin/httpd", "-D","FOREGROUND"]
here is my docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "8090:80"
centos:
image: "centos:centos7"
# links:
# - db
# volumes:
# - .:/newvolebs
#db:
# image: "mysql:5.6"
# volumes:
# - newvolebs:/var/lib/mysql/data
# networks:
# - overlay
# environment:
# MYSQL_ROOT_PASSWORD: PASSWORD
# MYSQL_DATABASE: DATABASENAME
i have created one ebs volume attached it with my instance and make it usable with name "newvolebs"
how to mount it in my application database.
Thanks in advance.
I trying to get a php:apache server in a docker container with docker-compose to work. But after compose up I get following error message:
[client 172.21.0.1:49230] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive
my dockerfile:
FROM php:7.1-apache
RUN apt-get update -y
RUN apt-get upgrade -y
RUN apt-get install -y \
adduser \
ca-certificates \
unzip \
curl \
git \
wget \
openssl
RUN docker-php-ext-install pdo_mysql
# Installiere benötigte PHP Erweiterungen
RUN apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
# Install bcmath
RUN docker-php-ext-install bcmath pcntl
# Install Phalcon-PHP
WORKDIR /phalcon/cphalcon
# https://github.com/phalcon/cphalcon/releases
ENV PHALCON_VERSION "v3.2.1"
RUN curl -L -o "phalcon-${PHALCON_VERSION}.tar.gz" "https://api.github.com/repos/phalcon/cphalcon/tarball/${PHALCON_VERSION}" \
&& tar -xvf "phalcon-${PHALCON_VERSION}.tar.gz" --strip 1 \
&& rm "phalcon-${PHALCON_VERSION}.tar.gz" \
&& cd /phalcon/cphalcon/build \
&& ./install
# Install Phalcon Devtools
WORKDIR /usr/local/lib/phalcon-devtools
# https://github.com/phalcon/phalcon-devtools/releases
ENV PHALCON_DEVTOOLS_VERSION "v3.2.0"
RUN curl -L -o "phalcon-devtools-${PHALCON_DEVTOOLS_VERSION}.tar.gz" "https://api.github.com/repos/phalcon/phalcon-devtools/tarball/${PHALCON_DEVTOOLS_VERSION}" \
&& tar -xvf "phalcon-devtools-${PHALCON_DEVTOOLS_VERSION}.tar.gz" --strip 1 \
&& rm "phalcon-devtools-${PHALCON_DEVTOOLS_VERSION}.tar.gz" \
&& ln -s /usr/local/lib/phalcon-devtools/phalcon.php /usr/bin/phalcon \
&& chmod +x /usr/bin/phalcon
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN chmod +x /usr/local/bin/composer
# Install Dockerize
ENV DOCKERIZE_VERSION v0.5.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
WORKDIR /usr/src/fileserver
COPY config/apache/apache2.conf /etc/apache2/apache2.conf
COPY config/apache/envvars /etc/apache2/envvars
COPY config/apache/site-default.conf /etc/apache2/sites-available/000-default.conf
COPY config/apache/project.conf /etc/apache2/project.conf
COPY config/php.ini /usr/local/etc/php/
COPY run.sh /run.sh
COPY start_safe_perms.sh /start_safe_perms.sh
RUN chmod 755 /*.sh
RUN a2enmod rewrite
RUN a2enmod headers
VOLUME ["/usr/src/fileserver"]
CMD ["/run.sh"]
docker-compose:
version: '3.1'
services:
fileserver:
build: fileServer/docker
container_name: fileServer
volumes:
- ./FileServer/:/usr/src/fileserver #/usr/src/app
ports:
- "5001:80"
I think the error comes from my site-default.config, which should normally overwrites the 000-default.conf
But I don´t know why the default directory is var/www/html. It should be /usr/src/fileserver/public/ (if i do docker exec at this container, it shows the fileserver folder with index.php)
site-default.conf
<VirtualHost *:5001>
DocumentRoot /usr/src/fileserver/public
<Directory /usr/src/fileserver/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /usr/src/fileserver/docker/logs/error.log
CustomLog /usr/src/fileserver/docker/logs/access.log combined
</VirtualHost>
Can someone help me? (please ask for more informations, if they needed)
Looks like you are exposing port 5001 from local machine to port 80 on the docker instance, but then you are defining your virtual host on port 5001. So your site-default.conf it probably should read:
<VirtualHost *:80>
Notice that the error is about /var/www/html/ (some Apache default directory) and not your own /usr/src/fileserver/public
I have built an image for my app with apache, container created from it is working just fine. I am able to see the page on localhost. (file #1: docker-compose.yml)
I push image to docker hub
I recreate a container using another (file #2: docker-compose-prod.yml) for the same app and it's exited immediately without any error, container output was:
Module rewrite already enabled
httpd (pid 1) already running
Here is app service described in docker-compose.yml file #1 which I used for build:
app:
build: .
links:
- db
- memcache:memcached
- search
ports:
#- 8080:80 # when varnish enabled
- 80:80
restart: always
environment:
DB_HOST: db:3306
DB_PASSWORD: *****
SEARCH_HOST: search:9312
MEMCACHE_HOST: memcache:11211
Here is docker-compose-prod.yml, file #2 I used for deployment which contains problem:
app:
image: vendor/app
#restart: always
links:
- db
- memcache:memcached
- search
ports:
#- 8080:80 # when varnish enabled
- 80:80
environment:
DB_HOST: db:3306
DB_PASSWORD: *****
SEARCH_HOST: search:9312
MEMCACHE_HOST: memcache:11211
#networks:
#- proxy
#- backend
Containers of next services are always up and running:
- db
- memcache:memcached
- search
Some details:
I run docker-compose.yml (file#1) from within app directory:
/home/user/app1$ docker-compose up -d
and second file from another directory
/home/user/app2$ docker-compose -f docker-compose-prod.yml up -d
Here is Dockerfile:
FROM ubuntu:trusty
MAINTAINER vendor
# Install base packages
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -yq install \
curl \
apache2 \
libapache2-mod-php5 \
php5-mysql \
php5-mcrypt \
php5-gd \
php5-curl \
php-pear \
php5-memcache \
php-apc && \
rm -rf /var/lib/apt/lists/*
#ENV ALLOW_OVERRIDE **False**
RUN /usr/sbin/php5enmod mcrypt
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf && \
sed -i "s/variables_order.*/variables_order = \"EGPCS\"/g" /etc/php5/apache2/php.ini
# Add image configuration and scripts
ADD run.sh /run.sh
RUN chmod 755 /*.sh
# Configure /app folder with sample app
RUN mkdir -p /app && rm -fr /var/www/html && ln -s /app /var/www/html
WORKDIR /app
ADD . /app
RUN chmod 0777 -R /var/www/html/app/tmp
RUN ln -sf /dev/stderr /var/www/html/app/tmp/logs/error.log
RUN ln -sf /dev/stdout /var/www/html/app/tmp/logs/debug.log
RUN chown www-data:www-data /app -R
# should not be run inside container
#RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#RUN /usr/local/bin/composer install
#RUN php vendor/bin/phinx migrate
EXPOSE 80
CMD ["/run.sh"]
And run.sh
#!/bin/bash
if [ "$ALLOW_OVERRIDE" = "**False**" ]; then
unset ALLOW_OVERRIDE
else
sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf
a2enmod rewrite
fi
source /etc/apache2/envvars
exec apache2 -D FOREGROUND
It happens because really
Module rewrite already enabled
Why does it happen?
When you create container rewrite_module is disabled. And run.sh enable it.
Later, you stop (but not remove) container, and run it again. But run.sh runs everytime when container is starts. And run.sh are trying to load this module again and you receive the error.
You can ignore the error or you can modify run.sh something like:
#!/bin/bash
if [ "$ALLOW_OVERRIDE" = "**False**" ]; then
unset ALLOW_OVERRIDE
else
sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf
if [! httpd -M | grep rewrite_module]; then
a2enmod rewrite;
fi;
fi
source /etc/apache2/envvars
apache2 -D FOREGROUND
I hope I helped you.
Finally I've got it working!
Solution:
# Apache gets grumpy about PID files pre-existing
I added to run.sh following line:
rm -f /var/run/apache2/apache2.pid
See https://github.com/docker-library/php/issues/187
and https://github.com/docker-library/php/blob/bdfd0fc1d19102cd6f951614072a51eabf7191bf/5.6/apache/apache2-foreground