-
Notifications
You must be signed in to change notification settings - Fork 127
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
Add docker_context_info module #1039
Conversation
This is a combination of the latest git version (https://github.com/docker/docker-py/tree/db7f8b8bb67e485a7192846906f600a52e0aa623) with some fixes to make it compatible with Python 2.7 and adjusting some imports.
Docs Build 📝Thank you for contribution!✨ This PR has been merged and the docs are now incorporated into |
@ssbarnea this might be useful for you when trying to mix community.docker with Docker CLI tools (which consider Docker contexts). Something like - name: Get current context
community.docker.docker_context_info:
only_current: true
register: docker_current_context
- name: Run community.docker modules with current context
module_defaults:
group/community.docker.docker: "{{ docker_current_context.contexts[0].config }}"
block:
- name: Task using the current context
community.docker.docker_container:
image: ubuntu:latest
name: ubuntu
state: started will allow you to run community.docker modules with Docker's current context. |
# This code is part of the Ansible collection community.docker, but is an independent component. | ||
# This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/) | ||
# | ||
# Copyright (c) 2016-2025 Docker, Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this copyright be docker? is it copied from some upstream sdk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it's copied from https://github.com/docker/docker-py/
host = parse_host(path, IS_WINDOWS_PLATFORM, tls) | ||
if host == DEFAULT_UNIX_SOCKET: | ||
# remove http+ from default docker socket url | ||
if host.startswith("http+"): | ||
host = host[5:] | ||
return host |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
host = parse_host(path, IS_WINDOWS_PLATFORM, tls) | |
if host == DEFAULT_UNIX_SOCKET: | |
# remove http+ from default docker socket url | |
if host.startswith("http+"): | |
host = host[5:] | |
return host | |
host = parse_host(path, IS_WINDOWS_PLATFORM, tls) | |
# remove http+ from default docker socket url if exists | |
return host.split("http+")[-1] |
But I don't know how the host looks like when it's not a DEFAULT_UNIX_SOCKET
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to change this, since it will be some divergence between the original Docker SDK for Python and this vendored version of the code that might break at some point in funny/unexpected ways.
Docker SDK for Python uses http+
or https+
before unix://
to indicate the exact protocol to use (with or without TLS), and at the same time tries to stay compatible with the Docker CLI programs that only use unix://
without http+
or https+
(and don't understand that). This check here is one of the places where it tries to return the same value that the CLI tools do, at least in one very specific case (namely the default Docker socket).
@markuman thanks a lot for reviewing this! |
SUMMARY
The goal is to have a module to query information on Docker contexts.
This also vendors the contexts part of the Docker SDK for Python (https://github.com/docker/docker-py/tree/main/docker/context).
ISSUE TYPE
COMPONENT NAME
docker_context_info