Created
October 5, 2023 00:41
-
-
Save firxworx/cd114d55c9b5135f19bab51ba212c92b to your computer and use it in GitHub Desktop.
bash script to launch phpmyadmin docker that connects to a running wp-env (@wordpress/env) database for local wp development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
# --------------------------------------------------------------------------------------------------------------------- | |
# script runs/stops phpmyadmin docker image as a container that's configured to connect to a running wp-env database | |
# author: Kevin Firko (@firxworx) | |
# date: 2023-10-24 | |
# version: 1.0.0 | |
# license: MIT (https://opensource.org/license/mit/) | |
# @see https://www.npmjs.com/package/@wordpress/env | |
# @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/ | |
# --------------------------------------------------------------------------------------------------------------------- | |
# description: | |
# - the script will launch a container named 'phpmyadmin' at: http://localhost:8080 | |
# dependencies: | |
# - wp-env must be installed and configured for your project (@wordpress/env) | |
# - docker must be installed and running | |
# - ensure the script is executable (e.g. chmod +x pma-docker.sh) | |
# assumptions: | |
# wp-env must be running | |
# wp-env must be using 'mariadb' image for its mysql-compatible database | |
# wp-env must be using 2x database containers that respectively include 'mysql' and 'tests-mysql' in their name | |
# wp-env is configured to bridge the database container to the host network port 3306 (mysql default port) | |
# your local dev workstation has no port conflicts (e.g. 3306, 8080, etc) | |
# usage: | |
# ./scripts/phpmyadmin/pma-docker.sh | |
# notes: | |
# - the docker command is run with `--rm` option to remove the container when stopped | |
# --------------------------------------------------------------------------------------------------------------------- | |
# local phpmyadmin port | |
HOST_PMA_PORT=8080 | |
HOST_PMA_CONTAINER_NAME="phpmyadmin" | |
# docker image name filter | |
IMAGE_NAME_FILTER="mariadb" | |
CONTAINER_NAME_FILTER="mysql" | |
# used to exclude 'tests-mysql' container from the results | |
EXCLUDE_GREP_FILTER_PATTERN="tests-mysql" | |
if docker ps --filter "name=$HOST_PMA_CONTAINER_NAME" --format "{{.Names}}" | grep -q "phpmyadmin"; then | |
echo "$HOST_PMA_CONTAINER_NAME container is running... stopping..." | |
docker stop "$HOST_PMA_CONTAINER_NAME" &> /dev/null | |
echo "$HOST_PMA_CONTAINER_NAME container stopped" | |
exit | |
else | |
echo "preparing to start $HOST_PMA_CONTAINER_NAME container..." | |
echo | |
echo "- checking for running wp-env containers..." | |
fi | |
# get the name of the container excluding the tests container | |
DB_CONTAINER_NAMES=($(docker ps \ | |
--filter "name=$CONTAINER_NAME_FILTER" \ | |
--filter "ancestor=$IMAGE_NAME_FILTER" \ | |
--format "{{.Names}}" | grep -v "$EXCLUDE_GREP_FILTER_PATTERN" | head -n 1 | |
)) | |
if [[ ${#DB_CONTAINER_NAMES[@]} -eq 0 ]]; then | |
echo "ERROR: no matching container found. is wp-env running?" >&2 | |
exit 1 | |
elif [[ ${#DB_CONTAINER_NAMES[@]} -gt 1 ]]; then | |
echo "ERROR: multiple matching containers found. check script filters vs. 'docker ps' output." >&2 | |
exit 1 | |
fi | |
DB_CONTAINER_NAME="${DB_CONTAINER_NAMES[0]}" | |
WP_ENV_NETWORK=$(docker inspect "$DB_CONTAINER_NAME" | jq -r '.[0].NetworkSettings.Networks | keys[]') | |
if [[ -z "$WP_ENV_NETWORK" ]]; then | |
echo "ERROR: failed to resolve the network for container $DB_CONTAINER_NAME." >&2 | |
exit 1 | |
fi | |
echo "- found wp-env database container: $DB_CONTAINER_NAME" | |
echo "- found wp-env network name: $WP_ENV_NETWORK" | |
PMA_CONTAINER_ID=$(docker run \ | |
-d \ | |
--rm \ | |
--name "$HOST_PMA_CONTAINER_NAME" \ | |
-e PMA_HOST="$DB_CONTAINER_NAME" \ | |
-e PMA_PORT=3306 \ | |
-e PMA_USER=root \ | |
-e PMA_PASSWORD=password \ | |
-p "$HOST_PMA_PORT:80" \ | |
--network "$WP_ENV_NETWORK" \ | |
phpmyadmin/phpmyadmin:latest | |
) | |
echo "- $HOST_PMA_CONTAINER_NAME container started in detached mode: $PMA_CONTAINER_ID" | |
echo | |
echo "$HOST_PMA_CONTAINER_NAME is now running at: http://localhost:$HOST_PMA_PORT" | |
echo | |
echo "stop the container by executing the following command" | |
echo "docker stop $HOST_PMA_CONTAINER_NAME" | |
echo | |
echo "happy coding!" |
Hi @RWareSteve thanks for the comment.
I haven't touched the project that I wrote this script for in a while (or much of WP at all for that matter!) so I'm glad to see you found it useful in your project and to hear that the script is still relevant.
It looks like you landed on exactly what the helper scripts I added to package.json
were doing to spin the dev environment up and down:
- run
wp-env start
first then runbash ./scripts/phpmyadmin/pma-docker.sh
(as documented in the comments in the script) - run
bash ./scripts/phpmyadmin/pma-docker.sh
to stop phpmyadmin and thenwp-env stop
In case its helpful to you when I took a peek just now I also added a few other scripts/commands that I found made working with wp-env a little bit smoother:
"dev:wp:logs": "wp-env run wordpress tail -50 wp-content/debug.log",
"dev:wp:logs:clear": "wp-env run wordpress truncate --size 0 wp-content/debug.log",
"dev:wp:ssh": "wp-env run wordpress bash",
"dev:cli:ssh": "wp-env run wordpress bash",
"wp": "wp-env run cli"
While you may prefer other naming conventions I recall these were pretty handy during development!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps there is a need for a second shell script which keeps "wp-env stop" running without following error:
wp-env stop
✖ Error while running docker-compose command.
Stopping 107b4c692d44a518b3d037f3d3c3e247_tests-cli_1 ...
Stopping 107b4c692d44a518b3d037f3d3c3e247_cli_1 ...
Stopping 107b4c692d44a518b3d037f3d3c3e247_tests-wordpress_1 ...
Stopping 107b4c692d44a518b3d037f3d3c3e247_wordpress_1 ...
Stopping 107b4c692d44a518b3d037f3d3c3e247_tests-mysql_1 ...
Stopping 107b4c692d44a518b3d037f3d3c3e247_mysql_1 ...
Stopping 107b4c692d44a518b3d037f3d3c3e247_tests-cli_1 ... done
Stopping 107b4c692d44a518b3d037f3d3c3e247_cli_1 ... done
Stopping 107b4c692d44a518b3d037f3d3c3e247_tests-wordpress_1 ... done
Stopping 107b4c692d44a518b3d037f3d3c3e247_wordpress_1 ... done
Stopping 107b4c692d44a518b3d037f3d3c3e247_tests-mysql_1 ... done
Stopping 107b4c692d44a518b3d037f3d3c3e247_mysql_1 ... done
Removing 107b4c692d44a518b3d037f3d3c3e247_tests-cli_1 ...
Removing 107b4c692d44a518b3d037f3d3c3e247_cli_1 ...
Removing 107b4c692d44a518b3d037f3d3c3e247_tests-wordpress_1 ...
Removing 107b4c692d44a518b3d037f3d3c3e247_wordpress_1 ...
Removing 107b4c692d44a518b3d037f3d3c3e247_tests-mysql_1 ...
Removing 107b4c692d44a518b3d037f3d3c3e247_mysql_1 ...
Removing 107b4c692d44a518b3d037f3d3c3e247_tests-mysql_1 ... done
Removing 107b4c692d44a518b3d037f3d3c3e247_cli_1 ... done
Removing 107b4c692d44a518b3d037f3d3c3e247_tests-cli_1 ... done
Removing 107b4c692d44a518b3d037f3d3c3e247_mysql_1 ... done
Removing 107b4c692d44a518b3d037f3d3c3e247_tests-wordpress_1 ... done
Removing 107b4c692d44a518b3d037f3d3c3e247_wordpress_1 ... done
Removing network 107b4c692d44a518b3d037f3d3c3e247_default
error while removing network: network 107b4c692d44a518b3d037f3d3c3e247_default id bad3d4fbc869a4a6820f299614c724ac103e6f6425344c3c0e978a962a8deb99 has active endpoints
Already implemented:
on first call of the script "pma-docker.sh" the phpmyadmin container will be started
on second call of the script "pma-docker.sh" the phpmyadmin container will be stopped.
I do these calls:
wp-env start; ~/pma-docker.sh
or
~/pma-docker.sh; wp-env stop
when launching my dev environment.