Skip to main content

Publishing an Image

This page takes the code you build into a container image and runs it, with Octo building the image and pushing it to ECR as part of the apply. The example is reports, a Node service of its own that runs on ECS, separate from the shop's checkout function that Shop Stack builds. Its compiled output is in reports/dist. Your own service takes its place without changing anything else on the page.

Terraform has no resource that puts an image into a registry, so Octo drives the build and the push itself. The image is built from what is on disk when apply runs, on the machine running Octo, with Docker.

Before you start​

Two things have to be true on the machine that runs apply.

  • Docker is running. The build and the push both go through it.
  • The service's build output is in place. The Dockerfile copies files from your project folder, so a service that has to be compiled is compiled first, for example with npm run build inside reports. A missing file fails the build, and Octo publishes nothing.

The Dockerfile​

Put the Dockerfile in the folder the image is built from. That folder is the build context, so everything the image holds has to be inside it.

reports/Dockerfile
FROM public.ecr.aws/docker/library/node:22-alpine

WORKDIR /app
COPY dist ./
CMD ["node", "server.js"]

The repository​

An image has a repository in each region it is used in. AwsEcrImageModule creates them, and creates no version inside them. The repository is named <imageFamily>/<imageName>.

octo.yaml
imports:
- className: AwsEcrImageModule
importPath: "@quadnix/octo-aws-cdk/modules/image/aws-ecr-image"

modules:
- moduleId: image-module
moduleClass: AwsEcrImageModule
moduleInputs:
imageFamily: shop
imageName: reports
imageTagsToKeep: 5
regions:
- "${{region-module.model.region}}"

imageTagsToKeep is how many of the newest images each repository keeps; ECR expires the rest. It defaults to 5. Count every tag you may roll back to: an expired tag comes back only as a new build of what is on disk.

One version​

AwsEcrImageTagModule publishes one version into the repository. There is one module per version: a second version is a second module, and removing a module stops declaring that version without touching the others.

octo.yaml
imports:
- className: AwsEcrImageTagModule
importPath: "@quadnix/octo-aws-cdk/modules/image/aws-ecr-image-tag"

modules:
- moduleId: image-tag-module
moduleClass: AwsEcrImageTagModule
moduleInputs:
architecture: x86_64
dockerfileDirectory: reports
dockerfileName: Dockerfile
image: "${{image-module.model.image}}"
imageTag: "0.0.1"

dockerfileDirectory is the build context, relative to the project root, and dockerfileName is the Dockerfile in it. architecture is the processor the image is built for, x86_64 or arm64, and the execution that runs the image has to run the same one.

imageTag is the only input that decides what is published. A tag is immutable, so editing dockerfileDirectory or the Dockerfile under a tag that already exists publishes nothing: put the new code under a new tag.

When the Dockerfile needs values at build time, there are two inputs, and the difference is where they end up.

octo.yaml
      buildArguments:
NODE_ENV: production
buildSecrets:
NPM_TOKEN: NPM_TOKEN
  • buildArguments are passed as --build-arg and read by ARG in the Dockerfile. They are written into Octo's state and into the image's history, so they are never secrets.
  • buildSecrets are passed as --secret and read by RUN --mount=type=secret in the Dockerfile. Each key is the secret's id and each value is the name of an environment variable on the machine running Octo. Octo keeps only the name, and a variable that is not set fails the build before anything is published.

Running it​

The deployment names the image and the tag to run, and the execution runs the deployment. Here they run on ECS, on top of the server, environment and subnet it needs; the pieces above them are in AwsDeploymentModule and AwsEcsExecutionModule.

octo.yaml
  - moduleId: reports-deployment-module
moduleClass: AwsDeploymentModule
moduleInputs:
command: [node, server.js]
cpu: 256
deploymentTag: v1
image: "${{image-module.model.image}}"
imageTag: "0.0.1"
memory: 512
ports:
- containerPort: 8080
protocol: tcp
server: "${{reports-server-module.model.server}}"

- moduleId: reports-execution-module
moduleClass: AwsEcsExecutionModule
moduleInputs:
deployments:
main:
containerProperties:
image:
essential: true
name: reports-v1
deployment: "${{reports-deployment-module.model.deployment}}"
sidecars: []
desiredCount: 1
environment: "${{environment-module.model.environment}}"
executionId: reports-v1-qa
subnet: "${{subnet-module.model.subnet}}"

The deployment's imageTag is the tag the execution runs, and it has to be a tag some AwsEcrImageTagModule publishes or the repository already holds. The deployment never copies the image's address: Octo gives the task definition the address of the tag the same apply published, so one run can publish a version and start the code that runs it.

Deploy it​

npm run octo:generate
npm run octo:validate

npm run octo:lock
npm run octo:apply
npm run octo:commit

The apply builds the image, pushes it, and only then registers the task definition that names it. How long that takes depends on your build. A second run with nothing changed reports no change, and republishes nothing.

A new version​

A new version of the code is a new tag. Build the service again, then change imageTag to 0.0.2 in image-tag-module and in reports-deployment-module, and run the same five commands. The apply builds and pushes 0.0.2 before the service is pointed at it.

Version 0.0.1 stays in the repository. It is there to roll back to until imageTagsToKeep newer images have pushed it out. To keep both versions declared side by side, add a second module with its own id instead of editing the first:

octo.yaml
  - moduleId: image-tag-module-2
moduleClass: AwsEcrImageTagModule
moduleInputs:
architecture: x86_64
dockerfileDirectory: reports
dockerfileName: Dockerfile
image: "${{image-module.model.image}}"
imageTag: "0.0.2"

A tag from your app's version​

To stop editing imageTag by hand, read it from the version in the app's package.json. Put a file beside octo.yaml:

octo/image-tags.js
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

const packageJsonPath = fileURLToPath(new URL('../reports/package.json', import.meta.url));

export const reports = JSON.parse(readFileSync(packageJsonPath, 'utf8')).version;

Then name the export in both places that carry the tag:

octo.yaml
      imageTag: "${import ./octo/image-tags.js#reports}"

The path is read from the definition file's directory, and the export is a string, not a function. Octo loads the file as an ES module, so the project's package.json needs "type": "module", or the file needs a .mjs extension. A new version is then a bump of version and the same five commands. Octo reads the file on every command, so it reads nothing but the package.json: a tag taken from the clock or from git changes the plan every time.

Rolling back​

Change imageTag back to 0.0.1 in the tag module and in the deployment, and run the five commands again. Since the repository still holds 0.0.1, Octo takes that tag as it is: it does not build it, so the files on disk can be newer than what the tag holds, and the service runs the image that was published. A tag that has expired out of the repository is built again from what is on disk now, which may not be the code it first held.

Where it stops​

An image is built for one architecture, and the execution that runs it must run that processor. Deleting a repository, or changing imageFamily or imageName, deletes every image in it, even one a running service uses, so change the tag rather than the name.