Back to All Articles
Kubernetes & Cloud

Running the Dev Container Workspace Locally, Then Moving to Codespaces

By Engineering Team•October 15, 2026•6 min read

Overview

A Codespace is a convenient disposable workspace, but it draws on a monthly allowance. While the infrastructure code is still changing every few minutes, that allowance goes quickly. The same dev container file also runs on your own machine, in a container you delete when you finish. The cost is zero, and when the code settles you move to a Codespace with no change to the setup.

This article covers the local path. The file itself, the cost controls and the Codespace steps are in Setting Up a GitHub Codespace for OpenTofu and AI Coding Agents. The reasons for keeping the cloud token out of long-lived places are in Connecting OpenTofu to Cloudflare Without Long-Lived Credentials.

When to use which

Local container Codespace
Cost None Counts against the free allowance
Where the token lives In the container’s environment for one session Stored as a repository-restricted Codespaces secret
Best for The first iterations, while the code changes constantly Once the code is stable, or when several people need the same environment
Needs on your machine A container runtime, the dev container command-line tool and VS Code with the Dev Containers extension A browser or VS Code

Both read the same .devcontainer/devcontainer.json, so the tools, extensions and variable names are identical.

What you need

  • A container runtime running on your machine (Docker Desktop, or any engine the Dev Containers tool supports).
  • The Dev Containers command-line tool: npm install -g @devcontainers/cli.
  • VS Code with the Dev Containers extension, so the editor and the AI coding agent work inside the container (Step 4).
  • A clone of the workspace repository.

Step 1: Create a short-lived token

Create a custom API token limited to one account and the permissions the configuration needs, with an expiry of a few days at most. The steps and permission list are in the companion article. Create it for this session only; a token on a personal machine should be easy to throw away.

Step 2: Read the token in without leaving a trace

Type the value into a hidden prompt, so it never reaches the screen, the shell history or a file:

read -rs -p "Cloud token: " CLOUD_TOKEN; echo

Do not paste it into a command line, an .env file or the dev container file.

Step 3: Start the container

From the repository root:

devcontainer up --workspace-folder . --remote-env "SECRET_KEY_CLOUD_TOKEN=$CLOUD_TOKEN"
unset CLOUD_TOKEN

Name the variable the way your workspace rules require. Check devcontainer up --help for your version, because the flag that passes environment variables has changed between releases.

The tool builds the image, installs the features and runs the post-create script, exactly as a Codespace would.

Step 4: Work in VS Code, not a bare terminal

Do the day-to-day work inside VS Code attached to the container, rather than in a terminal on its own. The editor, the OpenTofu extension and the AI coding agent then all run in the same container, side by side, and see the same files and the same environment. A terminal alone gives you the tools but none of that.

  1. Install the Dev Containers extension in VS Code on your machine.
  2. Open the Command Palette and run Dev Containers: Attach to Running Container, then choose the container from Step 3.
  3. In the window that opens, open the workspace folder and install any extension that is missing. Check that the OpenTofu extension and the AI agent extension appear under the container’s extensions.
  4. Open the integrated terminal and the AI agent panel. Both run inside the container.

Starting the container from the command line and then attaching keeps the token out of the shared dev container file. The alternative, Dev Containers: Reopen in Container, builds from the file directly. It cannot receive a one-session token unless you add a remoteEnv entry to the file. A remoteEnv that reads a variable your machine does not have resolves to an empty value, and it can hide the Codespaces secret of the same name, so do not add it to the shared file.

A note on the AI agent. The agent runs as you, inside the container, so it can read the container’s environment and run commands, including tofu apply. Keep the token out of what it can echo: map it into one command at a time (Step 5), keep the repository’s secret-guard hook active, and read every plan before approving a change. If you run an AI agent from a terminal on your own machine, it sees your machine’s environment instead, which is why this article keeps the token inside the container.

Step 5: Check without printing

Confirm the variable arrived, without showing it:

[ -n "${SECRET_KEY_CLOUD_TOKEN:+x}" ] && echo set || echo missing
tofu --version

Map the value into the one command that needs it, rather than exporting it for the whole shell:

CLOUDFLARE_API_TOKEN="$SECRET_KEY_CLOUD_TOKEN" tofu plan

Step 6: Remove everything

This step is the one that makes the local path safe. A variable passed to a container is stored in the container’s configuration until the container is removed. It does not live only in memory, so stopping the container is not enough.

Push your changes first, then remove the container and its volumes:

docker ps -a --filter "label=devcontainer.local_folder=$(pwd)" -q | xargs docker rm -f -v

Then delete the token in the provider’s dashboard if the session is over, or if the machine is shared. If you left the container running overnight, treat the token as exposed and revoke it.

Step 7: Move to a Codespace later

When the code is stable and you want a shared, always-identical environment:

  1. Store the token as a Codespaces secret, restricted to the workspace repository.
  2. Create a Codespace from the repository.
  3. Run the same checks as in Step 5.

Nothing in the dev container file changes. Only the way the token arrives changes: from your hidden prompt to a stored secret. From there, the delivery pipeline takes over the applies once the code is stable.

Pitfalls

  • Saving the value. A token typed into the wrong place (a script, a note, a chat) is stored there. Use the hidden prompt every time.
  • Leaving the container running. It keeps the token and your files. Remove it at the end of every session.
  • Reusing the token. Create a new one per session, or at least per few days. A spare kept “just in case” is a standing credential.
  • Starting an AI agent from a shell that holds the token. The agent can read the environment. Keep the token inside the container and map it per command, as above.

Summary

Run the same dev container locally while the code is moving, pass the cloud token in through a hidden prompt, and remove the container and its volumes afterwards. It costs nothing and leaves no tools or token on the machine. When the setup is stable, move to a Codespace using the same file.