Skip to content

Commit

Permalink
Merge pull request #200 from h0tw1r3/remote-docker
Browse files Browse the repository at this point in the history
docker context and DOCKER_HOST env support
  • Loading branch information
jordanbreen28 authored Nov 1, 2023
2 parents e166c1c + edf750a commit 113c01a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,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
Expand Down Expand Up @@ -167,7 +169,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
Expand Down
12 changes: 11 additions & 1 deletion tasks/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require 'json'
require 'uri'
require 'yaml'
require 'puppet_litmus'
require_relative '../lib/task_helper'
Expand Down Expand Up @@ -159,7 +160,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
Expand Down

0 comments on commit 113c01a

Please sign in to comment.