Python - tlsv1 alert protocol version error in Docker client connection
<p>I'm using <a href="https://docker-py.readthedocs.io/en/latest/api/" rel="nofollow">Docker-py</a> and <a href="https://github.com/d11wtq/dockerpty" rel="nofollow">dockerpty</a> in order to <code>exec</code> commands using the <code>Docker</code> Python API. </p>

<p>The code it's pretty simple:</p>

<pre><code>container = client.inspect_container(containerId)[0]
dockerpty.exec_command(client, container, command)
</code></pre>

<p>When I want to execute commands such as <code>echo 'hello'</code> it works fine. However, commands like <code>/bin/bash</code>, even though I'm able to get the terminal, it is causing the following error:</p>

<pre><code>ubuntu:test$ python main.py exec [containerid] /bin/bash
root@so1:/opt/apache# Traceback (most recent call last):
  File "__main__.py", line 216, in &lt;module&gt;
    main()
  File "__main__.py", line 201, in main
    ec.execute()
    dockerpty.exec_command(client, container, command)
  File "/usr/local/lib/python2.7/site-packages/dockerpty/__init__.py", line 44, in exec_command
    PseudoTerminal(client, operation).start()
  File "/usr/local/lib/python2.7/site-packages/dockerpty/pty.py", line 334, in start
    self._hijack_tty(pumps)
  File "/usr/local/lib/python2.7/site-packages/dockerpty/pty.py", line 373, in _hijack_tty
    pump.flush()
  File "/usr/local/lib/python2.7/site-packages/dockerpty/io.py", line 367, in flush
    read = self.from_stream.read(n)
  File "/usr/local/lib/python2.7/site-packages/dockerpty/io.py", line 120, in read
    return self.fd.recv(n)
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 194, in recv
    data = self.connection.recv(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1302, in recv
    self._raise_ssl_error(self._ssl, result)
  File "/usr/local/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1172, in _raise_ssl_error
    _raise_current_error()
  File "/usr/local/lib/python2.7/site-packages/OpenSSL/_util.py", line 48, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'ssl3_read_bytes', 'tlsv1 alert protocol version')]
</code></pre>

<p>When I create the client, I specify the <code>tls_config</code> to use <code>tlsv1.2</code>:</p>

<pre><code>    tls_config = docker.tls.TLSConfig(
        client_cert=(cert, key),
        ssl_version=ssl.PROTOCOL_TLSv1_2,
    )
    client = docker.Client(base_url=url, timeout=timeout,
                           tls=tls_config, user_agent=user_agent)
</code></pre>

<p>Why am I getting this <code>'tlsv1 alert protocol version'</code> error, and how can I fix this?</p>

<p>In some older versions of Python, <code>ssl.PROTOCOL_TLSv1_2</code> isn't available. You can easily check by attempting to import it from the Python console inside the container: </p>

<pre><code>root@57c6d8b01861:/# python
Python 2.7.8 (default, Nov 26 2014, 22:28:51) 
&gt;&gt;&gt; import ssl
&gt;&gt;&gt; ssl.PROTOCOL_TLSv1_2
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: 'module' object has no attribute 'PROTOCOL_TLSv1_2'
&gt;&gt;&gt; ssl.PROTOCOL_TLSv1
3
</code></pre>

<p>If this is the case, try updating Python in your docker image to <code>&gt;=2.7.9</code>.</p>

<p>Also ensure that the <code>openssl</code> version is >=<code>1.0.1</code>. </p>

python bash ssl docker