How to use docker context create command to manage Docker endpoints

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage different Docker endpoints using the docker context create command. You will explore how to create a new context that points to your local Docker daemon, allowing you to explicitly define and switch between connections.

Furthermore, you will discover how to streamline context creation by leveraging the configuration of existing contexts. This includes creating a new context by copying from an existing one and creating a context that utilizes the Docker endpoint configuration from another context, providing flexibility and efficiency in managing your Docker environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/images("List Images") subgraph Lab Skills docker/ls -.-> lab-555129{{"How to use docker context create command to manage Docker endpoints"}} docker/inspect -.-> lab-555129{{"How to use docker context create command to manage Docker endpoints"}} docker/create -.-> lab-555129{{"How to use docker context create command to manage Docker endpoints"}} docker/images -.-> lab-555129{{"How to use docker context create command to manage Docker endpoints"}} end

Create a context with a local Docker endpoint

In this step, you will learn how to create a Docker context that points to your local Docker daemon. A Docker context is a way to manage connections to different Docker daemons (local or remote). By default, Docker uses the default context, which points to the local daemon. However, you can create new contexts to explicitly define connections.

First, let's check the existing contexts.

docker context ls

You should see the default context listed.

Now, let's create a new context named my-local-context that points to the local Docker endpoint. We will use the docker context create command with the --docker flag to specify the Docker endpoint. Since we are connecting to the local daemon, we can use the default socket path.

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

After creating the context, you can list the contexts again to see the newly created one.

docker context ls

You should now see both default and my-local-context listed. The asterisk (*) indicates the currently active context, which should still be default.

To use the new context, you need to switch to it using the docker context use command.

docker context use my-local-context

Now, if you list the contexts again, my-local-context should be the active one.

docker context ls

You can verify that you are using the my-local-context by running a simple Docker command, like listing images.

docker images

This command will now be executed against the Docker daemon specified by the my-local-context.

Finally, you can switch back to the default context if needed.

docker context use default

Create a context from an existing context

In this step, you will learn how to create a new Docker context by copying the configuration from an existing context. This can be useful if you want to create a slightly modified version of an existing context without having to specify all the details again.

First, let's make sure we have the my-local-context created in the previous step. You can list the contexts to confirm.

docker context ls

Now, we will create a new context named my-copied-context based on the my-local-context. We will use the docker context create command and specify the --from flag followed by the name of the existing context.

docker context create my-copied-context --from my-local-context

After creating the new context, list the contexts again to see my-copied-context in the list.

docker context ls

You should now see default, my-local-context, and my-copied-context. The my-copied-context will have the same Docker endpoint configuration as my-local-context.

You can switch to the new context to verify its configuration.

docker context use my-copied-context

Now, list the contexts again to confirm that my-copied-context is the active one.

docker context ls

You can also inspect the configuration of the new context using the docker context inspect command.

docker context inspect my-copied-context

This will show you the details of the context, including the Docker endpoint it is configured to use. You should see that it is configured to use the local Docker socket, just like my-local-context.

Finally, switch back to the default context for the next step.

docker context use default

Create a context using the Docker endpoint configuration from another context

In this step, you will learn how to create a new Docker context and explicitly specify the Docker endpoint configuration, potentially using information from another context as a reference. While the previous step showed how to copy an entire context, this method allows for more granular control over the new context's configuration.

First, let's list the existing contexts to see what we have.

docker context ls

You should see default, my-local-context, and my-copied-context.

Now, we will create a new context named my-explicit-context. We will explicitly define the Docker endpoint using the --docker flag, similar to Step 1. We can refer to the configuration of an existing context like my-local-context to get the correct endpoint details, although in this case, it's the standard local socket.

docker context create my-explicit-context --description "Context with explicit local endpoint" --docker "host=unix:///var/run/docker.sock"

We've also added a description using the --description flag to make the context's purpose clearer.

List the contexts again to see the newly created my-explicit-context.

docker context ls

You should now see default, my-local-context, my-copied-context, and my-explicit-context.

Switch to the new context to make it active.

docker context use my-explicit-context

List the contexts one more time to confirm that my-explicit-context is the active one.

docker context ls

You can also inspect the configuration of my-explicit-context to see the details, including the description you added.

docker context inspect my-explicit-context

This demonstrates how you can create a context by explicitly providing the Docker endpoint configuration, which is useful when connecting to remote Docker daemons or when you need precise control over the connection details.

Summary

In this lab, you learned how to manage Docker endpoints using the docker context create command. You started by creating a new context named my-local-context that explicitly points to the local Docker daemon, demonstrating how to define a connection using the --docker flag and the socket path. You practiced listing existing contexts, switching between them using docker context use, and verifying the active context. You also saw how running a Docker command like docker images is executed against the currently active context.

Furthermore, you explored how to create a new context by leveraging the configuration of an existing context, which is a convenient way to build upon existing connection settings. This allows for efficient management and reuse of Docker endpoint configurations.