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.
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.
First, list all contexts to see which one is active:
docker context lsThe currently active context is marked with an asterisk (*). For example:
NAME DESCRIPTION DOCKER ENDPOINT
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock
my-context * My custom context tcp://remote-host:2376If the context you want to remove has the asterisk, you need to switch away from it first.
Use docker context use to switch to the default context or another available context:
# 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:
docker context lsThe asterisk should now be next to the context you switched to, not the one you want to remove.
Now that you've switched away from the context, you can remove it:
docker context rm <context-name>For example:
docker context rm my-old-contextYou can also remove multiple contexts at once:
docker context rm context1 context2 context3If you want to remove the currently active context without switching first, use the --force (or -f) flag:
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.
If switching contexts doesn't work, an environment variable might be overriding your selection:
# Check if DOCKER_CONTEXT is set
echo $DOCKER_CONTEXT
# Check all Docker-related environment variables
env | grep DOCKERIf DOCKER_CONTEXT is set, unset it:
# 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.
If the issue persists, check Docker's configuration file:
# View Docker config
cat ~/.docker/config.jsonLook for a currentContext field. If it references the context you're trying to remove, you can edit the file to change it:
# Edit the config file
nano ~/.docker/config.json
# or
vim ~/.docker/config.jsonChange the currentContext value to "default" or remove the field entirely, then save the file.
After editing, verify with:
docker context ls
docker context rm <context-name>### 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.
# 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:
#!/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.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec