The CUE Central Registry provides a well-known location for well-known schemas, including those for compose files used by Docker Compose and podman-compose. This guide shows you how to get started defining your compose files in CUE using a curated module from the schema library.

The latest pre-release of the cue command is required – please upgrade to this version if it’s not already installed:

TERMINAL
$ cue version
cue version v0.13.0-alpha.3
...

Login to the Central Registry

TERMINAL
$ cue login # only during beta

The Central Registry requires authentication while it’s in beta testing, so you need to login before you can use its schemas.

Initialise your local CUE module

CUE that uses schemas and modules from the Central Registry needs to exist within its own CUE module.

TERMINAL
$ cue mod init cue.example

You can choose any module name you like - it’s easy to change it later. It makes sense for your CUE module to exist at the root of a source code repository, but the commands in this guide will work in any setup.

Create a compose file

Declare a compose file in CUE. This one is based on a PostgreSQL example from docker/awesome-compose:

compose.cue
// filepath: compose.cue

package dev

import "test.cue.works/x1/dockercompose"

files: example: dockercompose.#Schema & {
	services: {
		postgres: {
			container_name: "postgres"
			image:          "postgres:latest"
			environment: [
				"POSTGRES_USER=${POSTGRES_USER}",
				"POSTGRES_PASSWORD=${POSTGRES_PW}",
				"POSTGRES_DB=${POSTGRES_DB}",
			]
			ports: ["5432:5432"]
			restart: "always"
		}
		pgadmin: {
			container_name: "pgadmin"
			image:          "dpage/pgadmin4:latest"
			environment: [
				"PGADMIN_DEFAULT_EMAIL=${PGADMIN_MAIL}",
				"PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PW}",
			]
			ports: ["5050:80"]
			restart: "always"
		}
	}
}

In later guides we’ll add more entries to the files struct.

The import at the top references the appropriate curated module for the file. Its path is currently temporary, but only while its proper location is being decided. The temporary path isn’t a problem because one important property of the Central Registry is that, once a schema is published, it will always be available at that location. When the curated module’s location is finalised and versions are published under the new path, you can use the cue refactor imports command to update your CUE easily, so it reflects the new location.

Tidy your local CUE module

TERMINAL
$ cue mod tidy

Tidying a module is an important part of using curated modules from the Central Registry. Always use cue mod tidy when you use a curated module for the first time.

Validate your compose file

TERMINAL
$ cue vet -c

Because cue vet doesn’t display any errors, you know that the curated schema has validated your compose file.

Export your compose file as YAML

TERMINAL
$ cue export --outfile compose.yaml -e files.example

If you chose to export the files.example shown above, your validated YAML file will look like this:

compose.yaml
# filepath: compose.yaml

services:
  postgres:
    container_name: postgres
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PW}
      - POSTGRES_DB=${POSTGRES_DB}
    image: postgres:latest
    ports:
      - "5432:5432"
    restart: always
  pgadmin:
    container_name: pgadmin
    environment:
      - PGADMIN_DEFAULT_EMAIL=${PGADMIN_MAIL}
      - PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PW}
    image: dpage/pgadmin4:latest
    ports:
      - "5050:80"
    restart: always

Run your compose file

Start the compose-based services by using the docker compose up or podman compose up commands. (This example requires a couple of environment variables to be specified beforehand.)

The cue.mod directory should be stored in your source code repository, along with your compose.cue and compose.yaml files. Whenever you update your CUE file, re-run the cue export command shown above, and then record any changes to these files and directories.