Sign inSign up

uilicious/statamic

By uilicious

Updated over 3 years ago

Container, adjusted specifically for common statamic deployment use cases, with control panel.

Image
1

7.4K

uilicious/statamic repository overview

Statamic Docker Containers

Prebuilt docker container, adjusted specifically for common statamic deployment use cases, with control panel.

Quick Dev Deployment Patterns

Running on local dev

If you have a local dev copy of the statamic files, you would like to "test" deploy within a docker container, you can run the following

#
# This would mount the current folder, into the statamic docker container.
# And reroute the localhost 8000 port into the docker container port 8000.
#
# Note this will automatically create a `.env` file with APP_DEBUG=true, 
# if it does not exists in the current folder. Peform `composer install`
# And reset its permission to 0777
#
# -it : Runs in interactive mode, logs directly to the console, and terminate 
#       the container when you close the console
#
sudo docker run -it \
    -v "$(pwd):/workspace/statamic" \
    -p 8000:8000 \
    uilicious/statamic 

Quickly Previewing a GIT repo locally

If you find a statamic GIT repo, you would like to quickly preview locally. You can run the following instead.

#
# Startsup a docker container, on localhost port 8000
# which clones a git repository, and does all the  
# initialization requried to do a quick preview
#
sudo docker run -it \
    -e PROJ_SOURCE_TYPE="git" \
    -e PROJ_SOURCE_URL="https://github.com/statamic/starter-kit-starters-creek.git" \
    -e PROJ_SOURCE_GIT_BRANCH="master" \
    -p 8000:8000 \
    uilicious/statamic 

With various environment variable flag

#
# Disables the APP_DEBUG (which defaults to true in this container)
# And include your site unique app key, and statamic key
#
sudo docker run -it \
    -e PROJ_SOURCE_TYPE="git" \
    -e PROJ_SOURCE_URL="<your-project-git-URL>" \
    -e PROJ_SOURCE_GIT_BRANCH="master" \
    -e APP_DEBUG="false" \
    -e APP_KEY="<site-unique-appkey>" \
    -e STATAMIC_LICENSE_KEY="<statamic-unique-key>" \
    -p 8000:8000 \
    uilicious/statamic 

Docker environment variables

Startup settings

Docker ENV VariablePossible ValuesDefault ValueDescription
PROJ_SOURCE_TYPEnone, git, tar, zipnoneIf Enabled - Download from a remote repository (or file) on startup, into the /workspace/statamic directory. Runs either as "none" (disabled), "git" URL, or "tar" / "zip" URL
PROJ_SOURCE_URLValid URL for git/tar/zip mode(starter-kit-git-url)URL to download or pull updates
PROJ_SOURCE_GIT_BRANCHgit branch to clone, for git modemastergit mode only - the git branch to checkout / pull from
PROJ_SOURCE_UPDATEStrue, falsetrueIf Enabled - Pull updates, if the project is already initialize

Auto Update settings

Docker ENV VariablePossible ValuesDefault ValueDescription
PROJ_PERODIC_UPDATEStrue, falsefalsePerodically pull updates from the PROJ_SOURCE_URL for changes
PROJ_PERODIC_UPDATE_INTERVALauto, 5m, 60s, 24h (valid time based value)autoAuto defaults to 5m for git repository, and 12h for tar/zip
PROJ_GIT_PUSH_BEFORE_PULLtrue, falsetrueOnly for git, enable push before pull updates
PROJ_GIT_PUSH_EMAILemail address string[email protected]Git email, to use for the commit
PROJ_GIT_PUSH_NAMEname of user/accountstatamic docker botGit name, to use for the commit
PROJ_GIT_PUSH_MSGgit message to use to commitperiodic commit ...Git message, to use for the commit

Statamic runtime settings

Docker ENV VariablePossible ValuesDefault ValueDescription
PROJ_RUN_COMPOSER_INSTALLtrue, falsetrueIf Enabled - Run composer install on container startup
PROJ_OVERWRITE_ENV_FILE_ENABLEtrue, falsetrueIf Enabled - Overwrite the project /workspace/statamic/.env with the specified file, if it exists. This is useful for overwriting config files in kubernetes.
PROJ_OVERWRITE_ENV_FILE_PATHValid file path, to copy the .env file from/workspace/.envThe file path to check, to overwrite inside the project (if present)
PROJ_RESET_PROJ_PERMISSIONtrue, falsetrueIf Enabled - Reset the project files permission, on startup
PROJ_RESET_PROJ_PERMISSION_LEVELValid chmod permission values0777Chmod value to overwrite with on startup
PROJ_SERVER_RUN_MODEnginx, artisannginxServer run mode to use, for production please use only the "nginx" mode
PROJ_AUTOSETUP_ENV_FILEtrue, falsetrueIf Enabled - Automatically setup the site .env file, if it does not exists (skips if APP_DEBUG, or APP_KEY is configured)
PROJ_AUTOSETUP_APP_KEYtrue, falsefalseIf Enabled - Generate a new APP_KEY, if its not configured in the .env file (or environment value)
PROJ_TAIL_ERROR_LOGStrue, falsetrueIf Enabled - At the end of the server startup, tail the various nginx error logs, into the docker container stdout
PROJ_DOCKER_COMMAND_MODEoverwrite, chainoverwriteHow docker command arguments should be handled, this is only useful for debugging purposes mostly.

Docker file structure

  • /workspace/statamic/ : Directory for the statamic site
  • /workspace/.env : to copy and overwrite /workspace/statamic/.env if present with PROJ_OVERWRITE_ENV_FILE_ENABLE enabled

Production deployment options

Note: Enabling HTTPS behind a proxy

It is common for a kubernetes / docker cluster setup to have a HTTPS-to-HTTP reverse proxy before the application container. To configure the statamic site to properlly give URL links in HTTPS you should add the following to your statamic site routes/web.php

// ----------------------------------------------------
// Adding HTTPS schema / proxy url overwrite options
//
// Modified from: https://cylab.be/blog/122/using-https-over-a-reverse-proxy-in-laravel
// ----------------------------------------------------
$app_url = config("app.url");
if (!empty($app_url)) {
    // URL::forceRootUrl($app_url);
    $schema = explode(':', $app_url)[0];
    URL::forceScheme($schema);
}
// ----------------------------------------------------

Once done, make sure to configure the env variables required for APP_URL and ASSET_URL to include the https:// prefix

Pull the GIT repository, on docker startup, push GIT changes on editor updates

The following would get a statamic server up and running, which will automatically push any changes back into the repository.

sudo docker run -it \
    -e PROJ_SOURCE_TYPE="git" \
    -e PROJ_SOURCE_URL="<your-project-git-URL>" \
    -e PROJ_SOURCE_GIT_BRANCH="<git-branch>" \
    -e APP_DEBUG="false" \
    -e APP_KEY="<site-unique-appkey>" \
    -e STATAMIC_LICENSE_KEY="<statamic-license-key>" \
    -e STATAMIC_GIT_ENABLED="true" \
    -e STATAMIC_GIT_PUSH="true" \
    -e APP_URL="https://<site-url>" \
    -e ASSET_URL="https://<site-url>" \
    -p 8000:8000 \
    uilicious/statamic 

Note: This does not pull any updates from the git repository automatically, you should get your git repository to trigger a "git pull" within the container "/workspace/statamic" folder respectively.

Extend the docker container with your files

@TODO

I want to build my own container (not extend this container)

The way the entrypoint script works is highly oppinionated, and has been adjusted for my personal use case - so i understand if you would like to do it differently.

What your looking for probably is the list of dependencies you would need to get things up and running (without 3 whole days of trial and error), so the following is what we used on alpine to make this work for most use cases, and what you can base your builds on.

FROM alpine:3.14

#
# Add some required PHP "addons"
#
# and other addons required by statamic
# along with a nginx server.
#
RUN apk add --no-cache \
        # Install php, and nginx \
        php8 php8-cli php8-common php8-fpm nginx \
        # Install dependency library for opcache \
        pcre-dev \
        # Utilities used to download / unzip files \
        unzip wget curl \
        # Image GD support (required for uploading images)
        freetype-dev libjpeg-turbo-dev libpng-dev gd \
        # Git command support \
        git openssh \
        # Bash, because its easier for me to write entryscripts for =D \
        # Vim, is because its my prefer editor to debug things =X \
        bash vim \
        # PHP opcache optimization
        php8-opcache \
        # PHP mysql extension \
        php8-mysqli \
        # PHP GD image manipulation support
        php8-gd php8-exif \
        # Required to be able to pull updates (download)
        php8-zip php8-zlib php8-curl \
        # Other PHP addons, I do not know what they maybe used for specifically \
        # But errors maybe thrown in the installation / setup process otherwise \
        php8-mbstring php8-xml php8-bcmath \
        php8-pdo php8-tokenizer php8-xml \
        php8-pcntl php8-phar php8-dom php8-xmlwriter \
        php8-fileinfo php8-session

# Create the symlink for php to php8 (as alpine does not support this)
RUN ln -s $(which php8) /usr/bin/php && \
    ln -s $(which php-fpm8) /usr/bin/php-fpm

# In addition we need to run the relevent docker-php-ext install
# NOTE: Due to the current issue of PHP8 official container, and some of the extensions listed here
#       we are depending on the alpine packages instead.
#
# RUN docker-php-ext-install \
#         opcache zlib curl mbstring xml bcmath gd mysqli pdo pdo_mysql tokenizer xml pcntl

#
# Install PHP composer
# Version and SHA can be found here : https://getcomposer.org/download/
#
ENV COMPOSER_VERSION 2.1.5
ENV COMPOSER_CHECKSUM be95557cc36eeb82da0f4340a469bad56b57f742d2891892dcb2f8b0179790ec
RUN wget -q https://getcomposer.org/download/$COMPOSER_VERSION/composer.phar && \
    echo "$COMPOSER_CHECKSUM  composer.phar" | sha256sum -c - && \
    mv composer.phar /usr/bin/composer && \
    chmod +x /usr/bin/composer

You can find the above inside our dockerfile, and if we have a module missing for a use case - just let me know in a pull request, and i will add it.

General Disclaimer

In event that an official statamic docker container is released and maintained, it is highly recommended to validate your use case against the official container (instead of this container) as we have no long term plans to maintain this container on a regular basis, or anything beyond our use cases. This is opensourced for refrence for you to either extend or build your own container. YMMV.

Tag summary

Content type

Image

Digest

sha256:214bb868e

Size

31.4 MB

Last updated

over 3 years ago

docker pull uilicious/statamic