@@ -215,11 +215,72 @@ public function containerRemove($container, $v = false, $force = false)
215215 return $ this ->browser ->delete ($ this ->url ('/containers/%s?v=%u&force=%u ' , $ container , $ v , $ force ))->then (array ($ this ->parser , 'expectEmpty ' ));
216216 }
217217
218+ /**
219+ * Sets up an exec instance in a running container id
220+ *
221+ * @param string $container container ID
222+ * @param array $config `array('Cmd' => 'date')` (see link)
223+ * @return Promise Promise<array> with exec ID in the form of `array("Id" => $execId)`
224+ * @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-create
225+ */
226+ public function execCreate ($ container , $ config )
227+ {
228+ return $ this ->postJson ($ this ->url ('/containers/%s/exec ' , $ container ), $ config )->then (array ($ this ->parser , 'expectJson ' ));
229+ }
230+
231+ /**
232+ * Starts a previously set up exec instance id.
233+ *
234+ * If detach is true, this API returns after starting the exec command.
235+ * Otherwise, this API sets up an interactive session with the exec command.
236+ *
237+ * @param string $exec exec ID
238+ * @param array $config (see link)
239+ * @return Promise Promise<array> stream of message objects
240+ * @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-start
241+ */
242+ public function execStart ($ exec , $ config )
243+ {
244+ return $ this ->postJson ($ this ->url ('/exec/%s/start ' , $ exec ), $ config )->then (array ($ this ->parser , 'expectJson ' ));
245+ }
246+
247+ /**
248+ * Resizes the tty session used by the exec command id.
249+ *
250+ * This API is valid only if tty was specified as part of creating and starting the exec command.
251+ *
252+ * @param string $exec exec ID
253+ * @param int $w TTY width
254+ * @param int $h TTY height
255+ * @return Promise Promise<null>
256+ * @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-resize
257+ */
258+ public function execResize ($ exec , $ w , $ h )
259+ {
260+ return $ this ->browser ->get ($ this ->url ('/exec/%s/resize?w=%u&h=%u ' , $ exec , $ w , $ h ))->then (array ($ this ->parser , 'expectEmpty ' ));
261+ }
262+
218263 private function url ($ url )
219264 {
220265 $ args = func_get_args ();
221266 array_shift ($ args );
222267
223268 return $ this ->url . vsprintf ($ url , $ args );
224269 }
270+
271+ private function postJson ($ url , $ data )
272+ {
273+ $ body = $ this ->json ($ data );
274+ $ headers = array ('Content-Type ' => 'application/json ' , 'Content-Length ' => strlen ($ body ));
275+
276+ return $ this ->browser ->post ($ url , $ headers , $ body );
277+ }
278+
279+ private function json ($ data )
280+ {
281+ if ($ data === array ()) {
282+ return '{} ' ;
283+ }
284+ return json_encode ($ data );
285+ }
225286}
0 commit comments