From edf750aca825355a3673615f72baa8b5205b1a8b Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Wed, 8 Feb 2023 22:45:45 -0600 Subject: [PATCH] docker context and DOCKER_HOST support --- README.md | 6 ++++-- tasks/docker.rb | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dda33e7..f682bdc 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,9 @@ Ran on 1 node in 1.54 seconds Given an docker image name it will spin up that container and setup external ssh on that platform. For helpful docker tips look [here](https://github.com/puppetlabs/litmus_image/blob/main/README.md) -provision +Containers by default will be managed in the current [docker context](https://docs.docker.com/engine/context/working-with-contexts/), on the [DOCKER_HOST](https://docs.docker.com/engine/reference/commandline/cli/#environment-variables), or on localhost if nether are configured. + +#### Provision ```ruby $ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision @@ -162,7 +164,7 @@ These defaults can be overriden by passing the flags with different values i.e. bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=provision platform=ubuntu:14.04 inventory=/Users/tp/workspace/git/provision vars='{ "docker_run_opts": ["-p 8086:8086", "-p 3000:3000"]}' ``` -tear_down +#### Tear down ```ruby $ bundle exec bolt --modulepath /Users/tp/workspace/git/ task run provision::docker --targets localhost action=tear_down inventory=/Users/tp/workspace/git/provision node_name=localhost:2222 diff --git a/tasks/docker.rb b/tasks/docker.rb index ca6f0e3..4ee2fb3 100755 --- a/tasks/docker.rb +++ b/tasks/docker.rb @@ -2,6 +2,7 @@ # frozen_string_literal: true require 'json' +require 'uri' require 'yaml' require 'puppet_litmus' require_relative '../lib/task_helper' @@ -152,7 +153,16 @@ def provision(image, inventory_location, vars) os_release_facts = get_image_os_release_facts(image) distro = os_release_facts['ID'] version = os_release_facts['VERSION_ID'] - hostname = 'localhost' + + hostname = (ENV['DOCKER_HOST'].nil? || ENV['DOCKER_HOST'].empty?) ? 'localhost' : URI.parse(ENV['DOCKER_HOST']).host || ENV['DOCKER_HOST'] + begin + # Use the current docker context to determine the docker hostname + docker_context = JSON.parse(run_local_command('docker context inspect'))[0] + hostname = URI.parse(docker_context['Endpoints']['docker']['Host']).host || hostname + rescue RuntimeError + # old clients may not support docker context + end + group_name = 'ssh_nodes' warn '!!! Using private port forwarding!!!' front_facing_port = random_ssh_forwarding_port