Symfony cache permissions with docker with nginx rsync - symfony

Following #sveneisenschmidt's workaround which utilizes rsync in a container to speed up Symfony on OSX:
https://forums.docker.com/t/how-to-speed-up-shared-folders/9322/15
I seem to have Symfony running this way, but I'm running into permissions issues with the web server that I'm not sure how to resolve in Docker.
I'm able to clear the cache via the CLI in my php-fom instance (cache:clear --env=prod --no-debug)
But the problem is when I view Symfony via app_dev.php, nginx cannot seem to write to the cache/logs directories:
Unable to write in the cache directory (/app/app/cache/dev)
I'm confused about how rsync fits into the permissions, but it seems that nginx needs more permissions than it has. Any ideas on how to resolve this?
docker_compose.yml
# Web server
nginx:
container_name: insight_nginx
build: docker/nginx
ports:
- "80:80"
links:
- php
- sync:sync
volumes_from:
- sync
# Data alias
data:
container_name: insight_data
build: docker/data/.
# Database
db:
container_name: insight_db
build: docker/db
ports:
- 3306:3306
volumes:
- "./.data/db:/var/lib/mysql"
- ./db-dump:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: root
# Application server
php:
container_name: insight_php
build: docker/php-fpm
external_links:
- insight_db:docker-mysql
environment:
DB_HOST: docker-mysql
# Syncing
volumes_from:
- sync
links:
- sync:sync
# Synchronization
### Symfony rsync workaround from here: https://forums.docker.com/t/how-to-speed-up-shared-folders/9322/15
sync:
container_name: insight_sync
build: docker/sync
command: "lsyncd -delay 1 -nodaemon -rsync /src /app"
volumes:
- /app
- "./:/src"
working_dir: /src
stdin_open: true
tty: true
nginx/Dockerfile
FROM nginx:latest
COPY symfony3.conf /etc/nginx/conf.d/symfony3.conf
#RUN usermod -u 1000 www-data
#RUN chown -R www-data:www-data /app/cache
#RUN chown -R www-data:www-data /app/logs
php-fpm/Dockerfile
FROM pvlltvk/ubuntu-trusty-php-fpm-5.6
RUN apt-get install -y \
php5-curl \
php5-sybase \
freetds-dev \
libxml2-dev
ADD freetds.conf /etc/freetds/freetds.conf
RUN echo 'alias sf="php /app/app/console"' >> ~/.bashrc
#RUN chmod -R 0777 /tmp/symfony/logs
#RUN chmod -R 0777 /tmp/symfony/cache
#ADD start.sh /start.sh
#RUN chmod +x /start.sh
WORKDIR /app
sync/Dockerfile
FROM ubuntu:16.04
RUN PACKAGES="\
rsync \
lsyncd \
" && \
apt-get update && \
apt-get install -y $PACKAGES && \
apt-get autoremove --purge -y && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
#RUN rm -rf /src/app/cache/* \
# rm -rf /src/app/logs/* \
# sudo chmod +R 777 /src/app/cache /src/app/logs
#RUN chmod -R 0777 ./app/logs
#RUN chmod -R 0777 ./app/cache

CMD instruction allows you to set a default command, which will be executed only when you run container without specifying a command. *
RUN executes the command(s) that you give in a new layer and creates a new image.**
try
CMD chown -R www-data:www-data /var/www && nginx
*http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
**https://til.codes/docker-run-vs-cmd-vs-entrypoint/

Related

How to install PHP Extensionson SourceGuardian on wordpress docker image?

