Claude Code Login in a Dev Container on a Headless Server

aidockersecurityself-hosting
Claude Code Login in a Dev Container on a Headless Server

Claude Code can't open a browser on a server. It prints a sign-in URL, you open it on your laptop or phone, and the page shows a code to paste back into the terminal. To keep the login after a dev container rebuild, store ~/.claude in a Docker volume and set CLAUDE_CONFIG_DIR to that path.

I tested this on November 20, 2026, on a 2 vCPU / 8 GB Ubuntu 24.04 VPS with Docker 29.10 and Claude Code 2.1.329 in the container. I completed a fresh login on November 22 with Claude Code 2.1.330. The other logged-in results come from a container I signed in to on November 19. I didn't test claude setup-token, API key logins, or third-party platforms like Bedrock.

How Do You Log In to Claude Code Inside a Dev Container?

Start Claude Code from the repo folder on the host (the Dev Containers CLI guide covers the setup):

devcontainer exec --workspace-folder . claude

On a fresh volume, it asks for a text style and then a login method. Pick option 1 for a Pro or Max subscription, and it prints a URL instead of opening a browser:

Terminal output of claude in the dev container. The Select login method menu offers a Claude subscription, an Anthropic Console account, or a third-party platform. After picking option 1, Claude Code shows Browser didn't open? Use the URL below to sign in, a claude.com sign-in URL, and Paste code here if prompted.

Open that URL on your laptop or phone and sign in. Its redirect_uri (where the browser goes after sign-in) points at platform.claude.com, not at localhost, so the browser lands on a page that shows a code:

The browser page after sign-in. It says Authentication code, Paste this into Claude Code, then a blurred code and a Copy code button.

Paste the code into the terminal, and Claude Code writes the token to .credentials.json in its config folder.

A sequence of steps. Claude Code in the container prints a sign-in URL. You open that URL on a laptop or phone and sign in at claude.com. The browser is redirected to platform.claude.com, which shows a code. You paste the code back into the terminal, and Claude Code writes the token to .credentials.json in the claude-config volume.

The terminal confirms the login, then asks whether you trust the folder:

Terminal output in the container. Welcome to Claude Code v2.1.330, logged in with the account redacted, Login successful, Press Enter to continue. Then it asks whether to trust /workspace, with No, exit selected and Yes, I trust this folder below it.

On my fresh volume, the first start also printed Claude configuration file not found with a command to restore a backup. I ignored it, and the setup continued.

Nothing connects to the server from outside, so you don't need port forwarding or an SSH tunnel. From a shell in the container, claude auth login runs the same flow without the menus.

The one-year token from claude setup-token can't start Remote Control sessions, according to the authentication docs, so this guide uses the pasted code.

How Do You Check If Claude Code Is Logged In?

Run this in the container:

claude auth status --text

It exits with code 1 when you're logged out, and it shows when the sign-in ends when you're logged in:

Terminal output of claude auth status in the container. On a new, empty volume it prints Not logged in and exits with code 1. On the volume signed in the day before it prints Login method: Claude Max account and Sign-in ends: between 2026-12-14 and 2026-12-17, and exits with code 0.

The docs say that using Claude Code doesn't extend the login, so check this date on long-running servers.

Why Does Claude Code Lose Its Login After a Rebuild?

A rebuild replaces the container, and everything written inside it goes with the old one. Claude Code keeps its state in the ~/.claude folder and in a ~/.claude.json file next to it, so both need to live outside the container.

The dev container from the CLI guide does that with a volume and one variable in devcontainer.json:

  "mounts": [
    "source=claude-config-${devcontainerId},target=/home/claude/.claude,type=volume",
    "source=${localWorkspaceFolder}/.devcontainer,target=/workspace/.devcontainer,type=bind,readonly"
  ],
  "containerEnv": {
    "CLAUDE_CONFIG_DIR": "/home/claude/.claude",
    "DISABLE_AUTOUPDATER": "1"
  },

${devcontainerId} stays the same for the same repo folder, so a rebuilt container gets the same volume back. CLAUDE_CONFIG_DIR makes Claude Code keep .claude.json in that volume too.

To see what the variable does, I mounted my logged-in volume in a container without it. claude auth status still said I was logged in, because the token lives in the volume. But an interactive claude opened the first-run text style picker again. The .claude.json file had stayed in the home folder, where the next rebuild throws it away.

What Happens When the Volume Has the Wrong Owner?

Claude Code says you're not logged in, even with the volume mounted. This happens when the volume's files belong to a different UID than the container user, for example after a Dockerfile change gave claude a new UID.

I reproduced it with a volume owned by UID 1001, mounted for claude with UID 1000:

Terminal output in the container. The claude user has UID 1000, but /home/claude/.claude is owned by 1001. claude auth status prints Not logged in and exits with code 1, and touching a file in /home/claude/.claude fails with Permission denied.

Give the folder back to claude as root, using the container name from docker ps:

docker exec -u root <container> chown -R claude:claude /home/claude/.claude

My test volume held no login, so I couldn't check whether chown restores one. If it doesn't, remove the volume and sign in again. That also deletes the settings and session history stored in it.

Which Hosts Does Claude Code Need for Login Behind a Firewall?

The network requirements list claude.ai, claude.com, platform.claude.com, and sso.claude.com for sign-in, next to api.anthropic.com. On November 20, 2026, all five resolved to 160.79.104.10.

So Anthropic's reference firewall, which only names api.anthropic.com, let the sign-in hosts through by accident:

Terminal output in a container running Anthropic's reference firewall, whose allowlist names api.anthropic.com but not the sign-in hosts. curl gets HTTP 403 from claude.ai, 200 from platform.claude.com, and 404 from api.anthropic.com, so all three connections went through.

Any status code means the connection went through. That overlap can change, so allow each host by name. The dev container firewall guide names claude.ai, claude.com, and platform.claude.com, but not sso.claude.com.

The firewall only limits where traffic can go. .credentials.json belongs to claude, the user the agent runs as. The dev container docs say a dev container doesn't stop a malicious project from sending the credentials in ~/.claude to an allowed host. Only point the agent at repos you trust.

Summary

Claude Code on a server signs in without a browser and without an open port. You open the URL it prints on another device and paste the code back. A named volume at ~/.claude plus CLAUDE_CONFIG_DIR keeps that login across rebuilds, as long as the volume's owner matches the container user.

My self-hosting course covers setting up the Ubuntu server that runs these containers.

Thanks for reading.

And stay awesome!


Frequently Asked Questions

How do you log in to Claude Code on a headless server?

Run claude or claude auth login on the server. It prints a sign-in URL. Open it on a laptop or phone and sign in, and the page at platform.claude.com shows a code. Paste that code back into the terminal. No browser on the server and no port forwarding are needed.

Why does Claude Code lose its login after a rebuild?

A rebuild replaces the container and everything written inside it. Mount a named volume at ~/.claude and set CLAUDE_CONFIG_DIR to the same path. Without the variable, the credentials survive in the volume, but the .claude.json settings file stays in the home folder and the first-run setup repeats.

How do you check if Claude Code is logged in?

Run claude auth status with the text option. It prints the login method and when the sign-in ends, or a Not logged in message with exit code 1. Using Claude Code does not extend the login.

Can Claude Code read its own login token in a dev container?

Yes. The credentials file belongs to the same user the agent runs as. The Claude Code docs warn that a dev container does not stop a malicious project from sending anything inside it, including those credentials, to an allowed host.

Which hosts does Claude Code need for login behind a firewall?

Besides api.anthropic.com, the docs list claude.ai, claude.com, platform.claude.com, and sso.claude.com for sign-in. On November 20, 2026, all five resolved to the same IP address, but that can change, so allow each one by name.

Browse all Self-Hosting guides →
Loading comments...

Whenever you're ready, there are 5 ways I can help you:

  1. Pragmatic Self-Hosting: Learn to deploy and run your apps on a VPS with Dokploy and Tailscale. Keep administration private, automate releases, and practice restoring backups, with source code and a runbook you can reuse.
  2. Pragmatic Clean Architecture: Join 5,000+ students in this comprehensive course that will teach you the system I use to ship production-ready applications using Clean Architecture. Learn how to apply the best practices of modern software architecture.
  3. Modular Monolith Architecture: Join 2,800+ engineers in this in-depth course that will transform the way you build modern systems. You will learn the best practices for applying the Modular Monolith architecture in a real-world scenario.
  4. Pragmatic REST APIs: Join 1,900+ students in this course that will teach you how to build production-ready REST APIs using the latest ASP.NET Core features and best practices. It includes a fully functional UI application that we'll integrate with the REST API.
  5. Patreon Community: Join a community of 5,000+ engineers and software architects. You will also unlock access to the source code I use in my YouTube videos, early access to future videos, and exclusive discounts for my courses.

The .NET Weekly

One practical .NET tip, every Saturday.

Join 66,000+ engineers who read it. Free, and you can unsubscribe any time.