How to use docker context export command to export a context

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker context export command to export a Docker context. We will begin by creating a new Docker context, which allows you to connect your Docker CLI to different Docker daemons.

Following the creation of a new context, you will practice exporting this context. You will learn how to export the context to a file for later use or sharing, and also how to export the context directly to standard output (STDOUT). This hands-on experience will demonstrate the practical application of the docker context export command for managing and sharing your Docker environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") subgraph Lab Skills docker/ls -.-> lab-555130{{"How to use docker context export command to export a context"}} docker/inspect -.-> lab-555130{{"How to use docker context export command to export a context"}} docker/create -.-> lab-555130{{"How to use docker context export command to export a context"}} 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 connect your Docker CLI to a different Docker daemon. This can be useful when you want to manage Docker on a remote machine or a different environment.

By default, your Docker CLI connects to the local Docker daemon. We can see the current context using the docker context ls command.

docker context ls

You should see an output similar to this, indicating the default context is active:

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

Now, let's create a new context. We will create a context named my-context. For this example, we will still point it to the local Docker daemon, but in a real-world scenario, you would point it to a remote daemon.

We use the docker context create command followed by the context name and the endpoint.

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

After creating the context, you should see a confirmation message:

my-context
Successfully created context "my-context"

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

docker context ls

You should now see both the default and my-context listed.

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

To switch to the new context, we use the docker context use command followed by the context name.

docker context use my-context

You will see a message confirming the context switch:

my-context
Current context is now "my-context"

Finally, let's list the contexts one more time to confirm that my-context is now the active context, indicated by the asterisk *.

docker context ls

The output should show my-context as the active context:

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

Export the Docker context to a file

In this step, we will learn how to export a Docker context to a file. Exporting a context allows you to save its configuration to a file, which can then be shared or imported on another machine. This is particularly useful for sharing access to remote Docker environments.

We will export the my-context that we created in the previous step. We use the docker context export command, followed by the context name and the desired output file path. We will export it to a file named my-context.dockercontext in the ~/project directory.

docker context export my-context > ~/project/my-context.dockercontext

This command will export the configuration of the my-context to the specified file. There will be no output to the console if the command is successful.

To verify that the file was created, we can list the files in the ~/project directory using the ls command.

ls ~/project/

You should see my-context.dockercontext listed among the files.

We can also view the content of the exported context file using the cat command.

cat ~/project/my-context.dockercontext

The output will show the configuration of the my-context in a JSON format. It will contain information about the context, such as its name and the Docker endpoint.

{
  "Name": "my-context",
  "Metadata": null,
  "Endpoints": {
    "docker": {
      "Host": "unix:///var/run/docker.sock",
      "SkipTLSVerify": false
    }
  },
  "TLSMaterial": null
}

This exported file can now be used to import this context on another machine using the docker context import command.

Export the Docker context to STDOUT

In this step, we will learn how to export a Docker context directly to standard output (STDOUT). This is useful when you want to quickly view the context configuration or pipe it to another command without saving it to a file.

We will again use the docker context export command with the my-context we created earlier. This time, we will not redirect the output to a file.

docker context export my-context

Executing this command will print the JSON representation of the my-context configuration directly to your terminal.

{
  "Name": "my-context",
  "Metadata": null,
  "Endpoints": {
    "docker": {
      "Host": "unix:///var/run/docker.sock",
      "SkipTLSVerify": false
    }
  },
  "TLSMaterial": null
}

This output is the same content that was saved to the my-context.dockercontext file in the previous step. Exporting to STDOUT is a convenient way to inspect the context configuration on the fly.

You can also pipe this output to other commands for further processing. For example, you could pipe it to jq to parse the JSON or to grep to search for specific information.

For instance, to find the Docker endpoint from the exported context:

docker context export my-context | grep "unix:///var/run/docker.sock"

This command will export the context to STDOUT and then use grep to find the line containing "unix:///var/run/docker.sock". The output will be:

      "Host": "unix:///var/run/docker.sock",

This demonstrates how you can easily work with the context configuration directly from the command line without creating intermediate files.

Summary

In this lab, we learned how to create and manage Docker contexts. We started by understanding what a Docker context is and how to list existing contexts using docker context ls. We then created a new context named my-context using docker context create, demonstrating how to specify the Docker endpoint. Finally, we practiced switching between contexts using docker context use.

Following the creation and switching of contexts, we explored the docker context export command. We learned how to export a specific Docker context to a file, allowing for easy sharing or backup of context configurations. Additionally, we saw how to export the context configuration directly to standard output, providing flexibility for scripting or immediate use. These steps provided a comprehensive understanding of managing and exporting Docker contexts for connecting to different Docker daemons.