Use `WP_CLI::add_command` with WordPress in Docker - php

I'm using WordPress in Docker with the following configuration:
# /wp-dock/docker-compose.yml
---
version: "3.3"
services:
db:
container_name: ${APP_NAME}-wordpress-db
image: "mysql:5.7"
platform: linux/amd64
volumes:
- "./data/mysql:/var/lib/mysql"
restart: always
ports:
- 18766:3306
environment:
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8080:80"
environment:
- PMA_HOST=db
wordpress:
container_name: ${APP_NAME}-wordpress
depends_on:
- db
image: "wordpress:latest"
ports:
- "81:80"
environment:
WORDPRESS_DB_HOST: "db:3306"
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
volumes:
- "./wordpress:/var/www/html"
- "./plugins:/var/www/html/wp-content/plugins"
- "./themes:/var/www/html/wp-content/themes"
wpcli:
container_name: ${APP_NAME}-cli
depends_on:
- wordpress
image: wordpress:cli-php8.1
user: 1000:1000
command: tail -f /dev/null
volumes:
- "./wordpress:/var/www/html"
environment:
WORDPRESS_DB_HOST: "db:3306"
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
I can access and use wp-cli like so:
docker exec site-cli wp --info
This works well. But I'm writing a plugin in the WordPress container that extends the wp-cli with a custom command as seen here.
When I try run that command, wp-cli says that it's not a registered command.
Any ideas on how to extend wp-cli in a Docker container?

Related

Set a path for the Wordpress files for Wordpress docker image