I am using docker to run Wordpress on my local machine. When I want to activate Elementor Pro on my wordpress site I get this error:
PHP script '/var/www/html/wp-content/plugins/elementor-pro/abzarwp/abzarwp.php' is protected by SourceGuardian and requires a SourceGuardian loader 'ixed.7.2.lin' to be installed.
1) Click here to download the required 'ixed.7.2.lin' loader from the SourceGuardian site
2) Install the loader to /usr/local/lib/php/extensions/no-debug-non-zts-20170718
3) Edit and add 'extension=ixed.7.2.lin' directive
4) Restart the web server
My docker-composer.yml is like here:
version: "3.8"
services:
# Wordpress
wordpress:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile
container_name: poshasin_wp
ports:
- "8081:80"
restart: always
volumes:
- ./wordpress:/var/www/html
- ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
networks:
- poshasin_wp_net
# ...
and, my wordpress Dockerfile is like here:
FROM wordpress:php7.2-apache
RUN apt-get update \
&& apt-get -y --no-install-recommends install wget \
&& rm -rf /var/lib/apt/lists/*
Now, how can I solve this problem? I mean how can I install SourceGuardian in wordpress docker image?
I update my Wordpress Dockerfile like this:
FROM wordpress:php7.2-apache
# Install and enable Source Gaurdian loader
RUN PHP_VERSION=$(php -v | head -n1 | cut -d' ' -f2 | cut -d. -f1-2) \
&& mkdir -p /tmp/sourceguardian \
&& cd /tmp/sourceguardian \
&& curl -Os https://www.sourceguardian.com/loaders/download/loaders.linux-x86_64.tar.gz \
&& tar xzf loaders.linux-x86_64.tar.gz \
&& cp ixed.${PHP_VERSION}.lin "$(php -i | grep '^extension_dir =' | cut -d' ' -f3)/sourceguardian.so" \
&& echo "extension=sourceguardian.so" > /usr/local/etc/php/conf.d/15-sourceguardian.ini \
&& rm -rf /tmp/sourceguardian
Now, everything is perfect!

Installing rpy2 with Docker is unable to find R path

A Django application using Docker needs to install rpy2 as a dependency. Although I install r-base container and specify it as a dependency, when installing django requirements I keep getting:
Collecting rpy2==2.8.3 (from -r /requirements/base.txt (line 55))
Downloading rpy2-2.8.3.tar.gz (186kB)
Complete output from command python setup.py egg_info:
Error: Tried to guess R's HOME but no command 'R' in the PATH.
How can specify inside Docker where the R path is?
My server.yml looks like this:
version: '2'
services:
r:
build: ./services/r
django:
build:
context: ./myproject/
dockerfile: ./compose/django/Dockerfile
env_file:
- .env
- .env-server
environment:
- DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}#postgres:5432/${POSTGRES_USER}
depends_on:
- postgres
- r
command: /gunicorn.sh
volumes:
- ./myproject:/app
The Dockerfile for django is:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
COPY ./requirements /requirements
RUN pip install -r /requirements/production.txt \
&& pip install -r /requirements/test.txt \
&& groupadd -r django \
&& useradd -r -g django django
COPY . /app
RUN chown -R django /app
COPY ./compose/django/gunicorn.sh /gunicorn.sh
COPY ./compose/django/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r//' /entrypoint.sh \
&& sed -i 's/\r//' /gunicorn.sh \
&& chmod +x /entrypoint.sh \
&& chown django /entrypoint.sh \
&& chmod +x /gunicorn.sh \
&& chown django /gunicorn.sh
WORKDIR /app
ENTRYPOINT ["/entrypoint.sh"]
The Dockerfile for R is:
FROM r-base
It was easier to just install r inside the django container. So removing the r container and modifiying the django docker file adding this lines, worked:
RUN apt-get --force-yes update \
&& apt-get --assume-yes install r-base-core

Docker compose php-fpm, nginx, mysql and use wp-cli on nginx

I to linked 3 separate containers together:
nginx:1.10-alpine
php:7.0.6-fpm-alpine
mariadb:5.5
my goal is to run wp-cli installation before the source code being copy into the nginx container.
issues:
trying to run installation script of wp-cli from Dockerfile (my-nginx image) works fine, but when i try running any wp-cli commands it returns error: env: can't execute 'php': No such file or directory
wp-cli installation script (with the right dependencis) works also on dockerfile.php-fpm (my-php image), it returns an error as well : Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress install exists under. and offers me to run this command with the flag --allow-root, wich is bad in this case becuase i want to use wp user permissions. or to run this command as another user.
core configuretions
docker-compose.yml:
version: '2'
services:
my-nginx:
build: .
volumes:
- .:/var/www/html
ports:
- "8080:80"
links:
- my-php
- my-mysql
my-php:
build:
context: .
dockerfile: Dockerfile.php-fpm
volumes:
- .:/var/www/html
ports:
- "9000:9000"
my-mysql:
image: mariadb:5.5
volumes:
- /var/lib/mysql
nginx.conf:
server {
server_name _;
listen 80 default_server;
root /var/www/html;
index index.php index.html;
access_log /dev/stdout;
error_log /dev/stdout info;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass my-php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
wp-cli integration is cloned from : https://github.com/conetix/docker-wordpress-wp-cli
option 1
Dokerfile:
FROM nginx:1.10-alpine
# add root dir
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
ENV WP_ROOT /var/www/html
# Install requirements for wp-cli support
RUN apk update \
&& apk add less mysql-client sudo curl
# copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Add WP-CLI
RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
COPY wp-su.sh /bin/wp
RUN chmod +x /bin/wp-cli.phar \
&& sudo mv /bin/wp-cli.phar /usr/local/bin/wp
RUN wp core download --allow-root
# copy all files for current dir (should be theme or plugin folder)
COPY . ./
Dockergile.php-fpm
FROM php:7.0.6-fpm-alpine
VOLUME /var/www/html
RUN docker-php-ext-install -j $(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) iconv gd mbstring fileinfo curl xmlreader xmlwriter mysqli
option 2
Dokerfile:
FROM nginx:1.10-alpine
# add root dir
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
RUN apk update \
&& apk add less mysql-client sudo
# copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
# copy all files for current dir (should be theme or plugin folder)
COPY . ./
Dockergile.php-fpm
FROM php:7.0.6-fpm-alpine
VOLUME /var/www/html
RUN docker-php-ext-install -j $(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) iconv gd mbstring fileinfo curl xmlreader xmlwriter mysqli
# Install requirements for wp-cli support
RUN apk update \
&& apk add curl sudo less
ENV WP_ROOT /var/www/html
# Add WP-CLI
RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
COPY wp-su.sh /bin/wp
RUN chmod +x /bin/wp-cli.phar \
&& sudo mv /bin/wp-cli.phar /usr/local/bin/wp
RUN wp core download
general questions:
what is the best practice for users premissions inside dockers?
wp-cli integration from https://github.com/conetix/docker-wordpress-wp-cli/blob/master/wp-su.sh uses www-data user - should i add this user to my docker, and if so - to wich one? ngnix or php-fpm and why.
running wp donlowd core from php-fpm container appaers as if it download the files but when i try to exec when its running, none of this files shown at /var/www/html . is there a certain way to order images file mounting?
why does 'php' isn't available command from my-nginx if i volume
my-php image? i must install php also on this image?
PHP isn't installed in your nginx container. I'd argue that it should not be installed there either. Use your php fpm container to run WP CLI. Here's how I get WP CLI running using a similar setup:
docker-compose.yml:
version: '2'
services:
mysql:
image: mysql:latest
volumes:
- "../.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: my_database
MYSQL_USER: my_user
MYSQL_PASSWORD: password
phpfpm:
depends_on:
- mysql
image: my/phpfpm:latest
build: ./docker/php-fpm
volumes:
- ".:/var/www/html"
- "./docker/php-fpm/php.ini:/usr/local/etc/php/php.ini"
- "./docker/php-fpm/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini"
links:
- mysql
restart: always
extra_hosts:
- "mysite.dev:172.18.0.1" # Use the gateway address for your docker network for the ip address. This is so that PHP FPM can find nginx for the postback to do things like cron jobs with WordPress
nginx:
depends_on:
- phpfpm
ports:
- "80:80"
image: nginx:latest
volumes:
- ".:/var/www/html"
- "./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf"
links:
- phpfpm
restart: always
docker/php-fpm/Dockerfile:
FROM php:5.5-fpm
ARG INSTALL_XDEBUG=true
ENV INSTALL_XDEBUG ${INSTALL_XDEBUG}
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
pecl install xdebug && \
docker-php-ext-enable xdebug \
;fi
RUN apt-get update && apt-get install -y libz-dev libmemcached-dev libjpeg-dev libpng-dev \
&& pecl install memcached \
&& docker-php-ext-enable memcached \
&& docker-php-ext-install -j$(nproc) pdo pdo_mysql mysqli gd \
&& docker-php-ext-enable pdo pdo_mysql mysqli gd
RUN docker-php-ext-install zip \
&& docker-php-ext-enable zip
RUN curl https://getcomposer.org/download/1.2.0/composer.phar > /tmp/composer.phar \
&& chmod +x /tmp/composer.phar \
&& mv /tmp/composer.phar /usr/local/bin/composer \
&& apt-get update && apt-get install -y less \
&& curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > /tmp/wp-cli.phar \
&& chmod +x /tmp/wp-cli.phar \
&& mv /tmp/wp-cli.phar /usr/local/bin/wp
CMD ["php-fpm"]
EXPOSE 9000
I also use a memcached container, so that's why the dockerfile is also installing the memcached extension. The important parts are where it installs WP CLI and moves it into place. You can also modify the version of PHP that's installed by changing it in that dockerfile. Once you have your cluster up and running with WP CLI installed on PHP, you can run the following command to run wp cli commands inside the container:
docker exec -u www-data <container_name> wp ... # whatever command after that
I have aliases for different projects that already have the container names:
alias mywp="docker exec -u www-data mysite_phpfpm_1 wp"
and that lets me run wp commands like this:
mywp core update-db --network
mywp post meta list 2119

"Unable to write in the cache directory" with Docker and Nginx

I've set up docker with nginx and php:fpm and I'm getting the:
"Unable to write in the cache directory" Could not create cache directory
or
"/usr/share/nginx/html/app/cache/dev/annotations".
errors no matter what I try. I know this is a common error but being new to Nginx and not great on Linux and I can't seem to figure out what users I need to create and what permissions I need to create and where?
I've tried adding the below into my nginx:latest dockerfile but no luck. Any ideas?
RUN adduser www-data www-data
RUN usermod -u 1000 www-data
RUN chown -R www-data:www-data /usr/share/nginx/html/app/cache
RUN chown -R www-data:www-data /usr/share/nginx/html/app/logs
RUN rm -rf /usr/share/nginx/html/app/cache/*
RUN rm -rf /usr/share/nginx/html/app/logs/*
Dockerfile:
FROM nginx:latest
ADD . /usr/share/nginx/html
RUN adduser www-data www-data
RUN usermod -u 1000 www-data
RUN chown -R www-data:www-data /usr/share/nginx/html/app/cache
RUN chown -R www-data:www-data /usr/share/nginx/html/app/logs
RUN rm -rf /usr/share/nginx/html/app/cache/*
RUN rm -rf /usr/share/nginx/html/app/logs/*
ADD /docker_setup/default.conf /etc/nginx/conf.d/
ADD /docker_setup/nginx.conf /etc/nginx/
ENV TERM xterm
EXPOSE 80
Docker compose:
version: '2'
services:
web:
# image: nginx:latest
build: .
ports:
- "8080:80"
volumes:
- .:/usr/share/nginx/html
links:
- php
volumes_from:
- php
volumes:
- ./logs/nginx/:/var/log/nginx
php:
image: php:fpm
expose:
- 9000
volumes:
- .:/usr/share/nginx/html

(Docker) How to install dependencies, using separate Composer container, in WordPress container?

Dockerfile
FROM wordpress
ENV REFRESHED_AT 2015-08-12
ADD \
composer.json /var/www/html
ADD \
composer.lock /var/www/html
# install the PHP extensions
RUN \
apt-get -qq update && \
apt-get -y upgrade && \
apt-get install -y vim wget && \
rm -rf /var/lib/apt/lists/*
# Symlink User's "wp-content" folder into the newly installed Wordpress
RUN \
rm -rf /usr/src/wordpress/wp-content/plugins/* && \
rm -rf /usr/src/wordpress/wp-content/themes/* && \
cp -fr /usr/src/wordpress/* /var/www/html/ && \
chown -R www-data:www-data /var/www/html/
# volume for mysql database and wordpress install
VOLUME ["/var/www/html/wp-content/plugins", "/var/www/html/wp-content/themes"]
# Define working directory.
WORKDIR /var/www/html/
EXPOSE 80 3306
CMD ["apache2-foreground"]
Docker Compose File
wordpress:
build: .
links:
- mysql
- composer
volumes:
- wp-content/plugins/:/var/www/html/wp-content/plugins
- wp-content/themes/:/var/www/html/wp-content/themes
environment:
- WORDPRESS_DB_PASSWORD=__WORDPRESS_DB_PASSWORD__
- WORDPRESS_DB_NAME=__WORDPRESS_DB_NAME__
# - WORDPRESS_DB_USER=__WORDPRESS_DB_USER__
ports:
- "9888:80"
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=__WORDPRESS_DB_PASSWORD__
- MYSQL_DATABASE=__WORDPRESS_DB_NAME__
composer:
image: composer/composer
Question details
I'm able to ADD the composer.json and composer.lock files to the working directory. I can confirm that these two files are in the working directory.
What I need is for the Dockerfile (or wherever) to also automatically install the dependencies into the working directory.
According to Docker Hub, https://hub.docker.com/r/composer/composer/,
I should be able to docker run -v $(pwd):/app composer/composer install to install the dependencies but how do I do this in Dockerfile?
Also I'm confused because the -v flag, https://docs.docker.com/engine/userguide/dockervolumes/, has to do with mounting the specified host directory into the a container but I've already ADDed the necessary files to the working directory. All I want to do is install the dependencies.
Thank you for your help.
You just need to mount the current directory to /app when running your composer container. I've put together a simple example to illustrate this working at https://gist.github.com/andyshinn/e2c428f2cd234b718239.
The key parts here are the volumes for the composer part of the application and the restart: 'yes' on the primary PHP application (the application likely will not run until Composer has run so you will want it to restart).

Resources