Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save markjlorenz/7733a6486f02d21f68053c0c1e43431f to your computer and use it in GitHub Desktop.
Save markjlorenz/7733a6486f02d21f68053c0c1e43431f to your computer and use it in GitHub Desktop.
Using the exec command with the docker API, and capturing output
#! /usr/bin/env bash
# Create the container with a long running process. If PID1 exists before
# we send the exec commands, they will fail because the container is not running
#
CONTAINER_NAME="TEST-`uuidgen`"
curl --silent --unix-socket /var/run/docker.sock "http:/containers/create?name=${CONTAINER_NAME}" -XPOST \
-H "Content-Type: application/json" \
-d '{
"Image": "ruby:latest",
"Cmd": [ "sleep", "10" ]
}' | jq '.'
# Start the container
#
curl --silent --unix-socket /var/run/docker.sock "http:/containers/${CONTAINER_NAME}/start" -XPOST \
-H "Content-Type: application/json" \
--output /dev/null \
--write-out "%{http_code}"
echo ""
# Create the exec
#
EXEC_CREATE=`curl --silent --unix-socket /var/run/docker.sock "http:/containers/${CONTAINER_NAME}/exec" -XPOST \
-H "Content-Type: application/json" \
-d '{
"AttachStdout": true,
"Tty": true,
"Cmd": [ "ruby", "-e", "5.times { puts :hi; sleep 1 }"]
}'
`
echo $EXEC_CREATE | jq '.'
# Run the exec
#
EXEC_ID=$(echo $EXEC_CREATE | jq -r '.Id')
curl --silent --unix-socket /var/run/docker.sock "http:/exec/${EXEC_ID}/start" -XPOST \
-H "Content-Type: application/json" \
-d '{
"Detach": false,
"Tty": true
}'
# Or use the logging API to attach to the container, and delete the JSON
# from `start`
#
# curl --silent --unix-socket /var/run/docker.sock "http:/containers/${CONTAINER_NAME}/attach?stream=1&stdout=1&stderr=1" -XPOST
@glafir
Copy link

glafir commented Apr 29, 2021

How to suggest this problem for long exec operation?

curl: (18) transfer closed with outstanding read data remaining

if sleep more 60 second for example in our case ?

@glafir
Copy link

glafir commented Apr 29, 2021

This is must help

curl --no-buffer --max-time 3700 -k -sS -XPOST -H "Content-Type: application/json" -H "Expect: " http://host

And Check proxy timeout variable, if API docker is behind proxy. For example nginx

          proxy_connect_timeout 30s;
          proxy_send_timeout 3700s;
          proxy_read_timeout 3700s;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment