Working with docker context

Corrado Ignoti
2 min readJun 6, 2022
Photo by Ian Taylor on Unsplash

What are docker contexts?

Let’s explain this concept with an easy example. You work in an enterprise where there are two different cluster: the develop and the production one. You have to mange (publish, see logs…) on both two clusters accordingly with your work. Each cluster represents a “context” switching the context you can issue docker commands on the different cluster.

To switch (change) the context you are operating on you can use this command:

$ docker context use <context-name>

A context is made up of these properties:

  • Name
  • Endpoint configuration
  • TLS info
  • Orchestrator

when you install docker it creates a default context, you can inspect it with:

$ docker context ls
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current ... unix:///var/run/docker.sock swarm

Create a context doing something like this:

$ docker context create docker-dev-host \
--default-stack-orchestrator=swarm \
--docker host=ssh://user@192.168.1.100

Dockercon

dockercon is a tool to switch between docker contexts faster. It also allows you to create context faster.

To add a context:

$ dockercon -a mydockerhst-local --host ssh://user@192.168.1.1 --stack swarm

To switch context:

$ dockercon mydockerhst-local
mydockerhst-local
Current context is now "mydockerhst-local"
is now the dafult context

To see the saved context:

$ dockercon
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current ... unix:///var/run/docker.sock swarm

Hope you enjoy!

--

--

Corrado Ignoti

Mobile enthusiastic with open source always in mind. Guitar player and music lover. With the head in the "cloud". Scout for the rest. He/him