Initialize WordPress site in Docker with WP-CLI and an install script
version: '3.4'
services:
db:
image: mariadb
volumes:
- db_volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-whocares}
MYSQL_USER: ${WORDPRESS_DB_USER:-wordpress}
MYSQL_PASSWORD: ${WORDPRESS_DB_PASSWORD:-wordpress}
MYSQL_DATABASE: ${MYSQL_DATABASE:-wordpress}
cli:
image: bozdoz/wordpress-initialize
env_file: .env
depends_on:
- db
volumes:
- wp_volume:/var/www/html
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: ${WORDPRESS_DB_USER:-wordpress}
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD:-wordpress}
WORDPRESS_DATABASE: ${MYSQL_DATABASE:-wordpress}
wordpress:
image: wordpress
depends_on:
- cli
volumes:
- wp_volume:/var/www/html
ports:
- ${WP_PORT:-1234}:80
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: ${WORDPRESS_DB_USER:-wordpress}
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD:-wordpress}
WORDPRESS_DATABASE: ${MYSQL_DATABASE:-wordpress}
volumes:
db_volume:
wp_volume:
WP_PORT=1234
WP_URL=localhost:1234
WP_USER=admin
WP_PASSWORD=password
WP_EMAIL=not@real.com
WP_TITLE=WordPress
WP_DESCRIPTION=Just Another WordPress Blog
WP_DEBUG=true
WP_THEME=twentynineteen
WP_PLUGINS="leaflet-map quote-tweet"
This will auto-install WordPress, so you can docker-compose down -v
and docker-compose up
without having to re-install WordPress each time.
You can also use this image to run WP CLI commands, like so: docker-compose run cli wp plugin list
or docker-compose run cli wp post create --post_type=post --post_title='New Post' --post_content='Another post' --post_status=publish
You can execute any script within the cli container with an initialize.sh file:
wp post create \
--post_type=post \
--post_title='Leaflet Map' \
--post_content='[leaflet-map] [leaflet-marker]' \
--post_status='publish'
And add it to the container in a volume:
cli:
image: bozdoz/wordpress-initialize
volumes:
- wp_volume:/var/www/html
+ - ./initialize.sh:/app/initialize.sh