How to fix Error: context is in use, cannot be removed in Docker

DockerBEGINNERLOWUpdated Dec 1, 2025ยท 132 views

The 'context is in use, cannot be removed' error occurs when you try to remove a Docker context that is currently active. Switch to a different context first using `docker context use`, or use the `--force` flag to remove the active context.

Quick fix

Fixes Error: context is in use, cannot be removed

docker context ls
  1. Check which context is currently active
  2. Switch to a different context
  3. Remove the context
  4. Use --force to remove the active context directly
docker โ€” error
docker context ls
Error: context is in use, cannot be removed

What this error means

The "context is in use, cannot be removed" error appears when you attempt to delete a Docker context using `docker context rm` while that context is currently active (selected). Docker contexts allow you to switch the CLI between different Docker endpoints - such as local Docker Engine, remote hosts, or cloud integrations like AWS ECS. When you run Docker commands, they execute against the currently active context. Docker prevents removal of the active context as a safety mechanism. If you deleted the context your CLI is currently using, subsequent Docker commands would fail with connection errors. The asterisk (*) in `docker context ls` output indicates which context is currently active. This error is straightforward to resolve: simply switch to a different context before removing the one you no longer need, or use the force flag to remove it anyway.

Typical symptoms

  • Running `docker context rm <context>` returns 'context is in use, cannot be removed'
  • The context you're trying to remove has an asterisk (*) in `docker context ls` output
  • Docker Desktop or CLI was configured to use a remote context that's now stale
  • Attempting to clean up contexts created for cloud deployments (ECS, ACI)
  • CI/CD pipeline fails when trying to remove temporary contexts

Common causes

  1. The context you're trying to remove is the currently active context (most common)
  2. The DOCKER_CONTEXT environment variable is set to the context you're trying to remove
  3. Docker Desktop automatically switched to a non-default context
  4. A previous `docker context use` command selected this context
  5. The context was set as default in Docker's config.json file

How to fix "Error: context is in use, cannot be removed"

1Check which context is currently active

First, list all contexts to see which one is active:

bash
docker context ls

The currently active context is marked with an asterisk (*). For example:

bash
NAME          DESCRIPTION                               DOCKER ENDPOINT
default       Current DOCKER_HOST based configuration   unix:///var/run/docker.sock
my-context *  My custom context                         tcp://remote-host:2376

If the context you want to remove has the asterisk, you need to switch away from it first.

2Switch to a different context

Use docker context use to switch to the default context or another available context:

bash
# Switch to the default context
docker context use default

# Or switch to any other context
docker context use <other-context-name>

Verify the switch was successful:

bash
docker context ls

The asterisk should now be next to the context you switched to, not the one you want to remove.

3Remove the context

Now that you've switched away from the context, you can remove it:

bash
docker context rm <context-name>

For example:

bash
docker context rm my-old-context

You can also remove multiple contexts at once:

bash
docker context rm context1 context2 context3
4Use --force to remove the active context directly

If you want to remove the currently active context without switching first, use the --force (or -f) flag:

bash
docker context rm --force <context-name>
# or
docker context rm -f <context-name>

Note: After force-removing the active context, Docker will automatically fall back to the default context. However, if you have the DOCKER_CONTEXT environment variable set, you may encounter errors until you unset it or set it to a valid context.

5Check for DOCKER_CONTEXT environment variable

If switching contexts doesn't work, an environment variable might be overriding your selection:

bash
# Check if DOCKER_CONTEXT is set
echo $DOCKER_CONTEXT

# Check all Docker-related environment variables
env | grep DOCKER

If DOCKER_CONTEXT is set, unset it:

bash
# Bash/Zsh
unset DOCKER_CONTEXT

# Windows PowerShell
Remove-Item Env:DOCKER_CONTEXT

# Windows Command Prompt
set DOCKER_CONTEXT=

After unsetting the variable, try removing the context again.

6Reset Docker context in config file (if needed)

If the issue persists, check Docker's configuration file:

bash
# View Docker config
cat ~/.docker/config.json

Look for a currentContext field. If it references the context you're trying to remove, you can edit the file to change it:

bash
# Edit the config file
nano ~/.docker/config.json
# or
vim ~/.docker/config.json

Change the currentContext value to "default" or remove the field entirely, then save the file.

After editing, verify with:

bash
docker context ls
docker context rm <context-name>

Advanced notes

### Understanding Docker Contexts

Docker contexts are stored in ~/.docker/contexts/ on Linux/macOS and %USERPROFILE%\.docker\contexts\ on Windows. Each context contains metadata about how to connect to a Docker endpoint.

The default context is special - it represents your local Docker installation and cannot be removed. It uses the DOCKER_HOST environment variable if set, otherwise falls back to the local Docker socket.

### Context Priority Order

When Docker determines which context to use, it follows this priority:

1. --context flag on the command line (highest priority)
2. DOCKER_CONTEXT environment variable
3. DOCKER_HOST environment variable (creates an implicit context)
4. currentContext in ~/.docker/config.json
5. The default context (lowest priority)

### Common Context Types

| Context Type | Created By | Use Case |
|--------------|-----------|----------|
| default | Docker installation | Local Docker Engine |
| desktop-linux | Docker Desktop | Docker Desktop on Linux |
| ECS contexts | docker context create ecs | AWS ECS deployments |
| ACI contexts | docker context create aci | Azure Container Instances |
| SSH contexts | docker context create --docker host=ssh:// | Remote Docker over SSH |

### Cleaning Up Cloud Contexts

If you created contexts for cloud deployments (ECS, ACI), remember that removing the context does not remove cloud resources. The context is just a local pointer to those endpoints.

bash
# List ECS contexts
docker context ls | grep ecs

# Remove ECS context (does NOT delete ECS resources)
docker context rm my-ecs-context

### Docker Desktop Considerations

Docker Desktop on macOS and Windows manages its own context. If you're having issues with Docker Desktop contexts:

1. Try restarting Docker Desktop
2. Reset Docker Desktop to factory defaults (Settings > Troubleshoot > Reset)
3. Check that Docker Desktop has finished starting before running context commands

### Scripting Context Management

When writing scripts that use multiple contexts, always switch back to the original context:

bash
#!/bin/bash
# Save current context
ORIGINAL_CONTEXT=$(docker context show)

# Switch to deployment context
docker context use my-deploy-context

# Run deployment commands
docker compose up -d

# Switch back to original context
docker context use $ORIGINAL_CONTEXT

### Multiple Docker Installations

If you have multiple Docker installations (e.g., Docker Desktop and a separate Docker Engine), be careful when removing contexts. The default context behavior depends on which Docker is currently running.

Frequently asked questions

What does "Error: context is in use, cannot be removed" mean?
The "context is in use, cannot be removed" error appears when you attempt to delete a Docker context using docker context rm while that context is currently active (selected). Docker contexts allow you to switch the CLI between different Docker endpoints - such as local Docker Engine, remote hosts, or cloud integrations like AWS ECS. When you run Docker commands, they execute against the currently active context.
How do I fix "Error: context is in use, cannot be removed"?
The 'context is in use, cannot be removed' error occurs when you try to remove a Docker context that is currently active. Switch to a different context first using docker context use, or use the --force flag to remove the active context.
What causes "Error: context is in use, cannot be removed"?
The context you're trying to remove is the currently active context (most common) The DOCKER_CONTEXT environment variable is set to the context you're trying to remove Docker Desktop automatically switched to a non-default context
Is the "Error: context is in use, cannot be removed" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading