Skip to content

Commit 113c01a

Browse files
Merge pull request #200 from h0tw1r3/remote-docker
docker context and DOCKER_HOST env support
2 parents e166c1c + edf750a commit 113c01a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ Ran on 1 node in 1.54 seconds
134134

135135
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)
136136

137-
provision
137+
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.
138+
139+
#### Provision
138140

139141
```ruby
140142
$ 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
@@ -167,7 +169,7 @@ These defaults can be overriden by passing the flags with different values i.e.
167169
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"]}'
168170
```
169171

170-
tear_down
172+
#### Tear down
171173

172174
```ruby
173175
$ 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

tasks/docker.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: true
33

44
require 'json'
5+
require 'uri'
56
require 'yaml'
67
require 'puppet_litmus'
78
require_relative '../lib/task_helper'
@@ -159,7 +160,16 @@ def provision(image, inventory_location, vars)
159160
os_release_facts = get_image_os_release_facts(image)
160161
distro = os_release_facts['ID']
161162
version = os_release_facts['VERSION_ID']
162-
hostname = 'localhost'
163+
164+
hostname = (ENV['DOCKER_HOST'].nil? || ENV['DOCKER_HOST'].empty?) ? 'localhost' : URI.parse(ENV['DOCKER_HOST']).host || ENV['DOCKER_HOST']
165+
begin
166+
# Use the current docker context to determine the docker hostname
167+
docker_context = JSON.parse(run_local_command('docker context inspect'))[0]
168+
hostname = URI.parse(docker_context['Endpoints']['docker']['Host']).host || hostname
169+
rescue RuntimeError
170+
# old clients may not support docker context
171+
end
172+
163173
group_name = 'ssh_nodes'
164174
warn '!!! Using private port forwarding!!!'
165175
front_facing_port = random_ssh_forwarding_port

0 commit comments

Comments
 (0)