How to use docker context show command to display current context

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker context show command to display the currently active Docker context. You will begin by displaying the default context, which connects to the local Docker daemon.

Following this, you will create a new Docker context, switch to this new context and verify the change using docker context show. Finally, you will switch back to the default context and explore how to integrate the docker context show command into your shell prompt for easy visibility of your current Docker connection.


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/info("Display System-Wide Information") docker/SystemManagementGroup -.-> docker/system("Manage Docker") subgraph Lab Skills docker/ls -.-> lab-555135{{"How to use docker context show command to display current context"}} docker/create -.-> lab-555135{{"How to use docker context show command to display current context"}} docker/info -.-> lab-555135{{"How to use docker context show command to display current context"}} docker/system -.-> lab-555135{{"How to use docker context show command to display current context"}} end

Display the default Docker context

In this step, we will learn how to display the default Docker context. A Docker context is a way to connect to different Docker daemons. By default, Docker uses the default context, which connects to the Docker daemon running on the local machine.

To display the current Docker context, we can use the docker context show command.

docker context show

You should see default as the output, indicating that you are currently using the default Docker context.

Create a new Docker context

In this step, we will create a new Docker context. Creating a new context allows you to define connection parameters for a different Docker daemon, which could be on a remote machine or a different local setup.

We will create a context named my-context that connects to the local Docker daemon. While this might seem redundant for a local connection, it demonstrates the process of creating a context.

To create a new context, we use the docker context create command followed by the context name. We also need to specify the endpoint for the Docker daemon using the --docker flag. For a local daemon, the endpoint is typically unix:///var/run/docker.sock.

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

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

To verify that the context was created, you can list all available contexts using the docker context ls command.

docker context ls

You should see my-context listed in the output along with the default context.

Switch to the new Docker context and display it

In this step, we will switch to the new Docker context we created in the previous step and then display the currently active context to confirm the switch.

To switch to a different Docker context, we use the docker context use command followed by the name of the context we want to use. In our case, the context name is my-context.

docker context use my-context

You should see output confirming that the context has been switched to my-context.

Now, let's display the current Docker context again using the docker context show command to verify that we are indeed using the my-context.

docker context show

The output of this command should now be my-context, indicating that the switch was successful.

Switch back to the default context and display it

In this step, we will switch back to the default Docker context. This is useful when you have been working with a different context and want to return to the standard local Docker daemon.

To switch back to the default context, we use the docker context use command followed by the name of the default context, which is default.

docker context use default

You should see output confirming that the context has been switched back to default.

Now, let's display the current Docker context again using the docker context show command to verify that we are back to using the default context.

docker context show

The output of this command should now be default, confirming that the switch was successful.

Integrate docker context show into your shell prompt

In this step, we will integrate the docker context show command into your shell prompt. This allows you to see the current Docker context directly in your terminal prompt, making it easier to keep track of which Docker daemon you are interacting with.

We will modify the .zshrc file in your home directory, which is the configuration file for the Zsh shell. We will add a function that gets the current Docker context and then include this function's output in the prompt string.

First, open the .zshrc file using the nano editor:

nano ~/.zshrc

Scroll to the end of the file and add the following lines:

## Function to get current Docker context
get_docker_context() {
  docker context show 2> /dev/null
}

## Add Docker context to prompt
PROMPT='$(get_docker_context) %~ %## '

Let's break down these lines:

  • get_docker_context(): This defines a new shell function named get_docker_context.
  • docker context show 2>/dev/null: Inside the function, this command gets the current Docker context. 2>/dev/null redirects any error output to /dev/null, preventing it from cluttering the prompt if Docker is not running or there's an issue.
  • PROMPT='$(get_docker_context) %~ %## ': This line sets the PROMPT environment variable, which defines the appearance of your shell prompt.
    • $(get_docker_context): This executes the get_docker_context function and includes its output in the prompt.
    • %~: This displays the current working directory, with the home directory abbreviated as ~.
    • %#: This displays a # if you are the root user or a % if you are a regular user.

Save the file by pressing Ctrl + X, then Y, and then Enter.

To apply the changes to your current terminal session, you need to source the .zshrc file:

source ~/.zshrc

After sourcing the file, your shell prompt should now display the current Docker context (which should be default) before the current directory.

You can test this by switching to the my-context again:

docker context use my-context

Your prompt should update to show my-context followed by your current directory.

Then switch back to the default context:

docker context use default

Your prompt should change back to showing default.

Summary

In this lab, we learned how to manage Docker contexts using the docker context show command. We began by displaying the default Docker context, which connects to the local daemon. We then created a new context named my-context, demonstrating the process of defining connection parameters for a different daemon, even if it's the same local one in this example.

Following the creation, we practiced switching to the newly created context using docker context use and verified the active context with docker context show. This hands-on experience solidified our understanding of how to navigate between different Docker environments and confirm the current connection point.