Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

Docker Contexts: Simplifying Remote Operations

5.00/5 (1 vote)
3 Apr 2024CPOL2 min read 1.9K  
The Docker context command simplifies the configuration and switching between contexts.
Docker Contexts simplify configuration and switching between environments, allowing remote server management. Benefits include executing commands on remote servers as if local and utilizing external authentication tools.

Introduction

Before performing these operations, ensure that Docker CLI is installed on your machine.

You can use an open-source tool such as:

Docker Contexts Overview

The Docker context command simplifies the configuration and switching between contexts. By default, when you install a Docker tool, a default context named “default” is automatically created.

For more information: here

Benefits of Docker Contexts:

  • Execute commands on a remote server as if it were a local Docker instance.
  • Enable the use of external authentication tools on your machine, allowing you to run them as if they were installed on the server.
    For example, you can use AWS CLI to authenticate with a private registry and download content on a server.

Some Useful Docker Context Commands

Print the name of the current context:

C++
docker context show

List all Docker contexts:

C++
docker context ls

Switch active context:

C++
docker context use CONTEXT
Replace “CONTEXT” with the desired context name.

To execute commands without switching contexts, add “ — context CONTEXT” on yuor docker commands.

C++
docker-compose --context CONTEXT .........
Replace “CONTEXT” with the desired context name.

Creating a New Docker Context for Remote Hosts

Before utilizing a remote Docker host, ensure SSH is enabled, necessitating SSH key-based login.

Generate or Locate Your Public SSH Key

Find your public SSH key (id_rsa.pub) typically in:

C:\Users\YOUR_USERNAME\.ssh

If absent, generate it using ssh-keygen.exe -t ed25519.

If you are looking how to use ssh, the easyest way to install in on windows is use chocolatey.

Copying Your Public SSH Key to the Machine

Initially, utilize password-based authentication to access the Linux machine.

Navigate to the .ssh folder and edit the authorized_keys file.

Paste the content of your local .pub file into this file.

Testing the SSH Connection

Verify connectivity to the remote machine using SSH, potentially with software like PuTTY.

 Creating a Docker Context

Utilize docker context create command:

docker context create CONTEXT - docker host=ssh://SERVERUSER@SERVERIP:PORT
Replace:
- “CONTEXT” with the desired context name
-  "SERVERUSER" with the linux server username
-  "SERVERIP" with the linux server ip
-  "PORT" with the linux server port

Once the context is created, easily test it by switching context and executing Docker commands like docker ps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)