Skip to content

Commit

Permalink
Merge pull request #154 from stellarwp/refactor/leverage-docker-compose
Browse files Browse the repository at this point in the history
Rewrite the service management to leverage Docker Compose
  • Loading branch information
lucatume authored Mar 30, 2023
2 parents 335bbf4 + a5ba0c4 commit b6c0e0a
Show file tree
Hide file tree
Showing 23 changed files with 356 additions and 844 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2023-03-30

* Change - Leverage Docker Compose syntax to manage network aliases and service dependencies. Clean up a remove code.
* Add the `dc` command to run Docker Compose commands in the stack.
* Support the `SLIC_DOCKER_COMPOSE_BIN` environment variable to allow for the use a different Docker Compose binary, the `docker compose` one is used by default.

## [1.2.4] - 2023-03-21

* Fix - [issue 142](https://github.com/stellarwp/slic/issues/142) ensure the hosts file in the docker container contains a tab character before trying to explode it.
Expand Down
16 changes: 8 additions & 8 deletions slic-stack.site.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# docker-compose configuration file used to run cross-activation tests.
# docker compose configuration file used to run cross-activation tests.

version: "3"

Expand All @@ -8,32 +8,32 @@ services:
volumes:
# Paths are relative to the directory that contains this file, NOT the current working directory.
# Share the WordPress core installation files in the `_wordpress` directory.
- ${SLIC_WP_DIR}:/var/www/html:cached
- ${SLIC_WP_DIR}:/var/www/html

cli:
volumes:
# Paths are relative to the directory that contains this file, NOT the current working directory.
# Share the WordPress core installation files in the `_wordpress` directory.
- ${SLIC_WP_DIR}:/var/www/html:cached
- ${SLIC_WP_DIR}:/var/www/html

codeception:
environment:
# Move to the target directory before running the command from the plugins directory.
CODECEPTION_PROJECT_DIR: /var/www/html/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}
volumes:
# Set the current site as project.
- ${SLIC_HERE_DIR}/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}:/project:cached
- ${SLIC_HERE_DIR}/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}:/project
# Paths are relative to the directory that contains this file, NOT the current working directory.
# Share the WordPress core installation files in the `_wordpress` directory.
- ${SLIC_WP_DIR}:/var/www/html:cached
- ${SLIC_WP_DIR}:/var/www/html

composer:
volumes:
# Set the current target as project.
- ${SLIC_HERE_DIR}/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}:/project:cached
- ${COMPOSER_CACHE_DIR}:${COMPOSER_CACHE_DIR}:cached
- ${SLIC_HERE_DIR}/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}:/project
- ${COMPOSER_CACHE_DIR}:${COMPOSER_CACHE_DIR}

npm:
volumes:
# Set the current plugin as project.
- ${SLIC_HERE_DIR}/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}:/project:cached
- ${SLIC_HERE_DIR}/${SLIC_CURRENT_PROJECT_RELATIVE_PATH}:/project
112 changes: 63 additions & 49 deletions slic-stack.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# docker-compose configuration file used to run cross-activation tests.
# docker compose configuration file used to run cross-activation tests.

version: "3"
version: "3.9"

networks:
slic:
Expand All @@ -11,33 +11,53 @@ volumes:
services:

db:
image: mariadb:10.7.3
image: mariadb:10.7.8
networks:
- slic
ports:
- "${SLIC_DB_LOCALHOST_PORT:-9006}:3306"
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root}
healthcheck: # The `test` db should exist.
test: mysqlshow -u root -p${MYSQL_ROOT_PASSWORD:-root} test
start_period: 5s
interval: 1s
timeout: 3s
retries: 30
tmpfs:
- /var/lib/mysql

redis:
image: redis
image: redis:7.0.10
networks:
slic:
- slic
ports:
- "${SLIC_REDIS_LOCALHOST_PORT:-8379}:6379"
healthcheck: # It should reply PONG to PING
test: redis-cli ping | grep PONG
start_period: 2s
interval: 1s
timeout: 3s
retries: 30
tmpfs:
- /data

wordpress:
image: ghcr.io/stellarwp/slic-wordpress-php${SLIC_PHP_VERSION}:${SLIC_VERSION}
networks:
- slic
slic:
aliases: # Allow reaching the site at `http://wordpress.test` and other domains that might be used in testing.
- wordpress.test
- www.wordpress.test
- test1.wordpress.test
- test2.wordpress.test
- test3.wordpress.test
depends_on:
- db
- redis
db:
condition: service_healthy
redis:
condition: service_healthy
# Run the container as the host user and group.
# Apache will run as the same user and permission issues with WordPress generated files should not arise.
user: "${SLIC_UID:-}:${SLIC_GID:-}"
Expand Down Expand Up @@ -69,36 +89,58 @@ services:
define( 'TRIBE_NO_FREEMIUS', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'WP_DEBUG_LOG', true );
define( 'DISABLE_WP_CRON', true );
define( 'WP_HTTP_BLOCK_EXTERNAL', true );
# Configure this to debug the tests with XDebug.
# Map the `_wordpress` directory to `/var/www/html' directory in your IDE of choice.
# Map the `_plugins` directory to `/plugins` directory in your IDE of choice.
PHP_IDE_CONFIG: "serverName=${XDK:-slic}"
# The `remote_host` is set to `host.docker.internal` that will resolve to the host machine IP address, from
# within the container, on macOS and Windows.
# On Linux set the host machine IP address before calling the stack:
# XDH=$(ip route | grep docker0 | awk '{print $9}') docker-compose ...
# XDH=$(ip route | grep docker0 | awk '{print $9}') docker compose ...
XDEBUG_CONFIG: "idekey=${XDK:-slic} remote_enable=${XDE:-1} remote_host=${XDH:-host.docker.internal} remote_port=${XDP:-9001} client_host=${XDH:-host.docker.internal} client_port=${XDP:-9001}"
# Whether to disable the XDebug extension in the Codeception container completely or not.
XDEBUG_DISABLE: "${XDEBUG_DISABLE:-0}"
volumes:
# Paths are relative to the directory that contains this file, NOT the current working directory.
- ${SLIC_WP_DIR}:/var/www/html:cached
- ${SLIC_PLUGINS_DIR}:/var/www/html/wp-content/plugins:cached
- ${SLIC_THEMES_DIR}:/var/www/html/wp-content/themes:cached
- ${COMPOSER_CACHE_DIR:-./.cache}:/composer-cache:cached
- ${SLIC_WP_DIR}:/var/www/html
- ${SLIC_PLUGINS_DIR}:/var/www/html/wp-content/plugins
- ${SLIC_THEMES_DIR}:/var/www/html/wp-content/themes
- ${COMPOSER_CACHE_DIR:-./.cache}:/composer-cache
healthcheck: # Apache service should be running correctly.
test: service apache2 status
start_period: 5s
interval: 1s
timeout: 3s
retries: 30

chrome:
image: ${SLIC_CHROME_CONTAINER:-selenium/standalone-chrome:3.141.59-oxygen}
image: ${SLIC_CHROME_CONTAINER:-selenium/standalone-chrome:3.141.59}
networks:
- slic
extra_hosts:
- "wordpress.test:172.${SLIC_TEST_SUBNET:-28}.1.1"
depends_on:
wordpress:
condition: service_healthy
healthcheck: # It should reply with a 200 status code to a request to the status endpoint.
test: curl -f http://localhost:4444/wd/hub/status
start_period: 5s
interval: 1s
timeout: 3s
retries: 30

