Deploy Your First Worker
Run a small HTTP application locally, create a Worker with the CLI, and verify an update.
Create a Worker that returns a greeting over HTTP. Run the application locally, deploy the application to an enabled tenant, and update the greeting without rebuilding the image.
Before You Start
You need:
- A tenant with Workers enabled and permission to create, read, update, and delete Workers, push images, and inspect deployments.
- Node.js 22 or later and Contextual CLI 0.17.0 or later.
- Docker with a running engine and support for building and running
linux/amd64images. - A terminal with
curl.
The example uses Worker ID hello-worker and CLI configuration ID my-config. Choose a different Worker ID if hello-worker is already in use. Replace <tenant-id> below with your tenant ID.
The sample has no application authentication and returns only a greeting. Keep the example application free of private data.
1. Check the CLI and Connect to Your Tenant
Install or update the Contextual CLI. The CLI's ctxl command and docker-credential-ctxl helper must be on PATH.
npm install -g @contextual-io/cli
ctxl --version
ctxl workers --help
The npm install command adds or updates the CLI in your global npm installation. Check that ctxl workers --help lists configure-docker, create, and update. If the CLI reports Command workers not found, the installed CLI does not include Worker commands. See Troubleshooting.
Add a CLI configuration for the tenant, select the configuration, and sign in. To reuse an existing configuration for the tenant, find the configuration ID with ctxl config list. Skip config add and replace my-config with the existing configuration ID throughout the quickstart.
ctxl config add my-config --tenant-id <tenant-id>
ctxl config use my-config
ctxl config login
ctxl config current --json
Follow the CLI sign-in prompt. Check that ctxl config current --json reports your intended tenant. The config use command changes the default configuration on your machine. The example Worker commands also pass --config-id explicitly.
Configure Docker to use your CLI login for the Worker image hosts:
ctxl workers configure-docker --pretty
The workers configure-docker command updates Docker's credential-helper configuration for the organization and platform image hosts of every saved CLI configuration. Rerun workers configure-docker after adding a CLI configuration for another tenant.
2. Create the Application Files
Create a new example directory. Save the following server.mjs and Dockerfile in the example directory.
import { createServer } from "node:http";
const port = Number(process.env.PORT ?? 8080);
const server = createServer((request, response) => {
response.setHeader("Content-Type", "application/json");
if (request.method !== "GET") {
response.writeHead(405, { Allow: "GET" });
response.end(JSON.stringify({ error: "Method not allowed" }));
return;
}
const path = new URL(request.url, "http://localhost").pathname;
if (path === "/health") {
response.end(JSON.stringify({ status: "ok" }));
return;
}
if (path !== "/") {
response.statusCode = 404;
response.end(JSON.stringify({ error: "Not found" }));
return;
}
response.end(JSON.stringify({ greeting: process.env.GREETING ?? "Hello" }));
console.info("Handled GET /");
});
server.listen(port, "0.0.0.0", () => console.info(`Listening on port ${port}`));
process.on("SIGTERM", () => server.close(() => process.exit(0)));
FROM node:22-alpine
WORKDIR /app
COPY --chown=node:node server.mjs ./
USER node
ENV PORT=8080
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
CMD node --input-type=module -e "const response = await fetch('http://127.0.0.1:' + process.env.PORT + '/health'); process.exit(response.ok ? 0 : 1)"
CMD ["node", "server.mjs"]
The application listens on all interfaces at PORT. The platform sets PORT to the public port and replaces any PORT you set. Use a port from 1024 to 65535. The Dockerfile declares the HTTP port and health check required for a public Worker. Locally, the application runs as the image's unprivileged node user. On Contextual, it runs as user ID 10001 with a read-only root filesystem. The example writes no files, so it runs unchanged. See Runtime Environment.
3. Check the Container Locally
From the directory containing server.mjs and Dockerfile, build the image and start a local container. The port mapping exposes the container port only on your machine's loopback interface.
docker build --platform linux/amd64 -t contextual-worker-hello:local .
docker run --platform linux/amd64 --rm -d \
--name contextual-worker-hello \
-p 127.0.0.1:8080:8080 \
-e "GREETING=Hello from Contextual" \
contextual-worker-hello:local
Check the application:
curl --fail http://127.0.0.1:8080/health
curl --fail http://127.0.0.1:8080/
docker logs contextual-worker-hello
Expect {"status":"ok"} from /health, {"greeting":"Hello from Contextual"} from /, and Handled GET / in the local container logs.
The application can take a moment to start. If curl fails right after docker run, for example with Empty reply from server, wait until docker logs contextual-worker-hello shows Listening on port 8080, then repeat the requests.
Stop the local container after the health response, greeting response, and log checks pass. The --rm flag causes Docker to remove the container after the container stops.
docker stop contextual-worker-hello
4. Create the Worker
Run workers create from the directory containing server.mjs and Dockerfile:
ctxl workers create hello-worker \
--size small \
--build . \
--env "GREETING=Hello from Contextual" \
--config-id my-config \
--pretty
The workers create command builds and pushes a linux/amd64 image to your organization's image host, then creates the Worker configuration. The response shows the image as images.<org>.my.contextual.io/hello-worker:<tag>@sha256:…, where <tag> is build- followed by a UTC timestamp. Saving the Worker starts deployment. Record the Worker ID, configuration version, and image reference from the CLI response for deployment verification.
5. Verify the Deployment
In Console V2, select the target tenant, open Workers, select hello-worker, and open Deployment. Inspect container readiness in Deployment and copy the Public Worker URL.
Read the saved configuration and inspect deployment instances from the CLI:
ctxl records get native-object:worker/hello-worker --config-id my-config --pretty
ctxl agents instances hello-worker --config-id my-config --pretty
The Worker record shows environment variable values as <ENCRYPTED>. Use the Worker ID with ctxl agents instances. Expect at least one instance with status Running. Update the configuration with ctxl workers commands.
Replace <worker-url> with the URL copied from the console, without a trailing slash:
curl --fail <worker-url>/health
curl --fail <worker-url>/
Expect the same two JSON responses as the local checks. A Running label does not mean the app is ready. Test the URL. If the requests fail, use Troubleshooting.
Read the application's output, as you did with docker logs locally:
ctxl workers logs hello-worker --config-id my-config
Expect Listening on port 8080 and one Handled GET / line for each greeting request. Each line starts with [<pod>/worker]. See Read Logs for more options.
6. Deploy a Configuration Change
Update the greeting:
ctxl workers update hello-worker \
--env "GREETING=Updated worker" \
--config-id my-config \
--pretty
The workers update command saves a new Worker version and requests deployment of the new configuration. The update reuses the current image and preserves existing Worker settings except GREETING. You may get the old greeting during rollout. Wait for the rollout to finish, then request / again. Expect {"greeting":"Updated worker"}.
To deploy application code changes later, use workers update hello-worker --build .. See Workers CLI Reference.
7. Remove the Test Worker
When you have finished, delete hello-worker:
ctxl records delete native-object:worker/hello-worker --config-id my-config
The records delete command sends the delete request immediately, without a confirmation prompt. Deleting the Worker also requests removal of the Worker's deployment. Confirm that hello-worker disappears from Workers. Then check the deployment:
ctxl agents instances hello-worker --config-id my-config --pretty
After the deployment is removed, the agents instances command fails with Agent hello-worker not found. Deleting the Worker does not delete local source files or container images.
Use This Guide with a Coding Agent
Give your coding agent the following task. Replace <tenant-id> and adjust the Worker and CLI configuration IDs if needed:
Read the Contextual Workers quickstart and CLI guide. Target tenant
<tenant-id> using CLI configuration my-config and Worker ID hello-worker.
Use the two-file HTTP example. Check the local container first, then create
the Worker with the documented CLI. Report the saved Worker version and
image reference, deployment readiness, and actual health and greeting responses.
Verify an update to the greeting. Keep credentials out of output and chat.