Last active
June 21, 2024 10:52
-
-
Save algj/89823668feb68f1818a2031a2d64128e to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
cleanup() { | |
if [ -n "$MYSQL_CONTAINER_ID" ]; then | |
echo "Stopping MySQL container..." | |
sudo docker stop $MYSQL_CONTAINER_ID > /dev/null | |
fi | |
if [ -n "$WORDPRESS_CONTAINER_ID" ]; then | |
echo "Stopping WordPress container..." | |
sudo docker stop $WORDPRESS_CONTAINER_ID > /dev/null | |
fi | |
exit 0 | |
} | |
trap cleanup SIGINT | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <port> <directory>" | |
exit 1 | |
fi | |
PORT=$1 | |
DIR=$2 | |
mkdir -p $DIR/sql | |
mkdir -p $DIR/html | |
sudo chown -R $(whoami) $DIR | |
USER_ID=$(id -u) | |
GROUP_ID=$(id -g) | |
# if docker ran correctly | |
check_docker_command() { | |
if [ $? -ne 0 ]; then | |
echo "Error: Docker command failed. Please ensure Docker is running, you have permissions to run doc | |
cleanup | |
fi | |
} | |
# run mysql | |
MYSQL_CONTAINER_ID=$(sudo docker run -d \ | |
--rm \ | |
-e MYSQL_ROOT_PASSWORD=admin \ | |
-e MYSQL_DATABASE=wordpress \ | |
-e MYSQL_USER=admin \ | |
-e MYSQL_PASSWORD=admin \ | |
-v $DIR/sql:/var/lib/mysql \ | |
--user $USER_ID:$GROUP_ID \ | |
--name mysql_container_$(date +%s) \ | |
mysql:latest) | |
check_docker_command | |
# get mysql IP | |
MYSQL_IP=$(sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $MYSQL_CONTAINE | |
check_docker_command | |
# run wp in docker | |
WORDPRESS_CONTAINER_ID=$(sudo docker run -d \ | |
--rm \ | |
-e WORDPRESS_DB_HOST=$MYSQL_IP:3306 \ | |
-e WORDPRESS_DB_USER=admin \ | |
-e WORDPRESS_DB_PASSWORD=admin \ | |
-e WORDPRESS_DB_NAME=wordpress \ | |
-p $PORT:80 \ | |
-v $DIR/html:/var/www/html \ | |
--user $USER_ID:$GROUP_ID \ | |
--name wordpress_container_$(date +%s) \ | |
wordpress:latest) | |
check_docker_command | |
# if not failed, print details | |
if [ -n "$MYSQL_CONTAINER_ID" ] && [ -n "$WORDPRESS_CONTAINER_ID" ]; then | |
echo "MySQL Container ID: $MYSQL_CONTAINER_ID" | |
echo "WordPress Container ID: $WORDPRESS_CONTAINER_ID" | |
echo "MySQL IP Address: $MYSQL_IP" | |
echo "" | |
echo -e "Running on: \t\thttp://localhost:$PORT/" | |
echo -e "Admin panel: \t\thttp://localhost:$PORT/wp-admin/" | |
fi | |
# wait for containers to stop | |
sudo sh -c " | |
cleanup() { | |
if [ -n \"$MYSQL_CONTAINER_ID\" ]; then | |
echo \"Stopping MySQL container...\" | |
docker stop $MYSQL_CONTAINER_ID > /dev/null | |
fi | |
if [ -n \"$WORDPRESS_CONTAINER_ID\" ]; then | |
echo \"Stopping WordPress container...\" | |
docker stop $WORDPRESS_CONTAINER_ID > /dev/null | |
fi | |
exit 0 | |
} | |
trap 'echo Received SIGINT; cleanup' INT; | |
docker wait $MYSQL_CONTAINER_ID; [ -n \"$MYSQL_CONTAINER_ID\" ] && cleanup | |
" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment