visiblevc / wordpress-starter

A slightly less shitty wordpress development workflow
688 stars 167 forks source link

How to connect to external database? #188

Open launchthatbrand opened 9 months ago

launchthatbrand commented 9 months ago

How can I use this to connect to an external cloud database from the local site?

It seems like in your base docker image you have

config create:
    dbhost: ${DB_HOST:-db}:3306
    ...

which is shown in the root run.sh.

Do i need to extend your image to overwrite this value?

I tried just replacing the DB Host env variable but I am getting database connection errors, presumably because it is still trying to use <mycloudhost>:3306 as the connection string.

Can you provide a quick tutorial for building the docker image custom?

launchthatbrand commented 9 months ago

For anyone looking at this in the future...

Everything is there for you to build a local docker image based on this github repo.

If you are using Windows switch over to WSL2 using vscode. IN the root of the project run:

npm run build

which will generate local versions of each php version specified in the /images/ folder of the repo.

For myself I still had issues running this process, specifically with outdated packages in any of the php 7.* dockerfiles. I just deleted those folders from the project, which got me further but I still ran into build issues with the php8 image.

I had to modify how it grabbed python-certbot-apache as it is now python3-certbot-apache. Here is my modified Dockerfile:

FROM php:8.0-apache
ARG VERSION=latest
LABEL maintainer="Derek P Sifford <dereksifford@gmail.com>" \
      version="${VERSION}-php8.0"

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Install base requirements & sensible defaults + required PHP extensions
# hadolint ignore=DL3008
RUN echo "deb http://ftp.debian.org/debian $(sed -n 's/^VERSION=.*(\(.*\)).*/\1/p' /etc/os-release)-backports main" >> /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        bash-completion \
        bindfs \
        ghostscript \
        less \
        libjpeg-dev \
        libmagickwand-dev \
        libpng-dev \
        libxml2-dev \
        libzip-dev \
        mariadb-client \
        sudo \
        unzip \
        vim \
        zip \
        certbot \
        python3-certbot-apache \
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    # TODO: uncomment this when imagick supports php8
    # && pecl install imagick \
    && pecl install redis \
    && docker-php-ext-install \
        bcmath \
        exif \
        gd \
        mysqli \
        opcache \
        soap \
        zip \
    # TODO: uncomment this when imagick supports php8
    # && docker-php-ext-enable imagick \
    && docker-php-ext-enable redis \
    && { \
        echo 'memory_limit = 512M'; \
        # See https://github.com/visiblevc/wordpress-starter/issues/160#issuecomment-544561961
        echo 'upload_max_filesize = 50M'; \
    } > /usr/local/etc/php/php.ini \
    # See https://secure.php.net/manual/en/opcache.installation.php
    && { \
        echo 'opcache.memory_consumption = 128'; \
        echo 'opcache.interned_strings_buffer = 8'; \
        echo 'opcache.max_accelerated_files = 4000'; \
        echo 'opcache.revalidate_freq = 2'; \
        echo 'opcache.fast_shutdown = 1'; \
        echo 'opcache.enable_cli = 1'; \
    } > /usr/local/etc/php/conf.d/opcache-recommended.ini \
    && sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf \
    # Fixes issue where error is logged stating apache could not resolve the
    # fully qualified domain name
    && echo 'ServerName localhost' > /etc/apache2/conf-available/fqdn.conf \
    # Grab and install wp-cli from remote
    && curl --create-dirs \
        -o /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
        -o /etc/bash_completion.d/wp-cli https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash \
    && a2enconf fqdn \
    && a2enmod rewrite expires

# Add admin superuser, create install directory, adjust perms, & add symlink
COPY run.sh /run.sh
RUN useradd -ms /bin/bash -G sudo admin \
    && echo "admin ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/admin \
    && chmod 0440 /etc/sudoers.d/admin \
    && chmod +x /usr/local/bin/wp /run.sh \
    && mkdir -m 0700 /app \
    && chown admin:admin /app \
    && printf '%s\t%s\t%s\t%s%s%s%s%s%s%s%s\t%d\t%d\n' \
        '/app' \
        '/var/www/html' \
        'fuse.bindfs' \
            'force-user=www-data,' \
            'force-group=www-data,' \
            'create-for-user=admin,' \
            'create-for-group=admin,' \
            'create-with-perms=0644:a+X,' \
            'chgrp-ignore,' \
            'chown-ignore,' \
            'chmod-ignore' \
        0 \
        0 > /etc/fstab

USER admin
WORKDIR /app
EXPOSE 80 443
CMD ["/run.sh"]

This got me an image built locally to use, I then needed to modify the docker-compose.yaml file to use this image:

version: "3"
services:
    wordpress:
        image: visiblevc/wordpress:latest-php8.0

This got the wordpress docker install launched and working, then I wanted to make it work connecting with Planetscale cloud database rather than local mariadb.

I created a custom wp-config.php file and placed it in /example/custom/wp-config.php, then volume mounted it like:

volumes:
            - ./data:/data
            - ./scripts:/docker-entrypoint-initwp.d
            - ./custom/wp-config.php:/app/wp-config.php

This has got it working as a base level for me. I will come back with further refinements to the build script once I reach another checkpoint, as now I do not need things like wp config create