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

docker context and DOCKER_HOST env support #200

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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 @@ -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
Expand Down
Loading