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..6070e62a 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 '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" + 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}")