How to use docker context update command to modify context configuration

DockerDockerBeginner
Practice Now

Introduction

In this lab, we will explore how to modify the configuration of existing Docker contexts using the docker context update command. We will begin by creating a new Docker context to work with.

Following the creation of the context, we will demonstrate how to update its description and change the Docker endpoint it connects to. Finally, we will verify that the context configuration has been successfully updated. This hands-on exercise will provide practical experience in managing and modifying Docker contexts for connecting to different Docker daemons.


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/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/SystemManagementGroup -.-> docker/system("Manage Docker") subgraph Lab Skills docker/ls -.-> lab-555136{{"How to use docker context update command to modify context configuration"}} docker/inspect -.-> lab-555136{{"How to use docker context update command to modify context configuration"}} docker/create -.-> lab-555136{{"How to use docker context update command to modify context configuration"}} docker/system -.-> lab-555136{{"How to use docker context update command to modify context configuration"}} 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. However, you can create new contexts to connect to remote Docker daemons or other container orchestration platforms.

To create a new Docker context, we use the docker context create command. This command requires a name for the new context and the endpoint of the Docker daemon it should connect to. For this lab, we will create a context named my-context that connects to the local Docker daemon.

Open your terminal and run the following command:

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

This command creates a new context named my-context. The --docker host=unix:///var/run/docker.sock option specifies that this context should connect to the Docker daemon via the Unix socket at /var/run/docker.sock. This is the default location for the Docker socket on most Linux systems.

After running the command, you should see output similar to this, indicating that the context was created successfully:

my-context
Successfully created context "my-context"

Now that we have created the context, we can verify its existence by listing the available contexts. Use the docker context ls command:

docker context ls

You should see a list of contexts, including the default context and the newly created my-context. The output will look something like this:

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

The asterisk (*) next to default indicates that it is currently the active context. We will learn how to switch contexts in a later step.

Update the description of the context

In this step, we will update the description of the Docker context we created in the previous step. Adding a description to a context can be helpful for remembering what the context is used for, especially when you have multiple contexts.

To update a Docker context, we use the docker context update command. This command requires the name of the context you want to update and the options you want to change. We will update the my-context context and add a description to it.

Open your terminal and run the following command:

docker context update my-context --description "My local Docker context"

This command updates the my-context context and sets its description to "My local Docker context".

After running the command, you should see output similar to this, confirming the update:

my-context
Successfully updated context "my-context"

Now, let's verify that the description has been updated. We can use the docker context ls command again to list the contexts and see their descriptions:

docker context ls

You should now see the description "My local Docker context" associated with the my-context. The output will look like this:

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

Adding descriptions is a good practice for organizing your Docker contexts.

Update the Docker endpoint of the context

In this step, we will update the Docker endpoint associated with our my-context. While in a real-world scenario you might update the endpoint to point to a remote Docker daemon, for this lab, we will demonstrate the process by changing the endpoint to a slightly different format that still points to the local daemon. This shows how you would modify the connection details if needed.

To update the Docker endpoint, we use the docker context update command again, but this time we specify the --docker host option with the new endpoint. We will change the endpoint from unix:///var/run/docker.sock to unix://var/run/docker.sock (removing the extra slash after unix:).

Open your terminal and run the following command:

docker context update my-context --docker host=unix://var/run/docker.sock

This command updates the my-context context and changes its Docker endpoint.

After running the command, you should see output similar to this, confirming the update:

my-context
Successfully updated context "my-context"

Now, let's verify that the Docker endpoint has been updated. We can use the docker context ls command again to list the contexts and see their endpoints:

docker context ls

You should now see the updated Docker endpoint unix://var/run/docker.sock associated with the my-context. The output will look like this:

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

Updating the endpoint is crucial when you need to change how your Docker context connects to the Docker daemon, such as switching from a local connection to a remote one.

Verify the updated context configuration

In this final step, we will perform a comprehensive verification of the my-context to ensure that both the description and the Docker endpoint have been updated correctly. We will use the docker context inspect command, which provides detailed information about a specific context.

Open your terminal and run the following command:

docker context inspect my-context

This command will output a JSON document containing the configuration details of the my-context. We are looking for the Description and Docker.Endpoint fields within this output.

The output should look similar to this (the exact output might vary slightly depending on your Docker version and environment):

[
  {
    "Name": "my-context",
    "Metadata": {
      "Description": "My local Docker context"
    },
    "Endpoints": {
      "docker": {
        "Host": "unix://var/run/docker.sock"
      }
    },
    "TLSMaterial": {},
    "Storage": "my-context"
  }
]

Examine the output to confirm that the Description field is "My local Docker context" and the Docker.Endpoint field is "unix://var/run/docker.sock". This confirms that the updates we made in the previous steps were successful.

This step concludes our lab on creating and updating Docker contexts. You have learned how to create a new context, add a description, and modify its Docker endpoint. Understanding Docker contexts is essential for managing connections to different Docker environments.

Summary

In this lab, we learned how to create a new Docker context using the docker context create command, specifying a name and the Docker endpoint. We then verified the creation of the new context by listing all available contexts with docker context ls.

Following the creation, we explored how to modify the configuration of an existing context using the docker context update command. Specifically, we updated the description and the Docker endpoint of the newly created context. Finally, we confirmed that these updates were successfully applied by again listing the contexts and observing the changes in the output.