How to use docker context rm command to remove contexts

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage Docker contexts, specifically focusing on their removal. You will begin by creating a new Docker context to understand the process of adding contexts. Then, you will learn how to list existing contexts to view available configurations and identify the current active context. The core of the lab will involve using the docker context rm command to remove a specific context. Finally, you will explore the behavior of attempting to remove the currently active context without force and then successfully remove it using the force option.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/SystemManagementGroup -.-> docker/system("Manage Docker") subgraph Lab Skills docker/ls -.-> lab-555134{{"How to use docker context rm command to remove contexts"}} docker/rm -.-> lab-555134{{"How to use docker context rm command to remove contexts"}} docker/create -.-> lab-555134{{"How to use docker context rm command to remove contexts"}} docker/system -.-> lab-555134{{"How to use docker context rm command to remove contexts"}} end

Create a new Docker context

In this step, we will learn how to create a new Docker context. A Docker context is a way to manage connections to different Docker daemons. By default, Docker uses the default context, which connects to the local Docker daemon. You can create new contexts to connect to remote Docker daemons or other orchestration tools.

To create a new Docker context, we use the docker context create command. The basic syntax is docker context create <context_name>. Let's create a new context named my-context.

docker context create my-context

After running the command, you should see output indicating that the context was created successfully.

my-context
Successfully created context "my-context"

This command creates a new context configuration file in your Docker configuration directory. This context currently points to the same local Docker daemon as the default context. In later steps, we will explore how to configure contexts to connect to different Docker environments.

List existing Docker contexts

In this step, we will learn how to list the existing Docker contexts. This is useful to see which contexts are available and which one is currently active.

To list the Docker contexts, we use the docker context ls command.

docker context ls

After running the command, you should see a table listing the available contexts. The output will include the context name, description, endpoint, and whether it is the current context. You should see the default context and the my-context we created in the previous step. The * next to a context name indicates that it is the currently active context.

NAME                DESCRIPTION                               DOCKER ENDPOINT                  KUBERNETES ENDPOINT   ORCHESTRATOR
default *           Current DOCKER_HOST environment           unix:///var/run/docker.sock
my-context          Docker context "my-context"               unix:///var/run/docker.sock

This command helps you keep track of the different Docker environments you can connect to.

Remove a specific Docker context

In this step, we will learn how to remove a specific Docker context. This is useful for cleaning up contexts that are no longer needed.

To remove a Docker context, we use the docker context rm command followed by the name of the context you want to remove. Let's remove the my-context we created in the previous steps.

docker context rm my-context

After running the command, you should see output confirming the removal of the context.

my-context

Now, let's list the contexts again to confirm that my-context has been removed.

docker context ls

The output should now only show the default context.

NAME                DESCRIPTION                               DOCKER ENDPOINT                  KUBERNETES ENDPOINT   ORCHESTRATOR
default *           Current DOCKER_HOST environment           unix:///var/run/docker.sock

This demonstrates how to remove a specific Docker context.

Attempt to remove the current Docker context without force

In this step, we will attempt to remove the currently active Docker context without using the force option. This will demonstrate Docker's behavior when trying to remove a context that is currently in use.

First, let's confirm the current context.

docker context ls

You should see that the default context is marked with an asterisk (*), indicating it is the current context.

Now, let's try to remove the default context using the docker context rm command.

docker context rm default

You will likely see an error message similar to this:

Error: context "default" is currently in use

This error occurs because Docker prevents you from removing the context that is currently active to avoid disrupting your current Docker operations. To remove the current context, you need to use the force option, which we will cover in the next step.

Force remove the current Docker context

In this step, we will learn how to force remove the currently active Docker context. As we saw in the previous step, you cannot remove the current context without using the force option.

To force remove the current Docker context, we use the docker context rm command with the -f or --force flag, followed by the name of the context. Let's force remove the default context.

docker context rm -f default

After running the command, you should see output confirming the removal of the context.

default

Now, let's list the contexts again to confirm that the default context has been removed.

docker context ls

You will likely see a message indicating that no contexts are available, or that Docker has switched to a different default context if one exists. In this environment, removing the default context will result in an error when trying to list contexts afterwards, as there is no active context.

Error: No contexts available. Create a context, or use --help for more information.

This demonstrates how to force remove the current Docker context.

Summary

In this lab, we learned how to manage Docker contexts. We started by creating a new context named my-context using the docker context create command, which allows us to define connections to different Docker daemons. We then used the docker context ls command to list the available contexts, observing the newly created my-context alongside the default context and identifying the currently active context marked with an asterisk.

The lab continues by demonstrating how to remove a specific Docker context using docker context rm. We will also explore the behavior of attempting to remove the currently active context without the force option and finally learn how to force remove the current Docker context.