How to run wp cli in docker-compose.yml - wordpress

Just starting in docker here
So I got this in my docker-compose.yml
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_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
command: wp core install --path="/var/www/html" --url=localhost --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo#bar.com
volumes:
db_data:
So I wanted to run the wp core install so that I won't have to go through the process of manually setting up my test wordpress site.
However when I run docker-compose up, this does not seem to work, I got this error on the console
What am I missing here? Anyone can help me accomplish my goal of automating the of setting up wordpress install?
Thanks in advance

Well there are a couple of problems. The first one is that those two containers (wordpress and wordpress-cli) don't share a volume. So while wordpress has a wordpress installation ready, the wordpress-cli doesn't.
So you can add volumes to both containers, and then wordpress-cli will find the wordpress installation.
Then there's a second problem: the wordpress:latest and wordpress:cli images both run with the user www-data, but the problem is that the individual www-data users have different user-id's:
$ docker run --rm wordpress:latest grep www-data /etc/passwd
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
$ docker run --rm wordpress:cli grep www-data /etc/passwd
www-data:x:82:82:Linux User,,,:/home/www-data:/bin/false
It seems they aren't exactly compatible here. So if you use a shared volume you have to make sure they both use the same user-id. I solved this by having the wordpress:cli run with the user xfs which also has the user id 33.
The last problem is that your containers have dependencies on each other. Wordpress needs a running MySQL instance and the wordpress-cli needs also the MySQL and the Wordpress to be ready. To make sure MySQL is ready for the wordpress cli installation you either use something like "wait-for-it" or in a simple case you can just wait a couple of seconds and then try it.
I have tested all those changes and came up with the following docker-compose.yml. I have annotated all the changes I've made with "vstm":
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_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
# vstm: This is required to run wordpress-cli with the same
# user-id as wordpress. This way there are no permission problems
# when running the cli
user: xfs
# vstm: The sleep 10 is required so that the command is run after
# mysql is initialized. Depending on your machine this might take
# longer or it can go faster.
command: >
/bin/sh -c '
sleep 10;
wp core install --path="/var/www/html" --url="http://localhost:8000" --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo#bar.com
'
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
volumes:
db_data:
# vstm: add shared volume
wp_data:
It uses a docker-volume but you can also map it to a filesystem. Depends on how you plan to use your docker-compose.

This one worked for me:
wpcli:
depends_on:
- mysql
- wordpress
image: wordpress:cli
links:
- mysql:db
entrypoint: wp
command: "--info"
container_name: ${COMPOSE_PROJECT_NAME}_wpcli
volumes:
- ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
working_dir: /var/www/html
Note that in the line:
links:
- mysql:db
mysql = name of my service
db = alias name I gave it, can be anything
Then you issue run wp like so:
docker-compose run --rm wpcli WORDPRESS_COMMAND
Source: https://medium.com/#tatemz/using-wp-cli-with-docker-21b0ab9fab79

this's my first answer at Stack Overflow :">
Actually, your question inspired me, and #vstm's answer guided me a bit.
You could try my piece of code:
1.wait-for-mysql.sh
#!/bin/bash -e
HOST=$(echo $WORDPRESS_DB_HOST | cut -d: -f1)
PORT=$(echo $WORDPRESS_DB_HOST | cut -d: -f2)
CMD=$#
until mysql -h $HOST -P $PORT -D $WORDPRESS_DB_NAME -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -e '\q'; do
>&2 echo "Mysql is unavailable - sleeping..."
sleep 2
done
>&2 echo "Mysql is up - executing command"
exec $CMD
2.compose.yml
version: '3.9'
services:
wordpress:
image: wordpress:5.7.0-php7.4-apache
ports:
- "80:80"
volumes:
- ./wp-data/:/var/www/html/
networks:
wp-net: {}
wp-cli:
image: wordpress:cli-2.4.0-php7.4
depends_on:
- wordpress
volumes:
- ./wait-for-mysql.sh:/wait-for-mysql.sh
- ./wp-data:/var/www/html/ # shared with wordpress service
user: "33"
command: >
/wait-for-mysql.sh
wp core install
--path="/var/www/html"
--url="http://your-url-here"
--title=your-title-here
--admin_user=your-user-here
--admin_password=your-password-here
--admin_email=your-email-here}
--skip-email
networks:
wp-net: {}
networks:
wp-net: {}
For your reference:
https://docs.docker.com/compose/startup-order/
https://gitlab.com/hino-hatake/wordpress

Related

Copying plugins to base wordpress image, Plugins folders are visible in cmd but not visible in volume mount

Copy plugins to wordpress base image. Plugins folders are visible in cmd CMD using command docker run -it --rm arslanliaqat/testwordpressimage sh but not visible in volume mounted Folder. I have attached the screenshot of cmd commands and folder as well.
Dockerfile
FROM wordpress:php7.1-apache
WORKDIR /var/www/html
COPY plugins/wordpress-seo/ /var/www/html/wp-content/plugins/wordpress-seo/
COPY plugins/wp-super-cache/ /var/www/html/wp-content/plugins/wp-super-cache/
EXPOSE 80
Docker-compose.yml
version: '3.3'
services:
db:
image: 'mysql:5.7'
volumes:
- './dbdata:/var/lib/mysql'
restart: always
environment:
#PMA_HOST: db
#PMA_PORT: 3000
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: 'arslanliaqat/testwordpressimage:latest'
#image: 'wordpress:latest'
volumes:
#- './wordpress/:/var/www/html/'
- './Wordpress:/var/www/html'
#- './wordpress:/var/www/html'
#- './wordpress/wp-content/plugins/wordpress-seo/:/wp-content/wordpress-seo/'
#- './wordpress/wp-content/plugins/wp-super-cache/:/wp-content/wp-super-cache/'
# - './plugins/wordpress-seo/:/var/www/html/wp-content/plugins/wordpress-seo/'
# - './plugins/wp-super-cache/:/var/www/html/wp-content/plugins/wp-super-cache/'
- './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
ports:
- '8000:80'
restart: always
environment:
WP_LOCALE: en_US
WORDPRESS_LOCALE: en_US
WORDPRESS_DB_HOST: 'db:3306'
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
WORDPRESS_DB_NAME: wordpress
working_dir: /var/www/html
volumes:
db_data:
wordpress:
Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container.
That means: when you define - './Wordpress:/var/www/html' in compose, the directory /var/www/html in container will be override by the directory ./Wordpress on host. So you can just see the contents of your hosts.
But for this, you did not use bind mount volume, so your things in container not override by host directory.
Detail refers to official guide

Wordpress intallation on Docker. trying to skip installation page

