How to use docker context use command to switch contexts

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage and switch between different Docker contexts using the docker context use command. Docker contexts allow you to easily connect to various Docker daemons, whether local or remote.

You will begin by listing the available Docker contexts to understand your current setup. Then, you will create a new Docker context, even if it points to the same local daemon, to practice the creation process. Finally, you will learn how to switch to the newly created context and then switch back to the default context, demonstrating the core functionality of docker context use.


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/create("Create Container") docker/SystemManagementGroup -.-> docker/system("Manage Docker") subgraph Lab Skills docker/ls -.-> lab-555137{{"How to use docker context use command to switch contexts"}} docker/create -.-> lab-555137{{"How to use docker context use command to switch contexts"}} docker/system -.-> lab-555137{{"How to use docker context use command to switch contexts"}} end

List available Docker contexts

In this step, we will learn how to list the available Docker contexts. A Docker context is a way to connect to different Docker daemons. By default, Docker connects to the local daemon. However, you can create contexts to connect to remote daemons or other environments.

To list the available Docker contexts, you can use the docker context ls command. This command will show you a table with the name, description, Docker endpoint, and orchestrator for each context. The asterisk (*) next to a context name indicates the currently active context.

Let's list the available Docker contexts in your environment.

docker context ls

You should see output similar to this:

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

This output shows that you currently have one context named default, which is the active context and connects to the local Docker daemon via the Unix socket /var/run/docker.sock.

Create a new Docker context

In this step, we will create a new Docker context. While we won't be connecting to a remote daemon in this lab, creating a context is a fundamental skill. We will create a context that points to the same local Docker daemon as the default context, but with a different name. This will help us understand the process of creating and switching contexts.

To create a new Docker context, you use the docker context create command followed by the name you want to give the new context. You also need to specify the endpoint for the Docker daemon. For this example, we will create a context named my-local-context that points to the local Docker daemon using the Unix socket /var/run/docker.sock.

Let's create the new context:

docker context create my-local-context --docker "host=unix:///var/run/docker.sock"

You should see output confirming the creation of the context:

my-local-context
Successfully created context "my-local-context"

Now, let's list the contexts again to see the newly created one.

docker context ls

The output will now show both the default context and your new my-local-context.

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

Notice that the default context is still marked with an asterisk (*), indicating it is the currently active context.

Use the new Docker context

In this step, we will switch to using the new Docker context we created in the previous step. Switching contexts allows you to direct your Docker commands to a different Docker daemon.

To switch to a different Docker context, you use the docker context use command followed by the name of the context you want to use.

Let's switch to the my-local-context we created:

docker context use my-local-context

You should see output confirming that the context has been set:

Current context is now "my-local-context"

Now, let's list the contexts again to see which one is active.

docker context ls

The output will now show that my-local-context is the active context, indicated by the asterisk (*).

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

Even though both contexts point to the same local Docker daemon in this case, you have successfully switched the context that your Docker commands will use.

Switch back to the default context

In this final step, we will switch back to the default Docker context. This is a common operation when you are done working with a specific context and want to return to your usual Docker environment.

To switch back to the default context, you use the same docker context use command, but this time you specify default as the context name.

Let's switch back to the default context:

docker context use default

You should see output confirming that the context has been set back to default:

Current context is now "default"

Finally, let's list the contexts one last time to confirm that the default context is now active.

docker context ls

The output will show the asterisk (*) next to the default context again.

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

You have successfully listed, created, used, and switched back between Docker contexts. This fundamental understanding is crucial when working with different Docker environments.

Summary

In this lab, we learned how to manage Docker contexts. We started by listing the available contexts using docker context ls, which showed us the default context and its connection details. We then practiced creating a new context named my-local-context using docker context create, demonstrating the process of defining a new connection point, even if it was to the same local daemon for this exercise.

Following the creation of the new context, we would typically learn how to switch to this new context using the docker context use command, making it the active context for subsequent Docker commands. Finally, we would practice switching back to the original default context, solidifying our understanding of how to navigate between different Docker environments using contexts.