Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote API for docker exec commands

Tags:

docker

Could someone please help me with remote api for docker exec to run a command?

I am able to run it directly:

# docker exec 941430a3060c date
Fri Apr 29 05:18:03 UTC 2016
like image 446
Jayashree Ramamoorthy Avatar asked Jul 28 '26 23:07

Jayashree Ramamoorthy


2 Answers

You have to follow below steps. I am adding rest api way. You can code it to any language.

  1. EXEC CREATE

POST /containers/(id or name)/exec

Body

{
  "AttachStdin": false,
  "AttachStdout": true,
  "AttachStderr": true,
  "Cmd": ["bash","-c","echo '*/5 * * * * /usr/bin/php /app/yii cronrun/job001'>> /var/spool/cron/crontabs/root"]
}

You have to select Content-Type: application/json

when you POST the above data you will get id in response. You have to use the id in next step.

*The above will add cron job in container which will execute every five minute.


  1. EXEC START

POST /exec/(id)/start

{
 "Detach": false,
 "Tty": false
}

You have to select Content-Type: application/json

Check response code, 200 is OK

like image 51
Hikmat Avatar answered Jul 31 '26 13:07

Hikmat


The API section which would help you is: Exec Create/Start

You can see some example in integration-cli/docker_api_exec_test.go

  • create:

    sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
    
  • start:

    sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
    
  • inspect:

    sockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "")
    
like image 33
VonC Avatar answered Jul 31 '26 13:07

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!