Click here to Skip to main content
15,922,407 members
Articles / Containers / Docker
Tip/Trick

Docker Contexts: Simplifying Remote Operations

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Apr 2024CPOL2 min read 1.3K   1
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)


Written By
Software Developer
Italy Italy
I'm a software engineer, specializing in backend development and distributed systems. I have extensive experience in designing and implementing resilient, responsive, scalable, and maintainable systems using C#, .NET on top of cutting-edge technologies.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA5-Apr-24 11:39
professionalȘtefan-Mihai MOGA5-Apr-24 11:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.