Skip to content
Closed
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 21 additions & 1 deletion lib/kitchen/driver/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand All @@ -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}")
Expand Down