Skip to content

Instantly share code, notes, and snippets.

@bryanhuntesl
Last active September 25, 2018 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanhuntesl/4c32140fa55e55d063e79719531df6a2 to your computer and use it in GitHub Desktop.
Save bryanhuntesl/4c32140fa55e55d063e79719531df6a2 to your computer and use it in GitHub Desktop.

dockerex

socat -d TCP-LISTEN:2376,range=127.0.0.1/32,reuseaddr,fork UNIX:/run/docker.sock &
# curl localhost:2376/containers/json
[{"Id":"47d8a2db2b0b64408f23e65d28e10a44d90b029b29b89b1427407bfddad8a61e","Names":["/objective_jepsen"],....
iex(10)> req_1="http+unix://" <> URI.encode_www_form("/run/docker.sock") <> "/containers/json"
"http+unix://%2Frun%2Fdocker.sock/containers/json"
iex(9)> HTTPoison.get req_1
{:ok,
 %HTTPoison.Response{body: "[{\"Id\":\"47d8a2db2b0b64408f23e65d28e10a44d90b029b29b89b1427407bfddad8a61e\",\"Names\":[\"/objective_jepsen\"],\"Image\":\"elixir\",\"ImageID\":\"sha256:97dbd18772c407993ca62b44a3526d925b57581125d53ee28871a73eee062c92\",\"Command\":\"/bin/bash\",\"Created\":1509120018,\"Ports\":[],\"Labels\":{},\"State\":\"running\",\"Status\":\"Up 23 minutes\",\"HostConfig\":{\"NetworkMode\":\"default\"},\"NetworkSettings\":{\"Networks\":{\"bridge\":{\"IPAMConfig\":null,\"Links\":null,\"Aliases\":null,\"NetworkID\":\"b0b7e48e35f3e101851a9a7f68709458f7c1f0d3fe956b68d02e01f5c5cc40ec\",\"EndpointID\":\"d32c2fb7294b4976a33242aff02974c74607a58f8f91354aaf7280fe4c1efda6\",\"Gateway\":\"172.17.0.1\",\"IPAddress\":\"172.17.0.2\",\"IPPrefixLen\":16,\"IPv6Gateway\":\"\",\"GlobalIPv6Address\":\"\",\"GlobalIPv6PrefixLen\":0,\"MacAddress\":\"02:42:ac:11:00:02\",\"DriverOpts\":null}}},\"Mounts\":[{\"Type\":\"bind\",\"Source\":\"/var/run/docker.sock\",\"Destination\":\"/var/run/docker.sock\",\"Mode\":\"\",\"RW\":true,\"Propagation\":\"rprivate\"},{\"Type\":\"bind\",\"Source\":\"/Users/bryanhunt/tmpdir\",\"Destination\":\"/foo\",\"Mode\":\"\",\"RW\":true,\"Propagation\":\"rprivate\"}]}]\n",
  headers: [{"Api-Version", "1.33"}, {"Content-Type", "application/json"},
   {"Docker-Experimental", "true"}, {"Ostype", "linux"},
   {"Server", "Docker/17.10.0-ce (linux)"},
   {"Date", "Fri, 27 Oct 2017 16:23:22 GMT"}, {"Content-Length", "1042"}],
  request_url: "http+unix://%2Frun%2Fdocker.sock/containers/json",
  status_code: 200}}
body = """
 {
    "AttachStdin": false,
     "AttachStdout": true,
    "AttachStderr": true,
    "DetachKeys": "ctrl-p,ctrl-q",
    "Tty": false,
    "Cmd": [ "date" ],
    "Env": [ "FOO=bar" ]
 }
"""
req_1="http+unix://" <> URI.encode_www_form("/run/docker.sock") <> "/containers/47d8a2db2b0b/exec"
HTTPoison.post(req_1,body)
{:ok,
 %HTTPoison.Response{body: "{\"message\":\"Content-Type specified (application/octet-stream) must be 'application/json'\"}\n",
  headers: [{"Api-Version", "1.33"}, {"Content-Type", "application/json"},
   {"Docker-Experimental", "true"}, {"Ostype", "linux"},
   {"Server", "Docker/17.10.0-ce (linux)"},
   {"Date", "Fri, 27 Oct 2017 16:35:49 GMT"}, {"Content-Length", "91"}],
  request_url: "http+unix://%2Frun%2Fdocker.sock/containers/47d8a2db2b0b/exec",
  status_code: 400}}
socat -v UNIX-LISTEN:/tmp/fake,fork UNIX-CONNECT:/var/run/docker.sock

-v : writes the traffic to stderr as text in addition to relaying instructions. Some conversions are made for the sake of readability so if certain sequences aren’t being interpreted properly, one could try -x (hex).
UNIX-LISTEN : listen for connections on the unix socket (In our case, /tmp/fake)
fork : create a separate subprocess for handling new connections so the main process may continue listening
UNIX-CONNECT : connect to the specified unix socket (In our case, /var/run/docker.sock)

Once that’s been started, it’s just a matter of ensuring your docker commands go through ‘unix:///tmp/fake’.

$ export DOCKER_HOST=unix:///tmp/fake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment