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

Conversation

h0tw1r3
Copy link
Contributor

@h0tw1r3 h0tw1r3 commented Feb 9, 2023

Adds support for parsing the docker context or DOCKER_HOST environment variable to determine the hostname used to connect to the instance.

@h0tw1r3 h0tw1r3 requested a review from a team as a code owner February 9, 2023 04:55
@CLAassistant
Copy link

CLAassistant commented Apr 19, 2023

CLA assistant check
All committers have signed the CLA.

@h0tw1r3 h0tw1r3 force-pushed the remote-docker branch 2 times, most recently from f3b2c3d to 9e51c97 Compare July 23, 2023 18:32
@puppetlabs puppetlabs deleted a comment from codecov-commenter Jul 23, 2023
@h0tw1r3
Copy link
Contributor Author

h0tw1r3 commented Jul 23, 2023

Until this is merged, here's my painless hack.

Drop the following code into a rake file (ie. rakelib/litmus.rake). When you run provision tasks, it will automatically patch the generated inventory file for remote docker support.

# Patch docker targets in inventory to support DOCKER_HOST environment variable
task :patch_docker_inventory do
  require 'yaml'
  require 'uri'
  hostname = if ENV.key? 'DOCKER_HOST'
               URI.parse(ENV['DOCKER_HOST']).host || ENV['DOCKER_HOST']
             else
               'localhost'
             end
  if hostname != 'localhost'
    inventory_fn = "#{__dir__}/../spec/fixtures/litmus_inventory.yaml"
    data = YAML.load_file(inventory_fn)
    data['groups'].map do |group|
      if group['targets'].size > 0
        group['targets'].map do |target|
          target['uri'].gsub!('localhost', hostname) if target['facts']['provisioner'] == 'docker' && target['uri'] =~ %r{^localhost:}
        end
      end
    end
    File.open(inventory_fn, 'w') { |f| f.write(data.to_yaml) }
  end
end

Rake::Task['litmus:provision'].enhance do
  Rake::Task['patch_docker_inventory'].execute
end
Rake::Task['litmus:provision_list'].enhance do
  Rake::Task['patch_docker_inventory'].execute
end

@jordanbreen28
Copy link
Contributor

jordanbreen28 commented Jul 27, 2023

hey @h0tw1r3 thanks for this, its great!
Just a couple of requests before we can merge..

Could you please:

  1. Add a spec test for this new functionality
  2. Add a section in the README docker section explaining how to use this
  3. rebase off main

Thanks again. 🚀

@h0tw1r3
Copy link
Contributor Author

h0tw1r3 commented Aug 3, 2023

Could you please:

1. Add a spec test for this new functionality

@jordanbreen28 Can you point me to the existing docker spec tests?

@h0tw1r3 h0tw1r3 requested a review from a team as a code owner August 20, 2023 21:08
@h0tw1r3 h0tw1r3 changed the title DOCKER_HOST env support docker context and DOCKER_HOST env support Aug 20, 2023
@h0tw1r3
Copy link
Contributor Author

h0tw1r3 commented Nov 1, 2023

Standard use cases are a docker CI runner using a remote docker to avoid the pitfalls of docker-in-docker, or a workstation which must to use a remote docker service (ie. Windows in AWS workspaces, security requirement, etc).

The "DOCKER_HOST" way, is universally supported by all docker versions.

export DOCKER_HOST=docker0.dev.internal:2376
pdk bundle exec rake 'litmus:provision[docker, litmusimage/ubuntu:22.04]'
pdk bundle exec rake litmus:install_agent
pdk bundle exec rake litmus:acceptance:parallel

The modern way, is to use contexts.

# create remote context
docker context create remote0 --description 'remote docker0' --docker "host=tcp://docker0.dev.internal:2376"

# run acceptance tests on remote docker
docker use remote0
pdk bundle ...

# run acceptance tests on local docker
docker use default
pdk bundle ...

@jordanbreen28 jordanbreen28 merged commit 113c01a into puppetlabs:main Nov 1, 2023
5 checks passed
@h0tw1r3 h0tw1r3 deleted the remote-docker branch December 28, 2023 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants