diff --git a/lib/docker.rb b/lib/docker.rb index b87657961..6f6ae5711 100644 --- a/lib/docker.rb +++ b/lib/docker.rb @@ -35,11 +35,25 @@ def default_socket_url end def env_url - ENV['DOCKER_URL'] + ENV['DOCKER_URL'] || ENV['DOCKER_HOST'] + end + + def env_options + if ENV['DOCKER_CERT_PATH'] + cert_path = ENV['DOCKER_CERT_PATH'] + { + client_cert: File.join(cert_path, 'cert.pem'), + client_key: File.join(cert_path, 'key.pem'), + ssl_ca_file: File.join(cert_path, 'ca.pem'), + scheme: 'https' + } + else + {} + end end def url - @url ||= ENV['DOCKER_URL'] || ENV['DOCKER_HOST'] || default_socket_url + @url ||= env_url || default_socket_url # docker uses a default notation tcp:// which means tcp://localhost:2375 if @url == 'tcp://' @url = 'tcp://localhost:2375' @@ -48,7 +62,7 @@ def url end def options - @options ||= {} + @options ||= env_options end def url=(new_url) @@ -57,7 +71,7 @@ def url=(new_url) end def options=(new_options) - @options = new_options + @options = env_options.merge(new_options || {}) reset_connection! end @@ -98,8 +112,8 @@ def validate_version! raise Docker::Error::VersionError, "Expected API Version: #{API_VERSION}" end - module_function :default_socket_url, :env_url, :url, :url=, :options, - :options=, :creds, :creds=, :logger, :logger=, + module_function :default_socket_url, :env_url, :url, :url=, :env_options, + :options, :options=, :creds, :creds=, :logger, :logger=, :connection, :reset_connection!, :version, :info, :authenticate!, :validate_version! end diff --git a/spec/docker_spec.rb b/spec/docker_spec.rb index f0e571aa6..49b3506a2 100644 --- a/spec/docker_spec.rb +++ b/spec/docker_spec.rb @@ -6,6 +6,7 @@ before do ENV['DOCKER_URL'] = nil ENV['DOCKER_HOST'] = nil + ENV['DOCKER_CERT_PATH'] = nil end it { should be_a Module } @@ -42,7 +43,7 @@ its(:connection) { should be_a Docker::Connection } end - context "when the DOCKER_HOST ENV variables is set" do + context "when the DOCKER_HOST ENV variable is set" do before do ENV['DOCKER_HOST'] = 'tcp://someserver:8103' end @@ -56,13 +57,30 @@ before do ENV['DOCKER_HOST'] = 'tcp://someserver:8103' ENV['DOCKER_URL'] = 'tcp://someotherserver:8103' - end its(:options) { {} } its(:url) { should == 'tcp://someotherserver:8103' } its(:connection) { should be_a Docker::Connection } end + + context "when the DOCKER_CERT_PATH and DOCKER_HOST ENV variables are set" do + before do + ENV['DOCKER_HOST'] = 'tcp://someserver:8103' + ENV['DOCKER_CERT_PATH'] = '/boot2dockert/cert/path' + end + + its(:options) { + { + client_cert: '/boot2dockert/cert/path/cert.pem', + client_key: '/boot2dockert/cert/path/key.pem', + ssl_ca_file: '/boot2dockert/cert/path/ca.pem', + scheme: '/boot2dockert/cert/path/https' + } + } + its(:url) { should == 'tcp://someserver:8103' } + its(:connection) { should be_a Docker::Connection } + end end describe '#reset_connection!' do