| 1 |
Metadata-Version: 1.1 |
| 2 |
Name: dockerpty |
| 3 |
Version: 0.4.1 |
| 4 |
Summary: Python library to use the pseudo-tty of a docker container |
| 5 |
Home-page: https://github.com/d11wtq/dockerpty |
| 6 |
Author: Chris Corbyn |
| 7 |
Author-email: [email protected] |
| 8 |
License: Apache 2.0 |
| 9 |
Description: # Docker PTY |
| 10 |
|
| 11 |
Provides the functionality needed to operate the pseudo-tty (PTY) allocated to |
| 12 |
a docker container, using the Python client. |
| 13 |
|
| 14 |
[] |
| 15 |
(https://travis-ci.org/d11wtq/dockerpty) |
| 16 |
|
| 17 |
## Installation |
| 18 |
|
| 19 |
Via pip: |
| 20 |
|
| 21 |
``` |
| 22 |
pip install dockerpty |
| 23 |
``` |
| 24 |
|
| 25 |
Dependencies: |
| 26 |
|
| 27 |
* docker-py>=0.3.2 |
| 28 |
|
| 29 |
However, this library does not explicitly declare this dependency in PyPi for a |
| 30 |
number of reasons. It is assumed you have it installed. |
| 31 |
|
| 32 |
## Usage |
| 33 |
|
| 34 |
The following example will run busybox in a docker container and place the user |
| 35 |
at the shell prompt via Python. |
| 36 |
|
| 37 |
This obviously only works when run in a terminal. |
| 38 |
|
| 39 |
``` python |
| 40 |
import docker |
| 41 |
import dockerpty |
| 42 |
|
| 43 |
client = docker.Client() |
| 44 |
container = client.create_container( |
| 45 |
image='busybox:latest', |
| 46 |
stdin_open=True, |
| 47 |
tty=True, |
| 48 |
command='/bin/sh', |
| 49 |
) |
| 50 |
|
| 51 |
dockerpty.start(client, container) |
| 52 |
``` |
| 53 |
|
| 54 |
Keyword arguments passed to `start()` will be forwarded onto the client to |
| 55 |
start the container. |
| 56 |
|
| 57 |
When the dockerpty is started, control is yielded to the container's PTY until |
| 58 |
the container exits, or the container's PTY is closed. |
| 59 |
|
| 60 |
This is a safe operation and all resources are restored back to their original |
| 61 |
states. |
| 62 |
|
| 63 |
> **Note:** dockerpty does support attaching to non-tty containers to stream |
| 64 |
container output, though it is obviously not possible to 'control' the |
| 65 |
container if you do not allocate a pseudo-tty. |
| 66 |
|
| 67 |
If you press `C-p C-q`, the container's PTY will be closed, but the container |
| 68 |
will keep running. In other words, you will have detached from the container |
| 69 |
and can re-attach with another `dockerpty.start()` call. |
| 70 |
|
| 71 |
## Tests |
| 72 |
|
| 73 |
If you want to hack on dockerpty and send a PR, you'll need to run the tests. |
| 74 |
In the features/ directory, are features/user stories for how dockerpty is |
| 75 |
supposed to work. To run them: |
| 76 |
|
| 77 |
``` |
| 78 |
-bash$ pip install -r requirements-dev.txt |
| 79 |
-bash$ behave features/ |
| 80 |
``` |
| 81 |
|
| 82 |
You'll need to have docker installed and running locally. The tests use busybox |
| 83 |
container as a test fixture, so are not too heavy. |
| 84 |
|
| 85 |
Step definitions are defined in features/steps/. |
| 86 |
|
| 87 |
There are also unit tests for the parts of the code that are not inherently |
| 88 |
dependent on controlling a TTY. To run those: |
| 89 |
|
| 90 |
``` |
| 91 |
-bash$ pip install -r requirements-dev.txt |
| 92 |
-bash$ py.test tests/ |
| 93 |
``` |
| 94 |
|
| 95 |
Travis CI runs this build inside a UML kernel that is new enough to run docker. |
| 96 |
Your PR will need to pass the build before I can merge it. |
| 97 |
|
| 98 |
- Travis CI build: https://travis-ci.org/d11wtq/dockerpty |
| 99 |
|
| 100 |
## How it works |
| 101 |
|
| 102 |
In a terminal, the three file descriptors stdin, stdout and stderr are all |
| 103 |
connected to the controlling terminal (TTY). When you pass the `tty=True` flag |
| 104 |
to docker's `create_container()`, docker allocates a fake TTY inside the |
| 105 |
container (a PTY) to which the container's stdin, stdout and stderr are all |
| 106 |
connected. |
| 107 |
|
| 108 |
The docker API provides a way to access the three sockets connected to the PTY. |
| 109 |
If with access to the host system's TTY file descriptors and the container's |
| 110 |
PTY file descriptors, it is trivial to simply 'pipe' data written to these file |
| 111 |
descriptors between the host and the container. Doing this makes the user's |
| 112 |
terminal effectively become the pseudo-terminal from inside the container. |
| 113 |
|
| 114 |
In reality it's a bit more complicated than this, since care must be taken to |
| 115 |
put the host terminal into raw mode (where keys such as enter are not |
| 116 |
interpreted with any special meaning) and restore it on exit. Additionally, the |
| 117 |
container's stdout and stderr streams along with `sys.stdin` must be made |
| 118 |
non-blocking so that they can be used with `select()` without blocking the main |
| 119 |
process. These attributes are restored on exit. |
| 120 |
|
| 121 |
The size of a terminal cannot be controlled by sending data to stdin and can |
| 122 |
only be controlled by the terminal program itself. Since the pseudo-terminal is |
| 123 |
running inside a real terminal, it is import that the size of the PTY be kept |
| 124 |
the same as that of the presenting TTY. For this reason, docker provides an API |
| 125 |
call to resize the allocated PTY. A SIGWINCH handler is used to detect window |
| 126 |
size changes and resize the pseudo-terminal as needed. |
| 127 |
|
| 128 |
## Contributors |
| 129 |
|
| 130 |
- Primary author: [Chris Corbyn](https://github.com/d11wtq) |
| 131 |
- Collaborator: [Daniel Nephin](https://github.com/dnephin) |
| 132 |
- Contributor: [Stephen Moore](https://github.com/delfick) |
| 133 |
- Contributor: [Ben Firshman](https://github.com/bfirsh) |
| 134 |
|
| 135 |
## Copyright & Licensing |
| 136 |
|
| 137 |
Copyright © 2014 Chris Corbyn. See the LICENSE.txt file for details. |
| 138 |
|
| 139 |
Keywords: docker,tty,pty,terminal |
| 140 |
Platform: UNKNOWN |
| 141 |
Classifier: Development Status :: 4 - Beta |
| 142 |
Classifier: License :: OSI Approved :: Apache Software License |
| 143 |
Classifier: Programming Language :: Python |
| 144 |
Classifier: Environment :: Console |
| 145 |
Classifier: Intended Audience :: Developers |
| 146 |
Classifier: Topic :: Terminals |
| 147 |
Classifier: Topic :: Terminals :: Terminal Emulators/X Terminals |
| 148 |
Classifier: Topic :: Software Development :: Libraries |
| 149 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |