create some scripts

This commit is contained in:
whaffman 2025-05-16 14:54:18 +02:00
parent c51cd0a471
commit 81ddf62aec
6 changed files with 190 additions and 1 deletions

View File

@ -0,0 +1,27 @@
DOMAIN_NAME=whaffman.42.fr
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=42wordpress42
MYSQL_ROOT_PASSWORD=42root42
DB_HOST=localhost
DB_PORT=3306
WP_TITLE=Inception
WP_DESCRIPTION=Inception project
WP_ADMIN=theboss
WP_ADMIN_PASSWORD=42theboss42
WP_ADMIN_EMAIL=inception@duinvoetje.nl
WP_THEME=twentytwentyfour
WP_USER=inception
WP_USER_PASSWORD=42inception42
WP_USER_EMAIL=inception@duinvoetje.nl

View File

@ -1,3 +1,49 @@
FROM alpine:3.20
RUN apk add --no-cache mariadb mariadb-client
# Install MariaDB and MariaDB client
# The --no-cache option is used to prevent the package manager from caching the downloaded packages
# rm -rf /var/cache/apk/* is used to remove any cached files after installation
# This helps to keep the image size small
RUN apk add --no-cache \
mariadb \
mariadb-client && \
rm -rf /var/cache/apk/*
# Create a new user and group
RUN addgroup -S mariadb && \
adduser -S mariadb -G mariadb
# Create the necessary directories
RUN mkdir -p /var/run/mysqld && \
chown -R mariadb:mariadb /var/run/mysqld && \
chown -R mariadb:mariadb /var/lib/mysql
# Copy the entrypoint script and configuration files
# and set the necessary permissions
# The entrypoint script will be responsible for initializing the database
# and starting the MariaDB server
# The configuration file will be used to set the server settings
# The init.sql file will be executed when the container is started for the first time
COPY ./docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY ./my.cnf /etc/my.cnf
COPY ./init.sql /usr/local/bin/init.sql
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Expose the MySQL port
EXPOSE 3306
# Set the user and group to run the container
USER mariadb:mariadb
# Set the working directory
WORKDIR /var/lib/mysql
# Set the entrypoint script to be executed when the container starts
# The entrypoint script will initialize the database and start the server
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
# Start the MariaDB server
CMD ["mysqld"]

View File

@ -0,0 +1,6 @@
[mysqld]
port=3306
bind-address=0.0.0.0
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid

View File

@ -0,0 +1,48 @@
#!/bin/bash
set -e
# If the first argument is 'mysqld', then start MariaDB
if [ "$1" = 'mysqld' ]; then
# Initialize the database if it doesn't exist
if [ ! -d "/var/lib/mysql/mysql" ]; then
echo "Initializing database..."
mysql_install_db --user=mysql --datadir=/var/lib/mysql --rpm
echo "Database initialized."
# Start MariaDB temporarily to set up initial configuration
mysqld --user=mysql --skip-networking &
pid="$!"
# Wait for MariaDB to start
while ! mysqladmin ping --silent; do
sleep 1
done
# Set up root user and any initial database
echo "Setting up initial database..."
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';"
if [ -n "$MYSQL_DATABASE" ]; then
mysql -e "CREATE DATABASE IF NOT EXISTS \`${MYSQL_DATABASE}\`;"
fi
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
mysql -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';"
mysql -e "GRANT ALL PRIVILEGES ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%';"
fi
mysql -e "FLUSH PRIVILEGES;"
# Load initial SQL dump if init.sql exists
if [ -f "/usr/bin/local/init.sql" ]; then
echo "Loading initial SQL dump..."
mysql "${MYSQL_DATABASE}" < /usr/bin/local/init.sql
echo "Initial SQL dump loaded."
fi
# Stop temporary MariaDB instance
kill "$pid"
wait "$pid"
fi
echo "Starting MariaDB..."
fi
exec "$@"

View File

@ -0,0 +1,31 @@
FROM:alpine:3.20
# Install WordPress and its dependencies
# The --no-cache option is used to prevent the package manager from caching the downloaded packages
# rm -rf /var/cache/apk/* is used to remove any cached files after installation
# This helps to keep the image size small
RUN apk add --no-cache \
php83-fpm \
php83-mysqli \
php83-mbstring \
php83-json \
php83-curl \
php83-xml \
php83-zip \
php83-gd \
php83-session \
mariadb-client \
rm -rf /var/cache/apk/*
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
chmod +x wp-cli.phar && \
mv wp-cli.phar /usr/local/bin/wp
RUN mkdir -p /var/www/html && \
chown -R www-data:www-data /var/www/html
# Copy the entrypoint script and configuration files
# and set the necessary permissions
# The entrypoint script will be responsible for initializing the database
COPY ./docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

View File

@ -0,0 +1,31 @@
#!bin/sh
# This script is used to install WordPress and configure it with a MariaDB database.
# It checks for the presence of a database and user, creates them if they don't exist,
# and sets up the WordPress configuration file with the database connection details.
wp core download --allow-root --path=/var/www/html
wp config create --allow-root \
--dbname=$MYSQL_DATABASE \
--dbuser=$MYSQL_USER \
--dbpass=$MYSQL_PASSWORD \
--dbhost=$DB_HOST \
--path=/var/www/html
wp core install --allow-root \
--url=$DOMAIN_NAME \
--title=$WP_TITLE \
--admin_user=$WP_ADMIN \
--admin_password=$WP_ADMIN_PASSWORD \
--admin_email=$WP_ADMIN_EMAIL \
--path=/var/www/html
wp user create --allow-root \
$WP_USER \
$WP_USER_EMAIL \
--role=author \
--user_pass=$WP_USER_PASSWORD \
--path=/var/www/html
wp theme install --allow-root \
$WP_THEME \
--activate \
--path=/var/www/html