slic:
image: ghcr.io/stellarwp/slic-php${SLIC_PHP_VERSION}:${SLIC_VERSION}
networks:
- slic
user: "${SLIC_UID:-}:${SLIC_GID:-}"
depends_on:
db:
condition: service_healthy
chrome:
condition: service_healthy
wordpress:
condition: service_healthy
environment:
COMPOSER_AUTH: "${COMPOSER_AUTH:-}"
COMPOSER_CACHE_DIR: "/composer-cache"
Expand All @@ -115,7 +157,7 @@ services:
# The `remote_host` is set to `host.docker.internal` that will resolve to the host machine IP address, from
# within the container, on macOS and Windows.
# On Linux set the host machine IP address before calling the stack:
# XDH=$(ip route | grep docker0 | awk '{print $9}') docker-compose ...
# XDH=$(ip route | grep docker0 | awk '{print $9}') docker compose ...
XDEBUG_CONFIG: "idekey=${XDK:-slic} remote_enable=${XDE:-1} remote_host=${XDH:-host.docker.internal} remote_port=${XDP:-9001} client_host=${XDH:-host.docker.internal} client_port=${XDP:-9001}"
# Move to the target directory before running the command from the plugins directory.
CODECEPTION_PROJECT_DIR: /var/www/html/wp-content/plugins/${SLIC_CURRENT_PROJECT:-test}/${SLIC_CURRENT_PROJECT_SUBDIR:-}
Expand All @@ -137,43 +179,15 @@ services:
volumes:
# Paths are relative to the directory that contains this file, NOT the current working directory.
# Share the WordPress core installation files in the `_wordpress` directory.
- ${SLIC_WP_DIR}:/var/www/html:cached
- ${SLIC_WP_DIR}:/var/www/html
# Share the plugins in the `/var/www/hmtl/wp-content/plugins` directory.
- ${SLIC_PLUGINS_DIR}:/var/www/html/wp-content/plugins:cached
- ${SLIC_THEMES_DIR}:/var/www/html/wp-content/themes:cached
- ${SLIC_PLUGINS_DIR}:/var/www/html/wp-content/plugins
- ${SLIC_THEMES_DIR}:/var/www/html/wp-content/themes
# In some plugins we use function-mocker and set it up to cache in `/tmp/function-mocker`.
# To avoid a long re-caching on each run, let's cache in a docker volume, caching on the host
# filesystem would be a worse cure than the disease.
# The volume is bound to the `a+rwx` directory the `slic` image provides to avoid file mode issues.
- function-mocker-cache:/cache
- ${COMPOSER_CACHE_DIR:-./.cache}:/composer-cache
# Scripts volume
- ${SLIC_SCRIPTS}:/slic-scripts:cached

# adminer:
# image: adminer
# networks:
# - slic
# environment:
# ADMINER_DEFAULT_SERVER: db
# ports:
# - "9080:8080"
#
#
# redis-cli:
# image: redis
# networks:
# slic:
# depends_on:
# - redis
# entrypoint: ["redis-cli","-h redis","-p 6379"]
# command: ["--version"]
#
# mailcatcher:
# image: dockage/mailcatcher
# networks:
# slic:
# ports:
# # Expose MailCatcher ports on localhost.
# - "1025:1025"
# - "1080:1080"
- ${SLIC_SCRIPTS}:/slic-scripts
6 changes: 3 additions & 3 deletions slic.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The entrypoint to anything you will need to do will be the `slic` binary, locate

## Requirements

The stack runs based on [Docker](https://www.docker.com/) and [docker-compose](https://docs.docker.com/compose/): you will need both installed and available to be able to use the local testing environment.
The stack runs based on [Docker](https://www.docker.com/) and [docker compose](https://docs.docker.com/compose/): you will need both installed and available to be able to use the local testing environment.
If your Docker installation requires root access, please follow [this guide](https://docs.docker.com/install/linux/linux-postinstall/) to make sure root access is not required to run `docker` commands.

You should be able to run the following command without issues and without requiring root access:
Expand All @@ -16,7 +16,7 @@ You should be able to run the following command without issues and without requi
docker run hello-world
```

If this is not the case, please take the time to read Docker and docker-compose documentation and fix the issues you encounter.
If this is not the case, please take the time to read Docker and docker compose documentation and fix the issues you encounter.

## Where to get help

Expand Down Expand Up @@ -178,7 +178,7 @@ slic cli db export /plugins/the-events/calendar/tests/_data/dump.sql
## How the stack works, an overview

The stack services are defined by the `slic-stack.yml` file.
This is a YAML format docker-compose configuration file the `slic` binary will use to run the `docker-compose` command.
This is a YAML format docker compose configuration file the `slic` binary will use to run the `docker compose` command.
The main services defined there are:

* `wordpress` - this uses the `wordpress:latest` image, the official Docker image for WordPress. When running the container will fill the `_wordpress` directory with the contents of the WordPress installation that is currently serving the container. Furthermore the WordPress container is configured to look for plugins in the `/plugins` directory, that directory is a shared volume that you can find in the `dev/test/plugins` directory.
Expand Down
Loading

0 comments on commit b6c0e0a

Please sign in to comment.