I'm using the following docker-compose file to run multiple wordpress files on different domains using caddy.
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:6.1-php8.1-fpm-alpine
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- ./caddy/sites/testwp/wordpress:/var/www/html/testwp/wordpress
wordpress2:
depends_on:
- db
image: wordpress:6.1-php8.1-fpm-alpine
restart: always
environment:
WORDPRESS_DB_HOST: db2
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- ./caddy/sites/testwp2/wordpress:/var/www/html/testwp2/wordpress
caddy:
image: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./caddy/Caddyfile:/etc/caddy/Caddyfile
- ./caddy/sites:/var/www/html
- ./caddy/caddy_data:/data
db:
image: mariadb
container_name: database
restart: always
environment:
MARIADB_DATABASE: exampledb
MARIADB_USER: exampleuser
MARIADB_PASSWORD: examplepass
MARIADB_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
db2:
image: mariadb
container_name: database2
restart: always
environment:
MARIADB_DATABASE: exampledb
MARIADB_USER: exampleuser
MARIADB_PASSWORD: examplepass
MARIADB_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db2:/var/lib/mysql
volumes:
db:
db2:
With this caddy file:
testwp.domain.nl {
root * /var/www/html/testwp/wordpress
php_fastcgi wordpress:9000
file_server
}
testwp2.domain.nl {
root * /var/www/html/testwp2/wordpress
php_fastcgi wordpress2:9000
file_server
}
This works, I get 2 wordpress installs, but I need to manually add the wordpress install files (wget https://wordpress.org/latest.tar.gz) to ./caddy/sites/testwp/wordpress and ./caddy/sites/testwp2/wordpress. This is because the wordpress docker image puts the files in (inside the container) in /var/www/html and if I don't put the files in /var/www/html/testwp/wordpress inside the container, I get a 404 on index.php. This is probably because php-fpm needs to access index.php on the exact same path as caddy? Not entirely sure...
Is there a way to ask the wordpress docker container to put the files in another folder than /var/www/html?
Ok, this combination works, one can set the root for php_fastcgi in the caddy file like this:
testwp.domain.nl {
root * /var/www/html/testwp/wordpress
php_fastcgi wordpress:9000 {
root /var/www/html
}
file_server
}
testwp2.domain.nl {
root * /var/www/html/testwp2/wordpress
php_fastcgi wordpress2:9000 {
root /var/www/html
}
file_server
}
And then this works, no more double Wordpress files!
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:6.1-php8.1-fpm-alpine
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- ./caddy/sites/testwp/wordpress:/var/www/html
wordpress2:
depends_on:
- db
image: wordpress:6.1-php8.1-fpm-alpine
restart: always
environment:
WORDPRESS_DB_HOST: db2
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- ./caddy/sites/testwp2/wordpress:/var/www/html
caddy:
image: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./caddy/Caddyfile:/etc/caddy/Caddyfile
- ./caddy/sites:/var/www/html
- ./caddy/caddy_data:/data
db:
image: mariadb
container_name: database
restart: always
environment:
MARIADB_DATABASE: exampledb
MARIADB_USER: exampleuser
MARIADB_PASSWORD: examplepass
MARIADB_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
db2:
image: mariadb
container_name: database2
restart: always
environment:
MARIADB_DATABASE: exampledb
MARIADB_USER: exampleuser
MARIADB_PASSWORD: examplepass
MARIADB_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db2:/var/lib/mysql
volumes:
db:
db2:
Hooray!

What is wrong with my Laravel docker compose File?

Can anybody help me with the following? I have a docker-compose file with the following services PHP, Mysql, Redis, and Nginx. The docker-compose file builds without any issues but when I try the URL its not working. The file is down below
version: '3.8'
services:
php:
build: ./docker-compose/php-81/build
image: php-81
container_name: cumax-php-db
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
- ./docker-files/storage/logs/php:/var/www/default/logs
networks:
- onboarding
db:
image: mysql:8.0
container_name: cumax-mysql-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql8.0/storage:/var/lib/mysql
networks:
- onboarding
nginx:
image: nginx:1.17-alpine
container_name: cumax-nginx
restart: unless-stopped
ports:
- "8070:80"
volumes:
- ./:/var/www
- ./docker-compose/nginx/conf.d:/etc/nginx/conf.d
networks:
- onboarding
redis:
image: "redis:alpine"
ports:
- "6379:6379"
networks:
- onboarding
networks:
onboarding:
On php you don't publish the port.
On the Nginx you only publish to port 8070 on the host.
What is the URL you are using? It should be http://< host >:8070/
And then the nginx should proxy onwards to php.

Why do extra folders get created in laravel public folder when i use docker as a dev environment and how do i stop it

In the image above the extra folders in the public folder only appears when ever i set up a dev environment with docker, the logs go to these folders instead of the /storage/logs/.
Another thing i experience when i use docker dev environment is i always have to reload cache (artisan optimize) before my config or route changes can be applied in the container.
THIS IS MY Docker-Compose FILE
version: "3"
networks:
laravel:
services:
nginx:
container_name: testproj-nginx
image: nginx:stable-alpine
ports:
- "2022:80"
volumes:
- ./:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8
container_name: testproj-mysql
restart: unless-stopped
tty: true
ports:
- "2306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: testproj
MYSQL_USER: root_user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build: ./
container_name: testproj-php
volumes:
- ./:/var/www/html
networks:
- laravel
mailhog:
image: "mailhog/mailhog:latest"
container_name: testproj-mailhog
ports:
- "${FORWARD_MAILHOG_PORT:-2025}:1025"
- "${FORWARD_MAILHOG_DASHBOARD_PORT:-9025}:8025"
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: testproj-pma
links:
- mysql
environment:
PMA_HOST: testproj-mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8083:80
networks:
- laravel
depends_on:
- php
- mysql
# ngrok:
# image: wernight/ngrok:latest
# container_name: testproj-ngrok
# ports:
# - 4041:4040
# environment:
# NGROK_AUTH: ${NGROK_AUTH_KEY}
# NGROK_REGION: eu
# NGROK_PROTOCOL: http
# NGROK_PORT: testproj-nginx:80
# networks:
# - laravel
# depends_on:
# - nginx

How to mirror production server using docker-compose for Wordpress

I have the following docker-compose file for running a Wordpress website.
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
- ./mysql-dump:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: admin123
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8081:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: admin123
networks:
- wpsite
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8001:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
networks:
- wpsite
volumes:
- "../../blogs:/var/www/html"
- "../../blogs/plugins:/var/www/html/wp-content/plugins"
networks:
wpsite:
volumes:
db_data: {}
Here I am using the official wordpress:latest image, but my production server has a different configuration. I would like to use the same PHP version and other configurations. How can I do it ?

How to load mysqli extension inside docker-compose.yml file with NGINX, PHP-FPM

When loading docker-compose up, wordpress loads on the url but the mysqli_connect function is undefined because of the absence of the extension.
I have tried to add the following under the fpm image
command: "docker-php-ext-install mysqli"
I have tried to add a Dockerfile into the directory of the docker-compose.yml file containing
version: "3"
services:
#database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# webserver
nginx:
image: nginx:latest
ports:
- "8080:80"
links:
- fpm
volumes:
- /Users/connergesbocker/Github/cbgesbocker/dotfiles/root/etc/nginx/conf.d/village.conf:/etc/nginx/conf.d/village.conf
- /Users/connergesbocker/WordPress:/WordPress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8888:8888"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: wordpress
networks:
- wpsite
fpm:
image: php:5.6.20-fpm
ports:
- "90:9000"
# command: "docker-php-ext-install mysqli"
links:
- db
volumes:
- /Users/connergesbocker/WordPress:/WordPress
working_dir: "/"
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:```
You could customize your dockerfile & add install in it:
Dockerfile:
FROM php:5.6.20-fpm
RUN docker-php-ext-install mysqli
Part of docker-compose.yaml:
fpm:
build: .
image: myphp:1
ports:
- "90:9000"
# command: "docker-php-ext-install mysqli"
links:
- db
volumes:
- /Users/connergesbocker/WordPress:/WordPress
working_dir: "/"
networks:
- wpsite
your override will install the extension but the container will stop after installation because php-fpm will not start by overriding CMD. Replace the command with below one.
command:
- /bin/sh
- -c
- |
docker-php-ext-install mysqli
echo "extension installed.....starting php-fpm........................................"
php-fpm

Categories