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

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).

Related

How to not show wordpress installation page at first time?

I'm building a website using docker-compose and 3 docker containers (mariadb/nginx/wordpress) and I want to install wordpress in a script, so my wordpress Dockerfile looks like this
from debian:buster
run apt-get update -y;\
apt-get install -y curl mariadb-client\
php php7.3 php7.3-fpm php7.3-mysql php-common php7.3-cli\
php7.3-common php7.3-json php7.3-opcache php7.3-readline\
php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc\
php-zip
run mkdir -p /var/www/html;\
cd /var/www/html
add conf/php-fpm.conf /etc/php/7.3/fpm/pool.d/www.conf
add conf/init_wp.sh /tmp
run mkdir -p /var/run /run/php
run curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
run chmod +x wp-cli.phar
run mv wp-cli.phar /usr/local/bin/wp
workdir /var/www/html
run wp core download --allow-root
run chown -R www-data:www-data /var/www/html
cmd [ "sh", "/tmp/init_wp.sh" ]
and my entrypoint script looks like this
FILE=/var/www/html/.exist
if [ ! -f "$FILE" ]
then
echo "Setting up wordpress"
rm -rf /var/www/html/wp-config.php
wp config create --dbname=$DB_NAME --dbuser=$WP_USER --dbpass=$WP_PASSWORD --dbhost="mariadb" --path="/var/www/html/" --allow-root --skip-check
wp core install --url="localhost" --title="inception" --admin_user=$ADMIN_USER --admin_password=$ADMIN_PASSWORD --admin_email=$ADMIN_EMAIL --path="/var/www/html/" --allow-root
wp user create testuser testuser#student.42.fr --role=author --user_pass="abc123" --allow-root
touch /var/www/html/.exist
fi
echo "Wordpress setup done"
exec php-fpm7.3 -F -R
but when I load the website for the first time I still get redirected to the wordpress installation page, shouldn't wp config create and wp core install do this for me ?
What did I do wrong?
mariadb works fine after the wordpress installation so the problem doesn't seem to come from here, so am I missing an element in the install script ?

Impossible to start Symfony 5 server on Docker container (symfony serve -d)

I trying to create Docker container to contenerized my Symfony 5 application.
I create first a Dockerfile
FROM php:7.4-fpm-alpine
# Update
RUN apk --no-cache update
RUN apk --no-cache add bash git
# Install Node
RUN apk --no-cache add --update nodejs npm
RUN apk --no-cache add --update python3
RUN apk --no-cache add --update make
RUN apk --no-cache add --update g++
# Install pdo
RUN docker-php-ext-install pdo_mysql
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Symfony CLI
RUN curl -sS https://get.symfony.com/cli/installer | bash && mv /root/.symfony/bin/symfony /usr/local/bin/symfony
# WORK DIR
COPY . /var/www/html
WORKDIR /var/www/html
RUN composer update
RUN composer install
RUN npm install
# Start Symfony server on Port 8000
EXPOSE 8000
RUN symfony serve -d
Then I created a docker-compose.yml file (where I simply redirect port 8000 of the container to port 8080 on my machine).
version: '3.8'
services:
php-fpm:
container_name: infolea
build: ./
ports:
- 8080:8000
volumes:
- ./:/var/www/html
Then, I build my image docker-compose build.
Then, I run my image docker-compose up -d.
On my browser, the localhost:8080 link doesn't display anything.
Then I restart the symfony server by typing symfony serve -d on the terminal of my container and on localhost:8080 I can see my application working.
Something is weard, is that when I verified if my server is not started yet on my docker container terminal, I got this :
docker container terminal
What i want, it's to start my Symfony server dirrectly, without retapping symfony serve -d.
How can i do it ?
Try using CMD istead of RUN
CMD ["/usr/local/bin/symfony", "local:server:start" , "--port=8000", "--no-tls"]
see https://docs.docker.com/engine/reference/builder/#cmd

How to download and unzip in Dockerfile

So, I have, it works, but I want to change the way to immediately download the file and unpack it:
Dockerfile
FROM wordpress:fpm
# Copying themes from local
COPY ./wordpress/ /var/www/html/wp-content/themes/wordpress/
RUN chmod -R 777 /var/www/html/
How can I immediately download the file from the site and unzip it to the appropriate folder?
docker-compose.yml
wordpress:
build: .
links:
- db:mysql
nginx:
image: raulr/nginx-wordpress
links:
- wordpress
ports:
- "8080:80"
volumes_from:
- wordpress
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: qwerty
I tried:
#install unzip and wget
RUN \
apt-get update && \
apt-get install unzip wget -y && \
rm -rf /var/lib/apt/lists/*
RUN wget -O /var/www/html/type.zip http://wp-templates.ru/download/2405 \
&& unzip '/var/www/html/type.zip' -d /var/www/html/wp-content/themes/ && rm
/var/www/html/type.zip || true;
Dockerfile has "native command" for copying and extracting .tar.gz files.
So you can change archive type from .zip to .tar.gz (maybe in future versions zip also will be supported, I'm not sure) and use ADD instead of COPY.
Read more about ADD
Best to use a multistage docker build. You will need the latest version of docker and buildkit enabled. Then do something along these lines
# syntax=docker/dockerfile:1
from alpine:latest as unzipper
apk add unzip wget curl
RUN mkdir /opt/ ; \
curl <some-url> | tar xvzf - -C /opt
FROM wordpress:fpm
COPY --from unzipper /opt/ /var/www/html/wp-content/themes/wordpress/
Even better is if there is a Docker image built already with the stuff in you want you just need the 'copy --from' line and give it the image name.
Finally dont worry about any mess in the 1st stage as its discarded when the build completes, so the fact its alpine, and not using no-cache is irrelevant, and none of the installed packages end up in the final image
Found more guidance for remote zipped files in Docker documentation
Because image size matters, using ADD to fetch packages from remote
URLs is strongly discouraged; you should use curl or wget instead.
That way you can delete the files you no longer need after they’ve
been extracted and you don’t have to add another layer in your image.
For example, you should avoid doing things like:
ADD https://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all
And instead, do something like:
RUN mkdir -p /usr/src/things \
&& curl -SL https://example.com/big.tar.xz \
| tar -xJC /usr/src/things \
&& make -C /usr/src/things all

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

Resources