Running wordpress on docker. Trying to skip install install.php by defining user in compose file. I have already added wordpress user , password and title to skip that page. but not working.
Is there some problem with my code? Is there another way to do it. or help me fix this code.
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
volumes:
- ./wordpress:/var/www/html
- ./docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_VERSION: 5.1
WORDPRESS_LOCALE: en_US
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: "wp_"
WORDPRESS_DEBUG: 1
WORDPRESS_DB_NAME: wordpress
# WORDPRESS_WEBSITE_TITLE: "My blog"
# WORDPRESS_WEBSITE_URL: "http://example.com"
# WORDPRESS_WEBSITE_URL_WITHOUT_HTTP: "example.com"
# WORDPRESS_WEBSITE_URL: "http://http://localhost:8000"
# WORDPRESS_WEBSITE_URL_WITHOUT_HTTP: "localhost"
# WORDPRESS_WEBSITE_POST_URL_STRUCTURE: "/%year%/%monthnum%/%day%/%postname%/"
# WORDPRESS_ADMIN_USER: "admin"
# WORDPRESS_ADMIN_PASSWORD: "admin"
# WORDPRESS_ADMIN_EMAIL: "admin#admin.com"
working_dir: /var/www/html
wordpress-cli:
depends_on:
- db
- wordpress
image: wordpress:cli
entrypoint: wp
user: xfs
command: >
/bin/sh -c ' sleep 10;
wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '
volumes:
- ./wordpress:/var/www/html
- ./docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
volumes:
db_data: {}
wordpress:
Got this error:
C:\DockerProjects\test6>docker-compose up
ERROR: yaml.scanner.ScannerError: while scanning a simple key
in ".\docker-compose.yml", line 54, column 6
could not find expected ':'
in ".\docker-compose.yml", line 55, column 6
Problem is in this part of code
user: xfs
command: >
/bin/sh -c ' sleep 10;
wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '
It has to be like this
user: xfs
command: >
/bin/sh -c ' sleep 10;
wp core install --url="http://localhost:8000" --title="Sample Title" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '
after command: > next 2 lines you need to give 1 Tab.
You can check your code with online formatter here.
Edit 1:
Formatted Code looks like this
version: '3.3'
services:
db:
image: 'mysql:5.7'
volumes:
- './db-data:/var/lib/mysql'
restart: always
environment: null
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: 'wordpress:latest'
volumes:
- './wordpress:/var/www/html'
- './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
ports:
- '8000:80'
restart: always
environment: null
WORDPRESS_VERSION: 5.1
WORDPRESS_LOCALE: en_US
WORDPRESS_DB_HOST: 'db:3306'
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DEBUG: 1
WORDPRESS_DB_NAME: wordpress
working_dir: /var/www/html
wordpress-cli:
depends_on:
- db
- wordpress
image: 'wordpress:cli'
entrypoint: wp
user: xfs
command: "/bin/sh -c ' sleep 10; wp core install --url=\"http://localhost:8000\" --title=\"Sample Title\" --admin_name=admin --admin_password=admin --admin_email=you#domain.com '\n"
volumes:
- './wordpress:/var/www/html'
- './docker/wordpress/php/php.ini:/usr/local/etc/php/conf.d/php.ini:ro'
volumes:
db_data: {}
wordpress: null
You can install wordpress Cli either in the compose file. or you can copy the wp cli into the image using dockerfile and install wordpress using custom entrypoint.
I've found this on dockerhub:
Since March 2021, WordPress images use a customized wp-config.php that pulls the values directly from the environment variables defined above (see wp-config-docker.php in docker-library/wordpress#572 and docker-library/wordpress#577). As a result of reading environment variables directly, the cli container also needs the same set of environment variables to properly evaluate wp-config.php.
So you'd need to set the environment like in your original wordpress, like below (which worked for me, also using traefik..):
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- ./db:/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
restart: always
environment: &env
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_CONFIG_EXTRA: |
define( 'AUTOMATIC_UPDATER_DISABLED', true );
define( 'FS_METHOD', 'direct' );
volumes:
- &html
./html:/var/www/html
labels: &lab
- "traefik.enable=true"
- "traefik.port=80"
- "traefik.frontend.rule=Host:subdomain.domain.com"
networks:
- default
- web
wordpress-cli:
image: wordpress:cli
depends_on:
- db
- wordpress
environment:
<<: *env
volumes:
- *html
entrypoint: sh
command: -c 'sleep 10; wp core install --url="https://subdomain.domain.com" --title="greatname" --admin_name=admin --admin_password="mysupersecureadminpw" --admin_email=my#domain.com'
networks:
web:
external: true
You can also leave out the entrypoint and command part and just run docker-compose run wordpress-cli core install ... after you checked that everything is running. If you remove the entrypoint part, you can also run any other wp-cli command instead of core install that way :-)

Docker - WordPress - Localhost ERR_EMPTY_RESPONSE

