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

Add docker_context_info module #1039

Merged
merged 10 commits into from
Feb 10, 2025

Conversation

felixfontein
Copy link
Collaborator

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
  • New Module Pull Request
COMPONENT NAME

docker_context_info

Copy link

github-actions bot commented Feb 6, 2025

Docs Build 📝

Thank you for contribution!✨

This PR has been merged and the docs are now incorporated into main:
https://ansible-collections.github.io/community.docker/branch/main

@felixfontein felixfontein changed the title [WIP] Add docker_context_info module Add docker_context_info module Feb 6, 2025
@felixfontein
Copy link
Collaborator Author

@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.
Copy link
Member

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?

Copy link
Collaborator Author

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/

Comment on lines +98 to +103
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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Collaborator Author

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).

@felixfontein felixfontein merged commit 3b6068e into ansible-collections:main Feb 10, 2025
82 checks passed
@felixfontein felixfontein deleted the context branch February 10, 2025 20:59
@felixfontein
Copy link
Collaborator Author

@markuman thanks a lot for reviewing this!

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.

2 participants