Attach to a container
POST /containers/(id)/attach
Attach to the container id
Example request:
POST /containers/16253994b7c4/attach?logs=1&stream=0&stdout=1 HTTP/1.1Example response:
HTTP/1.1 101 UPGRADED
Content-Type: application/vnd.docker.raw-stream
Connection: Upgrade
Upgrade: tcp
{{ STREAM }}Query Parameters:
detachKeys – Override the key sequence for detaching a container.
logs – 1/True/true or 0/False/false, return logs. Default
false.stream – 1/True/true or 0/False/false, return stream. Default
false.stdin – 1/True/true or 0/False/false, if
stream=true, attach tostdin. Defaultfalse.stdout – 1/True/true or 0/False/false, if
logs=true, returnstdoutlog, ifstream=true, attach tostdout. Defaultfalse.stderr – 1/True/true or 0/False/false, if
logs=true, returnstderrlog, ifstream=true, attach tostderr. Defaultfalse.
Status Codes:
101 – no error, hints proxy about hijacking
200 – no error, no upgrade header found
400 – bad parameter
404 – no such container
500 – server error
Stream details:
When using the TTY setting is enabled in
POST /containers/create, the stream is the raw data from the process PTY and client'sstdin. When the TTY is disabled, then the stream is multiplexed to separatestdoutandstderr.The format is a Header and a Payload (frame).
HEADER
The header contains the information which the stream writes (
stdoutorstderr). It also contains the size of the associated frame encoded in the last four bytes (uint32).It is encoded on the first eight bytes like this:
header := [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}STREAM_TYPEcan be:0:
stdin(is written onstdout)1:
stdout2:
stderrSIZE1, SIZE2, SIZE3, SIZE4are the four bytes of theuint32size encoded as big endian.PAYLOAD
The payload is the raw stream.
IMPLEMENTATION
The simplest way to implement the Attach protocol is the following:
Read eight bytes.
Choose
stdoutorstderrdepending on the first byte.Extract the frame size from the last four bytes.
Read the extracted size and output it on the correct output.
Goto 1.
Last updated