I started working with Docker for WordPress. I followed the docker documentation to get it up and running:
https://docs.docker.com/compose/wordpress/
I added volumes for the plugin & theme directory.
When ran the command docker-compose up -d the first time and went to http://localhost:8000/ i saw the installation of WordPress. When i rebooted my PC and started the services again with: docker-compose up -d or docker-compose start i got the error message: ERR_EMPTY_RESPONSE.
I tried:
Removing the containers, services and volumes
Killing the netstat port
Currently I have no idea why it isn't working anymore. I am working on macOS
This is my current docker-compose.yml
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
volumes:
- ./plugins/my-plugin:/var/www/html/wp-content/plugins/my-plugin
- ./themes/my-theme:/var/www/html/wp-content/themes/my-theme
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpres
volumes:
db_data: {}
The status of containers after running it:
Follow these steps
docker-compose down - twice
Edit the yml file and replace all instances of db_data with db_datax
Run docker-compose up -d
Alternately,
docker-compose down - twice {removes the network as well}
docker system prune --volumes
docker-compose up -d

Docker Compose WordPress Volumes Appear Empty

I'm trying to set up a simple WordPress build using docker compose. However, when I build it, the volumes appear to be empty.
Here is my docker-compose.yml:
version: '3'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8000:80
volumes:
- ./development:/var/www/html
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: password
depends_on:
- db
networks:
- wordpress-network
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 8080:80
links:
- db:db
db:
image: mariadb:latest
ports:
- 127.0.0.1:3306:3306
command: [
'--default_authentication_plugin=mysql_native_password',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci'
]
volumes:
- wp-data:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: password
networks:
- wordpress-network
networks:
wordpress-network:
driver: bridge
volumes:
wp-data:
driver: local
Here is my local project structure, with theme stylesheet:
I run docker-compose to build the image:
docker-compose up -d --build
But when I open the build in my browser, it looks like the theme is empty:
This leads me to believe the volume is empty. I'd appreciate any help or insights into this issue, thank you.
In your docker-compose file you say ./wp-data:/var/lib/mysql which is host folder mapping (not volume) but in your docker-compose you define docker named volume called wp-data and if you want to use this volume you have to use it as wp-data:/var/lib/mysql. I would also suggest to remove ${PWD} because it might cause problem in sh

Using composer with wordpress in docker

I'm try to setup a docker workspace with Alpine, PHP, Apache, MySQL and Composer.
Currently I'm trying to use the following images:
PHP, Alpine and Composer: https://hub.docker.com/r/petehouston/docker-alpine-php-composer/
Wordpress: https://hub.docker.com/_/wordpress/
I created a docker-compose.yml file to manage this dependencies for me.
docker-compose.yml
version: '2'
services:
db:
image: mysql:5.7
volumes:
- ./db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./www:/var/www/html
links:
- db
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
alpine:
image: petehouston/docker-alpine-php-composer:latest
links:
- wordpress
So my problem is I'm trying to use the composer of the alpine container to manager my Wordpress in the wordpress container but when i try to use the following command:
docker run --rm -v $(pwd):/www -w /wordpress/var/www/html composer/composer create-project roots/sage your-theme-name 8.5.0
nothing happens, and the alpine container doesn't stay up, after i run compose-docker.up he exits
You can't access to Wordpress container from Alpine container using -w /wordpress/var/www/html.
As far as I understand, image petehouston/docker-alpine-php-composer:latest provides PHP deployment environment. That means you should use this image instead of wordpress image.
Your compose file can be like
version: '2'
services:
db:
image: mysql:5.7
volumes:
- ./db:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
alpine:
image: petehouston/docker-alpine-php-composer:latest
volumes:
- ./www:/home
links:
- db
ports:
- "8000:80"
command: composer require phpunit/phpunit
restart: always
Plz give me feedback. I'm sorry because i can't comment to get more information from you. Should you still get errors, comment here. I'll recheck

Resources