Module: Docker::API::Transport

Defined in:
lib/docker/api/transport.rb,
lib/docker/api/transport/tcp.rb,
lib/docker/api/transport/tls.rb,
lib/docker/api/transport/base.rb,
lib/docker/api/transport/fake.rb,
lib/docker/api/transport/unix.rb,
lib/docker/api/transport/named_pipe.rb

Overview

Transports know how to reach a daemon, and nothing else.

Every transport answers Base#connect with a connected IO. That single responsibility is what lets the same connection code drive a unix socket in development, TLS in CI and a Windows named pipe on a laptop.

Defined Under Namespace

Classes: Base, Fake, NamedPipe, Tcp, Tls, Unix

Class Method Summary collapse

Class Method Details

.for(url:, tls: nil, open_timeout: 10) ⇒ Docker::API::Transport::Base

Build the transport a Docker host URL calls for.

Examples:

Transport.for(url: "unix:///var/run/docker.sock")
Transport.for(url: "tcp://build:2376", tls: { ca_file: "ca.pem" })

Parameters:

  • url (String)

    a Docker host URL. Understands unix://, tcp://, http://, https://, npipe://, and a bare path (taken as a unix socket), matching what DOCKER_HOST may contain.

  • tls (Hash, nil) (defaults to: nil)

    TLS material: :ca_file, :cert_file, :key_file, :verify. Any of these upgrades a tcp:// URL to TLS.

  • open_timeout (Numeric) (defaults to: 10)

    seconds to wait for a connection

Returns:

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/docker/api/transport.rb', line 28

def self.for(url:, tls: nil, open_timeout: 10)
  scheme, rest = split(url.to_s)
  tls = nil if tls.respond_to?(:empty?) && tls.empty?

  case scheme
  when "unix"
    Unix.new(path: rest)
  when "npipe"
    NamedPipe.new(path: rest)
  when "https"
    tcp_transport(rest, tls || {}, open_timeout, secure: true)
  when "tcp", "http"
    tcp_transport(rest, tls, open_timeout, secure: !tls.nil?)
  else
    raise ConnectionError.new(
      "cannot reach a Docker daemon over '#{scheme}' (from #{url.inspect}); " \
      "expected one of unix, tcp, http, https or npipe"
    )
  end
end