Python library to use the pseudo-tty of a docker container
Likely abandoned. No release or commit for years (last release over 10 years ago).
pip install dockerptyDocker PTY
Provides the functionality needed to operate the pseudo-tty (PTY) allocated to a docker container, using the Python client.
[]
(https://travis-ci.org/d11wtq/dockerpty)
Installation
Via pip:
pip install dockerpty
Dependencies:
- docker-py>=0.3.2
However, this library does not explicitly declare this dependency in PyPi for a number of reasons. It is assumed you have it installed.
Usage
The following example will run busybox in a docker container and place the user at the shell prompt via Python.
This obviously only works when run in a terminal.
import docker
import dockerpty
client = docker.Client()
container = client.create_container(
image='busybox:latest',
stdin_open=True,
tty=True,
command='/bin/sh',
)
dockerpty.start(client, container)
Keyword arguments passed to start() will be forwarded onto the client to
start the container.
When the dockerpty is started, control is yielded to the container's PTY until the container exits, or the container's PTY is closed.
This is a safe operation and all resources are restored back to their original states.
Note: dockerpty does support attaching to non-tty containers to stream container output, though it is obviously not possible to 'control' the container if you do not allocate a pseudo-tty.
If you press C-p C-q, the container's PTY will be closed, but the container
will keep running. In other words, you will have detached from the container
and can re-attach with another dockerpty.start() call.
Tests
If you want to hack on dockerpty and send a PR, you'll need to run the tests. In the features/ directory, are features/user stories for how dockerpty is supposed to work. To run them:
-bash$ pip install -r requirements-dev.txt
-bash$ behave features/
You'll need to have docker installed and running locally. The tests use busybox container as a test fixture, so are not too heavy.
Step definitions are defined in features/steps/.
There are also unit tests for the parts of the code that are not inherently dependent on controlling a TTY. To run those:
-bash$ pip install -r requirements-dev.txt
-bash$ py.test tests/