Warning: mysqli_real_connect(): (HY000/1698): Access denied for user 'root'#'localhost' - Docker - wordpress

I'm attempting to bring a Docker image of Wordpress up with supervisor only struggling. The code is from a reputable source:
https://github.com/how2dock/docbook/tree/master/ch01/supervisor
Here's my Dockerfile
FROM ubuntu:19.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get -y install \
apache2 \
php7.2 \
php7.2-mysql \
supervisor \
wget
RUN echo 'mysql-server mysql-server/root_password password root' | debconf-set-selections && \
echo 'mysql-server mysql-server/root_password_again password root' | debconf-set-selections
RUN apt-get install -qqy mariadb-server
RUN wget http://wordpress.org/latest.tar.gz && \
tar xzvf latest.tar.gz && \
cp -R ./wordpress/* /var/www/html && \
rm /var/www/html/index.html
RUN (/usr/bin/mysqld_safe &); sleep 5; mysqladmin -u root -proot create wordpress
COPY wp-config.php /var/www/html/wp-config.php
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 80
CMD ["/usr/bin/supervisord"]
and a portion of my wp-config.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', 'root');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
supervisord.conf
[supervisord]
nodaemon=true
[program:mysqld]
command=/usr/bin/mysqld_safe
autostart=true
autorestart=true
user=root
[program:httpd]
command=/bin/bash -c "rm -rf /run/httpd/* && /usr/sbin/apachectl -D FOREGROUND"
Build the container; docker build -t wordpress . and run the webserver with docker run -d -p 82:80 wordpress. Browse to http://localhost:82 expecting to find a shiny new WP install - only to find:
Access denied for user 'root'#'localhost'
I've tried several database username/password combinations. I'm a newbie to Docker. What am I missing?

Why you are reinventing the wheel with this Dockerfile? and also violating the rule of thumb to have single process per container?
In the case of MySQL, it not that simple to have Dockerfile and work with build time. You need to create a user at runtime using entrypoint once the MySQL service is up.
Will suggest using offical WordPress image and MySQL as simple like this.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Now come to your question, the reason is no user exist in your Docker container.
RUN (/usr/bin/mysqld_safe &); sleep 5; mysqladmin -u root -proot create wordpress
Each run directive executes in its own shell. so the above command will not do the job as you are expecting. you need to move these steps to entrypoint as MySQL offical dockerfile does.
Another suggestion, use MySQL as base image and expend as per your need, but again you need some proper entrypoint to work with MySQL. its not like WordPress or node container.

Related

Docker for WordPress slow

Problem:
I have a problem with WordPress & Docker because the slow loading time (+- 7 seconds) of my website. I am not sure why this happends but I think it has something to do with the external database or the shared volumes.
Setup:
I have a custom Dockerfile built on WordPress with XDebug and Mailhog. This Dockerfile is included within my docker-compose.yml, the other services my docker-compose contains are WP-CLI and Mailhog. My Database is hosted on an Amazon RDS so I can share it with my colleagues.
Code:
My Dockerfile looks like this:
FROM wordpress:latest
# Plugins & Media
RUN mkdir -p /var/www/html/wp-content/plugins
RUN mkdir -p /var/www/html/wp-content/uploads
RUN chown -R www-data:www-data /var/www
RUN find /var/www/ -type d -exec chmod 0755 {} \;
RUN find /var/www/ -type f -exec chmod 644 {} \;
# Mailhog
RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 && \
chmod +x /usr/local/bin/mhsendmail
RUN echo 'sendmail_path="/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025 --from=noreply#examle.com"' > /usr/local/etc/php/conf.d/mailhog.ini
# Xdebug
ENV XDEBUG_PORT 9000
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.profiler_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.profiler_output_name=cachegrind.out.%t" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.profiler_output_dir=/tmp" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "max_input_vars=2000" >> /usr/local/etc/php/conf.d/custom.ini \
&& rm -rf /usr/local/etc/php/conf.d/opcache-recommended.ini
EXPOSE 9000
My Docker-compose.yml looks like this:
version: "3.7"
services:
wordpress:
container_name: "${PROJECT_NAME}_wordpress"
restart: always
build:
context: ./
dockerfile: ./Dockerfile
ports:
- "8888:80"
- "443:443"
environment:
WORDPRESS_DB_NAME: "${PROJECT_NAME}"
WORDPRESS_DB_HOST: "${MYSQL_HOST}"
WORDPRESS_DB_USER: "${MYSQL_USER}"
WORDPRESS_DB_PASSWORD: "${MYSQL_PASSWORD}"
WORDPRESS_DEBUG: 1
XDEBUG_CONFIG: remote_host=host.docker.internal
WORDPRESS_CONFIG_EXTRA: |
define('FS_METHOD', 'direct');
volumes:
- "wordpress:/var/www/html"
- "./build/uploads:/var/www/html/wp-content/uploads:cached"
- "./build/plugins:/var/www/html/wp-content/plugins:cached"
- "./build/themes:/var/www/html/wp-content/themes:cached"
cli:
container_name: "${PROJECT_NAME}_cli"
image: "wordpress:cli"
volumes:
- "wordpress:/var/www/html"
- "./build/plugins:/var/www/html/wp-content/plugins:cached"
depends_on:
- wordpress
mailhog:
container_name: "${PROJECT_NAME}_mailhog"
image: mailhog/mailhog
depends_on:
- wordpress
ports:
- "1025:1025"
- "8025:8025"
volumes:
wordpress: null
But I can't find out why it is so slow; I got version 2.1.09.3 of Docker Desktop and working on a fast Mac or Windows.
Can someone help me or point me in the right direction?
Edits
If I look in the docker stats my CPU is around 0.01% and my MEM is around 2.73% so that can't be the problem.
Found out the biggest problem is connecting to the external database. If I move over to a local database the loading time is a lot faster (+- 1 sec).
There some issues for Mac & Windows Volume performance.
Link for more details:
Docker Wordpress super slow
In Mac and Windows there are some volumes performance issues that we should consider.
I made change in my docker-compose.yml
Note as I changed the short syntax to long syntax.
This notation permits add consistency option.
I added wp-content and php-conf (to get php.ini) because they are files directory most frequently called every time when a Wordpress page is loaded in browser.
services:
wordpress:
...
volumes:
- ./data:/data
- ./scripts:/docker-entrypoint-initwp.d
#- ./wp-content:/app/wp-content
- type: bind
source: ./wp-content
target: /app/wp-content
consistency: cached
#- ./php-conf:/usr/local/etc/php
- type: bind
source: ./php-conf
target: /usr/local/etc/php
consistency: cached

Copy file from host to Docker container by using Docker file error

I am new to Docker. I know there are lot of answers out there. I tried this link host to container But I can't solve my issue with that. I am creating a docker for WordPress with WordPress cli image.
Here it is:
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_files:/var/www/html
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpressdb
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: wordpressdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
wordpress_files:
db_data:
In above code, I am using a official WordPress image which is connected with MySQL and it was created successfully. Next I want to install WordPress cli in that WordPress image. Here are the commands I found to install WordPress cli.
echo "Installing WP-CLI"
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
I tried in the direct way by putting the above commands in the command section in Docker file. But it failed. So I saved the contents in the install.sh file in the host.
Next I want to transfer the file to WordPress image and the file should be triggered after installing the WordPress image and it should install cli in that image by using the file.
Here the code I modified:
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_files:/var/www/html
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpressdb
COPY /files/install.sh /var/www/html/ =>modified
command: =>modified
/var/www/html/files/install.sh =>modified
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: wordpressdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
wordpress_files:
db_data:
Error:
ERROR: yaml.scanner.ScannerError: while scanning a simple key
in "./word_press_docker_file.yml", line 18, column 6
could not find expected ':'
in "./word_press_docker_file.yml", line 19, column 6
But it again fails. I tried several Stack Overflow answers but unable to figure it out. I tried the COPY command but it fails.
One way of achieving this is via Dockerfile.
A sample Dockerfile for your case may look like this:
FROM wordpress:latest
RUN cd /tmp && echo "Installing WP-CLI" && curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp
Then you can build this as a new Custom Image and use in your YAML File.
Edit:
Use of Local images is no longer permitted. So, you can use the docker tag command to tag the image and then use in YAML file.
You can read more about this here:
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
One other way of doing this is mentioned here:
https://hub.docker.com/_/wordpress/
This image variant does not contain WordPress itself, but instead contains WP-CLI.
The simplest way to use it with an existing WordPress container would be something similar to the following:
$ docker run -it --rm \
--volumes-from some-wordpress \
--network container:some-wordpress \
wordpress:cli user list
Generally speaking, for WP-CLI to interact with a WordPress install, it needs access to the on-disk files of the WordPress install, and access to the database (and the easiest way to accomplish that such that wp-config.php does not require changes is to simply join the networking context of the existing and presumably working WordPress container, but there are many other ways to accomplish that which will be left as an exercise for the reader).
You can try using the RUN instruction as follows:
RUN echo "Installing WP-CLI" \
&& curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x wp-cli.phar \
&& mv wp-cli.phar /usr/local/bin/wp

How to update wordpress on docker

I'm running a php-fpm wordpress container.
The wordpress source files are mounted in a named volume "wordpress" shared with the Nginx container.
Everything is running well except when i need to update wordpress to a new version. The code inside the named volume persists. It is normal for a named volume...
I could manually delete the volume but there must be a better way.
My dockerfile:
FROM wordpress:4.9.5-php5.6-fpm-alpine
My docker-compose.yml
version: '3.1'
services:
php:
build: ./docker/php/
restart: unless-stopped
volumes:
- wordpress:/var/www/html
- ./web/wp-content/:/var/www/html/wp-content/
- ./web/wp-config.php:/var/www/html/wp-config.php
environment:
- DEBUG=${DEBUG:-0}
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
- MYSQL_DATABASE=$MYSQL_DATABASE
nginx:
image: nginx:1-alpine
restart: unless-stopped
expose:
- 80
volumes:
- wordpress:/var/www/html
- ./web/wp-content/:/var/www/html/wp-content/
- ./docker/nginx/site.conf:/etc/nginx/conf.d/default.conf
- ./docker/nginx/wordpress.conf:/etc/nginx/wordpress.conf
environment:
- VIRTUAL_HOST=localhost
mysql:
image: mysql:5.6
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
- MYSQL_DATABASE=$MYSQL_DATABASE
volumes:
- mysql:/var/lib/mysql
volumes:
wordpress: {}
mysql: {}
networks:
default:
external:
name: wordpress
Looking forward to reading your suggestions
Thank you
When the wordpress container comes up it checks for the existence of files at /var/www/html and copies only if not present. So in your case may you can update the entrypoint script to check the wordpress version in the wp-includes/version.php in the /var/www/html and the files in the container and then make a decision to replace the new files.
Edit:
According to this just deletion of index.php or wp-includes/version.php should copy the files from container again. Or may you can update your entrypoint script to copy files to /var/www/html all the time, but that may cause issues if you choose to scale the wordpress layer.
Thank you for your help.
It worked.
Here is the code i'm using.
I overriden the entrypoint in dockerfile
COPY check-wordpress-version.sh /usr/local/bin/
ENTRYPOINT ["check-wordpress-version.sh"]
Here is the content of check-wordpress-version.sh to check wordpress current version.
VOLUME_VERSION="$(php -r 'require('"'"'/var/www/html/wp-includes/version.php'"'"'); echo $wp_version;')"
echo "Volume version : "$VOLUME_VERSION
echo "WordPress version : "$WORDPRESS_VERSION
if [ $VOLUME_VERSION != $WORDPRESS_VERSION ]; then
echo "Forcing WordPress code update..."
rm -f /var/www/html/index.php
fi
docker-entrypoint.sh php-fpm
Wordpress seems to have addressed this under this issue.
I notice you are using a custom wp-config.php. Most likely, you can use the WORDPRESS_CONFIG_EXTRA for this rather than mounting wp-config.php.
Theoretically (per the link above), updating the image should update the database, but I have not confirmed.
Based on this, my stack.yml/docker-compose.yml looks like this:
environment:
WORDPRESS_CONFIG_EXTRA: |
define( 'AUTOMATIC_UPDATER_DISABLED', true );
volumes:
- "./themes:/var/www/html/wp-content/themes/"
- "./plugins:/var/www/html/wp-content/plugins/"
- "./uploads:/var/www/html/wp-content/uploads/"
It's easier solution.
You have to edit wp-config.php file by add define('FS_METHOD','direct'); to the end of file.
Save the file and run update. From now, you don't need FTP server to update your WordPress.
Remember! Before update make a backup :)
To expend on #Bigbenny's answer, my Dockerfile looked like the following:
FROM wordpress:latest
WORKDIR /var/www/html
COPY . /var/www/html
COPY check-wordpress-version.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/check-wordpress-version.sh
ENTRYPOINT ["/usr/local/bin/check-wordpress-version.sh"]
Two things to notice here:
I had to chmod 755 the file or I would get a permissions denied error
I placed the script inside the /usr/local/bin because for some reason when I would just use ENTRYPOINT["check-wordpress-version.sh"], the file wouldn't be found by the container.
I also, slightly tweaked the script to look like:
#!/bin/sh
VOLUME_VERSION="$(php -r 'require('"'"'/var/www/html/wp-includes/version.php'"'"'); echo $wp_version;')"
echo "Volume version : "$VOLUME_VERSION
echo "WordPress version : "$WORDPRESS_VERSION
if [ $VOLUME_VERSION != $WORDPRESS_VERSION ]; then
echo "Forcing WordPress code update..."
rm -f /var/www/html/index.php
rm -f /var/www/html/wp-includes/version.php
fi
docker-entrypoint.sh apache2-foreground
For my use-case, I had to use apache2-foreground rather than php-fpm; I also deleted the /var/www/html/wp-includes/version.php file.
Finally, in my docker-compose, instead of the using the image directive; I used build: ./wordpress.
I hope this helps!😁
My answer applied to the official docker wordpress image. So
probably off topic but might help someone.
If you are using docker-compose you can pull the latest image using this command.
docker pull wordpress
I believe this will update your core docker image. Any other local project which you docker-compose up -d with this yml image setting as this will use the latest update.
services:
wordpress:
image: wordpress:latest
If you currently running the image will you need to docker-compose down and docker-compose up -d to invoke the update.

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

Dockerize wordpress

Trying to dockerise wordpress I figure out this scenenario:
2 data volume containers, one for the database (bbdd) and another for wordpress files (wordpress):
sudo docker create -v /var/lib/mysql --name bbdd ubuntu:trusty /bin/true
sudo docker create -v /var/www/html --name wordpress ubuntu:trusty /bin/true
Then I need a container for mysql so I use the official mysql image from docker hub and also the volume /var/lib/mysql from the first data container:
docker run --volumes-from bbdd --name mysql -e MYSQL_ROOT_PASSWORD="xxxx" -d mysql:5.6
Then I need a container for apache/php so I use official wordpress image from docker hub and also the volume /var/lib/mysql from the first data container:
docker run --volumes-from wordpress --name apache --link mysql:mysql -d -p 8080:80 wordpress:4.1.2-apache
What I understand from docker docs is that if I don't remove the data containers, I'll have persistance.
However if I stop and delete running containers (apache and mysql) and recreate them again with last commands, data get lost:
docker run --volumes-from bbdd --name mysql -e MYSQL_ROOT_PASSWORD="xxxx" -d mysql:5.6
docker run --volumes-from wordpress --name apache --link mysql:mysql -d -p 8080:80 wordpress:4.1.2-apache
However if I create the containers without data containers, it seems to work as I expected:
docker run -v /home/juanda/project/mysql:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD="juanda" -d mysql:5.6
docker run -v /home/juanda/project/wordpress:/var/www/html --name apache --link mysql:mysql -d -p 8080:80 wordpress:4.1.2-apache
You need to run the data container for once to make it persistent:
sudo docker run -v /var/lib/mysql --name bbdd ubuntu:trusty /bin/true
sudo docker run -v /var/www/html --name wordpress ubuntu:trusty /bin/true
This is an old bug of Docker described here. You may be affected if your Docker version is old.
In a very simplified test case this appears to work as advertised and documented in Creating and mounting a Data Volume Container:
prologic#daisy
Thu Apr 30 08:18:45
~
$ docker create -v /test --name data busybox /vin/true
Unable to find image 'busybox:latest' locally
latest: Pulling from busybox
cf2616975b4a: Pull complete
6ce2e90b0bc7: Pull complete
8c2e06607696: Already exists
busybox:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b52004ee8502d
Status: Downloaded newer image for busybox:latest
6f5fc1d2e33654867cff8ffdb60c5765ced4b7128441ae2c6be24b68fb6454ef
prologic#daisy
Thu Apr 30 08:20:53
~
$ docker run -i -t --rm --volumes-from data crux /bin/bash
bash-4.3# cd /test
bash-4.3# ls
bash-4.3# touch foo
bash-4.3# echo "Hello World" >> foo
bash-4.3# cat foo
Hello World
bash-4.3# exit
prologic#daisy
Thu Apr 30 08:21:20
~
$ docker run -i -t --rm --volumes-from data crux /bin/bash
bash-4.3# cd /test
bash-4.3# ls
foo
bash-4.3# cat foo
Hello World
bash-4.3# exit
Note that I deleted the attached container to make sure the persistent data volume container's data was left in tact.
The data volume container and it's data would only disappear if you ran the following:
docker rm -v data
Note: the -v option to actually remove volumes.
See (specifically the -v/--volumes option):
$ docker rm -h
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
-f, --force=false Force the removal of a running container
(uses SIGKILL) --help=false Print usage -l, --link=false
Remove the specified link -v, --volumes=false Remove the volumes
associated with the container
For reference I am running:
prologic#daisy
Thu Apr 30 08:24:51
~
$ docker version
Client version: 1.6.0
Client API version: 1.18
Go version (client): go1.3.3
Git commit (client): 47496519da
OS/Arch (client): linux/amd64
Server version: 1.6.0
Server API version: 1.18
Go version (server): go1.3.3
Git commit (server): 47496519da
OS/Arch (server): linux/amd64
Update: For a quick example (which you can use in production) of a Dockerized Wordpress setup with full hosting support see: https://gist.github.com/prologic/b5525a50bb4d867d84a2
You can simply use docker-compose file as:
version: '3.3'
services:
# https://hub.docker.com/_/nginx
# Doesn't play well with wordpress fpm based images
# nginx:
# image: nginx:latest
# container_name: "${PROJECT_NAME}_nginx"
# ports:
# - "${NGINX_HTTP_PORT}:80"
# working_dir: /var/www/html
# volumes:
# - ./docker/etc/nginx:/etc/nginx/conf.d
# - ./logs/nginx:/var/log/nginx
# - ./app:/var/www/html
# environment:
# - NGINX_HOST=${NGINX_HOST}
# #command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/wordpress.conf > /etc/nginx/conf.d/wordpress.conf && nginx -g 'daemon off;'"
# links:
# - wordpress
# restart: always
# https://hub.docker.com/r/jwilder/nginx-proxy
nginx-proxy:
image: jwilder/nginx-proxy
container_name: "${PROJECT_NAME}_nginx-proxy"
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
# https://hub.docker.com/_/mysql
mysql:
image: mysql:${MYSQL_TAG}
# For MySQL 8.0
#image: mysql:8
#command: '--default-authentication-plugin=mysql_native_password'
container_name: "${PROJECT_NAME}_mysql"
ports:
- "${MYSQL_PORT}:3306"
volumes:
- ./data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
restart: always
# https://hub.docker.com/_/wordpress
wordpress:
# -fpm or apache, unfortunately fpm doesn't work properly with nginx-proxy
image: wordpress:${WP_VERSION}-php${PHP_VERSION}-apache
container_name: "${PROJECT_NAME}_wordpress"
environment:
- VIRTUAL_HOST=${WP_HTTP_HOST}
- WORDPRESS_DB_HOST=mysql:3306
- WORDPRESS_DB_NAME=${MYSQL_DATABASE}
- WORDPRESS_DB_USER=${MYSQL_USER}
- WORDPRESS_DB_PASSWORD=${MYSQL_ROOT_PASSWORD}
working_dir: /var/www/html
volumes:
- ./app:/var/www/html
#- ./app/wp-content:/var/www/html/wp-content
- ./docker/etc/php-fpm/custom.ini:/usr/local/etc/php/conf.d/999-custom.ini
#depends_on:
# - mysql
ports:
- "${WP_HTTP_PORT}:80"
expose:
- ${WP_HTTP_PORT}
links:
- mysql
restart: always
# https://hub.docker.com/r/phpmyadmin/phpmyadmin
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: "${PROJECT_NAME}_phpmyadmin"
ports:
- "${PMA_PORT}:80"
expose:
- ${PMA_PORT}
environment:
VIRTUAL_HOST: ${PMA_HTTP_HOST}
PMA_HOST: mysql
depends_on:
- mysql
# #todo services
# jwilder/nginx-proxy
# https / letsencrypt
# composer
# mailhog
# redis
# phpredisadmin
# blackfire
networks:
default:
external:
name: nginx-proxy
SOURCE: https://github.com/MagePsycho/wordpress-dockerized

Resources