From 0d482298c20b4cce963131558828ee04a705a60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Guimar=C3=A3es=20Malucelli?= Date: Tue, 7 Aug 2018 00:20:19 -0300 Subject: [PATCH 1/2] feat: added use_internal_docker_network feature Co-authored-by: AaronKalair --- README.md | 13 +++++++++++++ lib/kitchen/driver/docker.rb | 22 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 187c5cc2..a41700e1 100644 --- a/README.md +++ b/README.md @@ -482,6 +482,19 @@ Examples: net: br3 ``` +### use_internal_docker_network + +If you want to use kitchen-docker from within another Docker container you'll +need to set this to true. When set to true uses port 22 as the SSH port and +the IP of the container that chef is going to run in as the hostname so that +you can connect to it over SSH from within another Docker container. + +Examples: + +```yaml + use_internal_docker_network: true +``` + ## Development * Source hosted at [GitHub][repo] diff --git a/lib/kitchen/driver/docker.rb b/lib/kitchen/driver/docker.rb index 81aa2c9a..ea3ab557 100644 --- a/lib/kitchen/driver/docker.rb +++ b/lib/kitchen/driver/docker.rb @@ -56,6 +56,7 @@ class Docker < Kitchen::Driver::Base default_config :public_key, File.join(Dir.pwd, '.kitchen', 'docker_id_rsa.pub') default_config :build_options, nil default_config :run_options, nil + default_config :use_internal_docker_network, false default_config :use_sudo, false @@ -119,7 +120,12 @@ def create(state) state[:ssh_key] = config[:private_key] state[:image_id] = build_image(state) unless state[:image_id] state[:container_id] = run_container(state) unless state[:container_id] - state[:hostname] = remote_socket? ? socket_uri.host : 'localhost' + state[:hostname] = 'localhost' + if remote_socket? + state[:hostname] = socket_uri.host + elsif config[:use_internal_docker_network] + state[:hostname] = container_ip(state) + end state[:port] = container_ssh_port(state) if config[:wait_for_sshd] instance.transport.connection(state) do |conn| @@ -376,6 +382,9 @@ def parse_container_ssh_port(output) def container_ssh_port(state) begin + if config[:use_internal_docker_network] + return 22 + end output = docker_command("port #{state[:container_id]} 22/tcp") parse_container_ssh_port(output) rescue @@ -384,6 +393,17 @@ def container_ssh_port(state) end end + def container_ip(state) + begin + cmd = "inspect --format '{{ .NetworkSettings.IPAddress }}'" + cmd << " #{state[:container_id]}" + docker_command(cmd).strip + rescue + raise ActionFailed, + 'Error getting internal IP of Docker container' + end + end + def rm_container(state) container_id = state[:container_id] docker_command("stop -t 0 #{container_id}") From 24f91c7945ea9a01df952f24ef1022ae614071a3 Mon Sep 17 00:00:00 2001 From: Jon Beilke Date: Wed, 10 Oct 2018 13:23:21 -0500 Subject: [PATCH 2/2] update container_ip() to fetch .IPAddress from .NetworkSettings.Networks --- lib/kitchen/driver/docker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kitchen/driver/docker.rb b/lib/kitchen/driver/docker.rb index ea3ab557..6070e62a 100644 --- a/lib/kitchen/driver/docker.rb +++ b/lib/kitchen/driver/docker.rb @@ -395,7 +395,7 @@ def container_ssh_port(state) def container_ip(state) begin - cmd = "inspect --format '{{ .NetworkSettings.IPAddress }}'" + cmd = "inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" cmd << " #{state[:container_id]}" docker_command(cmd).strip rescue