Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker): Honor the host specified in current docker context #464

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

rajiv-k
Copy link
Contributor

@rajiv-k rajiv-k commented Jul 20, 2023

One can have multiple docker hosts that they manage and can switch between them using the "docker context" command.
Previously, we used to connect to either the "default docker host" (for example, unix:///var/run/docker.sock in case of Linux/macOS) or to the host specified via the "DOCKER_HOST" environment variable. However, if one switched to a different context, we had no way of detecting that.

Now, we determine the docker host that we connect to by evaluating the following sources in decreasing order of precedence:

  • value of "DOCKER_HOST" environment variable
  • host specified in the context set via the "DOCKER_CONTEXT" environment variable
  • "default docker host" as applicable, otherwise

Fixes: #285

@rajiv-k rajiv-k changed the title Honor the host specified in current docker context feat(docker): Honor the host specified in current docker context Jul 20, 2023
return defaultDockerHost, nil
}

storeConfig := ctxstore.NewConfig(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what's happening from here to line 380?

Copy link
Contributor Author

@rajiv-k rajiv-k Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Here's the gist of what's happening.

Line: 364: We create a new Store by passing the path to store directory ($HOME/.docker/contexts) and the "store config" (which is used to return a typed value from a "generic" value that the Store can store).
Here, we are interested in retrieving the information associated with a docker context aka the EndpointMeta, which contains the docker host information. That is why the "store config" includes a getter meant for the EndpointMeta (Line: 359).

The rest of the surrounding lines are just plumbing code to extract the host information and error handling.

Bonus:

What's inside the contexts directory?

$ tree ~/.docker/contexts/meta/
$HOME/.docker/contexts/meta/
├── 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
│   └── meta.json
├── ed027fa4e9dc0950c3d3acf4a7bbaf242810ff73290de387e34e6d26c2a5b21b
│   └── meta.json
├── f24fd3749c1368328e2b149bec149cb6795619f244c5b584e844961215dadd16
│   └── meta.json
└── fe9c6bd7a66301f49ca9b6a70b217107cd1284598bfc254700c989b916da791e
    └── meta.json

Those directory names are the sha256 sum of the of the context name. For example, one of my context is named colima .

$ echo -n "colima"| openssl sha256
(stdin)= f24fd3749c1368328e2b149bec149cb6795619f244c5b584e844961215dadd16

What's inside the meta.json?

$ cat $HOME/.docker/contexts/meta/f24fd3749c1368328e2b149bec149cb6795619f244c5b584e844961215dadd16/meta.json | jq '.'
{
  "Name": "colima",
  "Metadata": {
    "Description": "colima"
  },
  "Endpoints": {
    "docker": {
      "Host": "unix:///$HOME/.colima/default/docker.sock",
      "SkipTLSVerify": false
    }
  }
}

One can have multiple docker hosts that they manage and can switch
between them using the "docker context" command.
Previously, we used to connect to either the "default docker host" (for
example, unix:///var/run/docker.sock in case of Linux/macOS) or to the
host specified via the "DOCKER_HOST" environment variable.
However, if one switched to a different context, we had no way of detecting
that.

Now, we determine the docker host that we connect to by evaluating the
following sources in decreasing order of precedence:
- value of "DOCKER_HOST" environment variable
- host specified in the context set via the "DOCKER_CONTEXT" environment variable
- "default docker host" as applicable, otherwise

Fixes: jesseduffield#285

Signed-off-by: Rajiv Kushwaha <raj25by10@gmail.com>
@lclpedro
Copy link

Is not resolved if you create link to colima with default docker.sock?
sudo ln -f -s ~/.colima/default/docker.sock /var/run/docker.sock

@rajiv-k
Copy link
Contributor Author

rajiv-k commented Sep 22, 2023

@lclpedro Creating the symlink would work. But the issue is that if you manage multiple docker environments, you will have to re-create these symlinks whenever you want to target a different environment (as described in linked issue #285). docker context exists to solve this very problem.

@jesseduffield jesseduffield merged commit d2fa5e9 into jesseduffield:master Oct 9, 2023
@jesseduffield
Copy link
Owner

Sorry this took me so long to get back to @rajiv-k . Great work!

@whobutsb
Copy link

Thank you for adding this feature! I added the feature request in December 2021. Really happy to see it get added.

@whobutsb
Copy link

whobutsb commented Oct 12, 2023

@rajiv-k - I was testing out this new functionality. Is honoring the docker context only available for http connections? Am I able to use ssh connections? When I changed my docker context and ran lazydocker I wasn't able to connect. Here is the inspection on my docker context:

> docker context inspect uj-staging
[
    {
        "Name": "uj-staging",
        "Metadata": {},
        "Endpoints": {
            "docker": {
                "Host": "ssh://uj-staging",
                "SkipTLSVerify": false
            }
        },
    }
]

Error Message:
Screenshot 2023-10-12 at 7 50 41 AM

@rajiv-k
Copy link
Contributor Author

rajiv-k commented Feb 14, 2024

@whobutsb Looks like a possible regression. I am working on a PR to make this work (again).
Could you please create a new issue for it?

Update: This commit fixes the above issue with remote docker daemon over ssh.

@rajiv-k rajiv-k deleted the honor-docker-context branch February 18, 2024 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

View docker enviroment when using docker context
4 participants