Docker / PHP / Apache - File permissions lost when created inside container - php

i'm trying to set up a docker app with Apache and Php stack.
But when i create a new file inside my project container, the permissions losted because root user is connected by default.
How can I connect my host user ?
docker-compose.yml
version: '3'
services:
web:
container_name: web
build:
context: docker/web
volumes:
- ./app/:/var/www/app
- ./docker/web/apache/log:/var/log/apache2/app
working_dir: /var/www/app
ports:
- 80:80
dockerfile
FROM php:7.4-apache
ENV APACHE_DOCUMENT_ROOT /var/www
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libzip-dev \
git \
zip \
unzip \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install zip pdo pdo_mysql opcache \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"
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
COPY php/php.ini /usr/local/etc/php/conf.d/app.ini
COPY apache/app.conf /etc/apache2/sites-available/000-default.conf
RUN groupadd dev -g 1000
RUN useradd dev -g dev -d /home/dev -m
RUN echo '%dev ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
I tried to create a dev user in my Dockerfile but not working.

And how about this?
# Add www-data to the '1000-group'
RUN usermod -u 1000 www-data

I solved the problem by adding my dev user to sudoers and then running apache2ctl in sudo.
FROM php:7.4-apache
RUN apt-get update && apt-get install -y \
sudo \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libzip-dev \
git \
zip \
unzip \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install zip pdo pdo_mysql opcache \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"
RUN sed -ri -e 's!/var/www/html!/var/www!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!/var/www!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
COPY php/php.ini /usr/local/etc/php/conf.d/app.ini
COPY apache/app.conf /etc/apache2/sites-available/000-default.conf
RUN groupadd dev -g 1000
RUN useradd dev -g dev -d /home/dev -m
RUN echo '%dev ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER dev:dev
CMD ["sudo", "/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Related

Docker - unable to run supervisor

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

Docker/Symfony/PhpUnit - No logs displaying in console when running PhpUnit in container

I don't have logs when running bin/phpunit on the console in my web container.
My web dockerfile:
FROM php:7.4-apache
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libzip-dev \
sudo \
unzip \
zip \
git \
nano \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install zip pdo pdo_mysql opcache \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN pecl install xdebug && docker-php-ext-enable xdebug
COPY ./apache/chat-app.conf /etc/apache2/sites-available/000-default.conf
COPY ./php/php.ini /usr/local/etc/php/conf.d/app.ini
RUN curl --silent --show-error https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN "date"
RUN sed -ri -e 's!/var/www/html!/var/www!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!/var/www!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN a2enmod rewrite
RUN a2enmod headers
When i run phpunit:
What's wrong ?
I've tried to add xdebug but it's also not working.
I'm on ubuntu 20.04 and I use Tilix console.
Finally, I removed symfony/phpunit-bridge package and use phpunit/phpunit instead.
composer remove --dev symfony/phpunit-bridge
composer require --dev phpunit/phpunit
Now, logs are displaying on the console in my web container.

While running docker build it can't read /etc/php/7.2/apache2/php.ini: No such file or directory

While i am building the docker file i am getting the error that can't read /etc/php/7.2/apache2/php.ini: No such file or directory but in the local path file was available.
I need to know how to give the access to the file which is available in my local.
No such file or directory Error image
My DOCKERfile
FROM ubuntu:16.04
MAINTAINER Sreekanth G S <mail#sreekanth.in>
#ENV http_proxy=http://sysfp0t.its.unc.edu:80
#ENV https_proxy=http://sysfp0t.its.unc.edu:80
#RUN DEBIAN_FRONTEND=noninteractive \
# apt-get -o Acquire::http::Proxy="http://sysfp0t.its.unc.edu:80" update && \
# apt-get -o Acquire::http::Proxy="http://sysfp0t.its.unc.edu:80" install -y language-pack-en-base &&\
# export LC_ALL=en_US.UTF-8 && \
#` export LANG=en_US.UTF-8
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y software-properties-common
#RUN cat /etc/locale.gen
#RUN cat /etc/environment
#RUN LANG=en_US.UTF-8 locale-gen --purge en_US.UTF-8 && echo -e 'LANG="en_US.UTF-8"\nLANGUAGE="en_US:en"\n' > /etc/default/locale && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && echo "LC_ALL=en_US.UTF-8" > /etc/environment && echo "LANG=en_US.UTF-8" >> /etc/environment && locale-gen "en_US.UTF-8" && export LC_ALL=en_US.UTF-8 && export LANG=en_US.UTF-8 && add-apt-repository ppa:ondrej/php
#RUN apt-get -o Acquire::http::Proxy="http://sysfp0t.its.unc.edu:80" update && apt-get -o Acquire::http::Proxy="http://sysfp0t.its.unc.edu:80" install -y software-properties-common language-pack-en-base && \
#LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php && apt-get -o Acquire::http::Proxy="http://sysfp0t.its.unc.edu:80" update && apt-get -o Acquire::http::Proxy="http://sysfp0t.its.unc.edu:80" install -y \
RUN echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" >> /etc/apt/sources.list && echo "deb-src http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" >> /etc/apt/sourceis.list && apt-get update --allow-unauthenticated
RUN cat /etc/apt/sources.list && ls -l /etc/apt/
RUN apt-get install -y --allow-unauthenticated apache2-bin libapache2-mod-php php-curl php-ldap php-mysql php-mcrypt \
php-gd php-xml patch curl vim git mysql-client wget unzip
RUN phpenmod mcrypt && phpenmod gd
RUN a2enmod xml2enc && a2enmod rewrite
RUN sed -i 's/variables_order = .*/variables_order = "EGPCS"/' /etc/php/7.2/apache2/php.ini
RUN sed -i 's/variables_order = .*/variables_order = "EGPCS"/' /etc/php/7.2/cli/php.ini
RUN useradd --uid 1000 --gid 50 docker
RUN echo export APACHE_RUN_USER=docker >> /etc/apache2/envvars
RUN echo export APACHE_RUN_GROUP=staff >> /etc/apache2/envvars
COPY 000-default.conf /etc/apache2/sites-enabled/000-default.conf
RUN chown -R docker /var/www/html
WORKDIR /var/www/html
RUN wget -q "http://www.sentrifugo.com/home/downloadfile?file_name=Sentrifugo.zip" -O Sentrifugo.zip
#COPY Sentrifugo.zip /var/www/html/
RUN unzip Sentrifugo.zip && mv Sentrifugo_3.2 sentrifugo
WORKDIR /var/www/html/sentrifugo
RUN chown -R docker .
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
RUN chmod 777 -R public/downloads public/uploads public/email_constants.php \
public/emptabconfigure.php \
public/site_constants.php \
public/db_constants.php \
public/application_constants.php \
public/mail_settings_constants.php \
logs/application.log \
application/modules/default/plugins/AccessControl.php \
install
VOLUME /var/www/html/sentrifugo/public/uploads
VOLUME /var/www/html/sentrifugo/public/downloads
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 80
docker-compose.yml file
version: '3'
services:
sentrifugo:
build:
context: .
dockerfile: Dockerfile-sentrifugo
container_name: sentrifugo
ports:
- "8080:80"
- "8443:443"
devices:
- "/dev/tty:/dev/tty"
links:
- "mariadb:mariadb"
mariadb:
build:
context: .
dockerfile: Dockerfile-mariadb
container_name: mariadb
ports:
- "3306:3306"
Dockerfile-mariadb
from mariadb:latest
ENV MYSQL_RANDOM_ROOT_PASSWORD=true
ENV MYSQL_DATABASE=sentrifugo
ENV MYSQL_USER=sentrifugo
ENV MYSQL_PASSWORD=54y6RxN7GfC7aes3
EXPOSE 3306
My local folder Image
My local drive path and file details
Command to build the script
docker-compose up -d
DOCKER FILE
FROM ubuntu:trusty
MAINTAINER Oleksandr Simonov <asimonov#gmail.com>
RUN apt-get update && apt-get install -y \
apache2-bin \
libapache2-mod-php5 \
php5-curl \
php5-ldap \
php5-mysql \
php5-mcrypt \
php5-gd \
patch \
curl \
vim \
git \
mysql-client
RUN php5enmod mcrypt
RUN php5enmod gd
RUN a2enmod rewrite
RUN sed -i 's/variables_order = .*/variables_order = "EGPCS"/' /etc/php5/apache2/php.ini
RUN sed -i 's/variables_order = .*/variables_order = "EGPCS"/' /etc/php5/cli/php.ini
RUN useradd --uid 1000 --gid 50 docker
RUN echo export APACHE_RUN_USER=docker >> /etc/apache2/envvars
RUN echo export APACHE_RUN_GROUP=staff >> /etc/apache2/envvars
COPY 000-default.conf /etc/apache2/sites-enabled/000-default.conf
COPY . /var/www/html
RUN chown -R docker /var/www/html
WORKDIR /var/www/html
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 80
It works for me error solved.

Combining php:7.2-alpine with nginx in ond Dockerfile

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.

Access my dockerized Laravel app in local

I am trying to dockerize a PHP / Laravel / VueJS app.
Here is the Dockerfile
FROM php:7.2-fpm
LABEL maintainer="contact#kendozone.com"
LABEL version="1.0.0"
LABEL description="Kendozone is a online tournament webapp coded with PHP / Laravel"
ENV node_version 10.8.0
ENV npm_version 6.4.1
ENV NVM_DIR /.nvm
ENV APP_DIR="/var/www"
ENV APP_PORT="80"
ENV DOCKER_FOLDER="docker/local"
RUN echo "deb http://ftp.de.debian.org/debian stretch main " >> /etc/apt/sources.list \
&& apt-get update -y
RUN apt-get install -y openssl zip unzip git gcc make automake \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libmagickwand-dev vim --no-install-recommends
RUN apt-get purge --auto-remove -y g++ \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install pdo pdo_mysql mbstring zip -j$(nproc) iconv -j$(nproc) gd
WORKDIR $APP_DIR
COPY . $APP_DIR
RUN ls $APP_DIR \
&& touch $APP_DIR/database/sqlite.db \
&& mv $DOCKER_FOLDER/.env.local .env \
&& chown -R www-data:www-data $APP_DIR
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer install --no-interaction
RUN mkdir -p $NVM_DIR && chown -R www-data:www-data $NVM_DIR \
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash \
&& [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" \
&& nvm install ${node_version}
ENV NODE_PATH $NVM_DIR/v$node_version/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$node_version/bin:$PATH
RUN npm install --save-exact imagemin-pngquant#5.0.* \
&& npm install \
&& npm run production \
&& php artisan key:generate \
&& php artisan migrate --seed
EXPOSE $APP_PORT
CMD php artisan serve --host=0.0.0.0 --port=$APP_PORT
the last line is supposed to run local server
Then I build it with :
docker build . -t xoco/kendozone:local-1.0.3
Then I run it with:
docker run -p 80:80 xoco/kendozone:local-1.0.3 -d bash
I can see on terminal the output:
[25-Oct-2018 19:56:40] NOTICE: fpm is running, pid 1
[25-Oct-2018 19:56:40] NOTICE: ready to handle connections
EDIT:
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fee456200b85 xoco/kendozone:local-1.0.3 "docker-php-entrypoi…" 2 hours ago Up 2 hours 9000/tcp, 0.0.0.0:8080->80/tcp youthful_keldysh
Which seems to mean that everything is ok
But I can't reach my app on 127.0.0.1
What am I missing
You have to add host configuration file while using docker and add an entry for it inside /etc/hosts and try again.
I solved it with this command:
docker run -p 80:80 xoco/kendozone:local-1.0.3
Just remove -d bash at the end, and it should be OK

Categories