Example: run WordPress
Run an off-the-shelf application — WordPress — as a Coderunner Service.
This example runs WordPress, an application you didn't write, as a Service. Nothing here is specific to WordPress: any application that ships as a container and serves HTTP runs the same way. Swap in your own image and settings.
What to know first
- Storage is temporary. Anything WordPress writes to local disk, such as uploads, is lost when the Service restarts or you redeploy. A production site should store media elsewhere, or keep it on a persistent disk mounted at
/data. - WordPress needs MySQL. Groundfloor's managed databases are PostgreSQL, so this example uses a MySQL database you already run elsewhere. If your own application can use PostgreSQL, use a Groundfloor managed database instead.
Before you start
- The
gfCLI, signed in:gf login, thengf workspaces use <workspace-id>. - A MySQL database and user for WordPress, reachable from the internet.
1. Store the database password
gf secrets set WORDPRESS_DB_PASSWORD - # paste the password, then press Ctrl+DStore it in the environment the Coderunner will run in; add -e <environment-id> to choose one.
2. Write the start script and Dockerfile
Groundfloor doesn't put your secrets into the container as environment variables. The container reads the password from the Secrets API when it starts, using the machine client you attach in step 3, and then starts WordPress with the password as WORDPRESS_DB_PASSWORD, which the official image reads. The script reads the password stored for the Coderunner's own environment (without environment_id, a production site would get the dev one). It removes the client's credentials before starting WordPress, so WordPress never sees them, and stops if it can't read the password. See Read secrets at runtime.
#!/bin/sh
set -eu
API="$CONTROLPLANE_URL/v1/workspaces/$GROUNDFLOOR_WORKSPACE_ID"
# The credentials go to curl on stdin, never as command-line arguments.
TOKEN=$(jq -n '{client_id: env.GROUNDFLOOR_M2M_CLIENT_ID, client_secret: env.GROUNDFLOOR_M2M_CLIENT_SECRET}' \
| curl -sf -X POST "$API/m2m/token" -H "Content-Type: application/json" -d @- \
| jq -r '.access_token // empty')
[ -n "$TOKEN" ] || { echo "start.sh: couldn't get an M2M token" >&2; exit 1; }
WORDPRESS_DB_PASSWORD=$(printf 'Authorization: Bearer %s\n' "$TOKEN" \
| curl -sf -H @- "$API/secrets/WORDPRESS_DB_PASSWORD?environment_id=${GROUNDFLOOR_ENVIRONMENT_ID:-}" \
| jq -r '.value // empty')
[ -n "$WORDPRESS_DB_PASSWORD" ] || { echo "start.sh: couldn't read WORDPRESS_DB_PASSWORD" >&2; exit 1; }
export WORDPRESS_DB_PASSWORD
unset GROUNDFLOOR_M2M_CLIENT_ID GROUNDFLOOR_M2M_CLIENT_SECRET TOKEN
exec docker-entrypoint.sh apache2-foregroundThe official image listens on port 80; a Service must listen on 8080. The database settings that aren't secret go in the image as ENV lines, so every deploy has them; Groundfloor doesn't keep deploy-time variables between deploys. The image also needs jq and the script:
FROM wordpress:latest
RUN apt-get update && apt-get install -y --no-install-recommends jq && rm -rf /var/lib/apt/lists/* \
&& sed -i 's/Listen 80/Listen 8080/' /etc/apache2/ports.conf \
&& sed -i 's/:80>/:8080>/' /etc/apache2/sites-available/000-default.conf
COPY start.sh /usr/local/bin/start.sh
RUN chmod +x /usr/local/bin/start.sh
ENV WORDPRESS_DB_HOST=your-mysql-host:3306 \
WORDPRESS_DB_USER=wordpress \
WORDPRESS_DB_NAME=wordpress
EXPOSE 8080
ENV PORT=8080
CMD ["start.sh"]3. Give the Service a machine client
In the Client Portal, go to Administer → Access → Machine clients and create a client with Can read secrets turned on. Leave Can deploy services off. Deploy the Service once (step 4), then attach the client on the Service's Machine client card and deploy again: the container gets the client's credentials at deploy time. See Give a Coderunner a machine client.
4. Deploy
From the folder with the Dockerfile:
gf deploy --name wordpress --workload-type service --runtime node --cpu 500m --memory 512MiThe first deploy has no client attached yet, so the script stops with "couldn't get an M2M token" and the deployment shows Failed; that's expected. Attach the client (step 3), then deploy again with gf deploy -c wordpress.
In the Client Portal's deploy dialog, set the minimum number of instances to 1, so the site doesn't have to start up on its first visit.
5. Check that it worked
gf coderunner status -c wordpressOpen the Deployment URL it prints: the first visit runs the WordPress installer. Complete the installer as soon as the first deploy finishes. Until you do, anyone who opens the URL can set up the site and make themselves its admin. If the deployment shows Failed, read the log:
gf coderunner logs -c wordpressThe usual causes are WordPress not listening on 8080, or a wrong database host, user or password; the log shows which. A start script that stops with "couldn't read WORDPRESS_DB_PASSWORD" means the password isn't stored in the Coderunner's environment, or the attached client can't read secrets: check step 1 and step 3, then deploy again.
Change it later
- CPU or memory: deploy again with new
--cpuand--memoryvalues, or use the deploy dialog. - Number of instances: the Client Portal's deploy dialog or the API. The CLI has no flag for this yet.
- Database password: change
WORDPRESS_DB_PASSWORDin Secrets, then deploy again (gf deploy -c wordpress) to restart the site; the running site keeps the old password until it restarts.
Next steps
- Services — everything a Service needs.
- Configuration and secrets — settings, secrets and resources.