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:
docker context show
List all Docker contexts:
docker context ls
Switch active context:
docker context use CONTEXT
Replace “CONTEXT” with the desired context name.
To execute commands without switching contexts, add “ — context CONTEXT” on yuor docker commands.
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:
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.