Skip to content

Commit

Permalink
Add curl examples
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Firshman <ben@firshman.co.uk>
  • Loading branch information
bfirsh committed Oct 18, 2016
1 parent 23bd0aa commit c7b6169
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion _data/toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ toc:
- sectiontitle: Docker Remote API
section:
- path: /api/
title: Overview of Docker Remote API
title: Overview
- path: /api/getting-started/
title: Getting Started
- path: /api/sdks/
Expand Down
104 changes: 102 additions & 2 deletions api/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This is the equivalent of doing `docker run alpine echo hello world`:
<dl class="horizontal tabs" data-tab >
<dd class="active"><a href="#tab-run-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-run-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-run-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-run-python">
Expand Down Expand Up @@ -78,20 +79,37 @@ func main() {
}
{% endhighlight %}
</section>
<section class="content" id="tab-run-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" \
-d '{"Image": "alpine", "Cmd": ["echo", "hello world"]}' \
-X POST http:/v1.24/containers/create
{"Id":"1c6594faf5","Warnings":null}

$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/start

$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/wait
{"StatusCode":0}

$ curl --unix-socket /var/run/docker.sock "http:/v1.24/containers/1c6594faf5/logs?stdout=1"
hello world
{% endhighlight %}
</section>
</div>

You can also run containers in the background, the equivalent of `docker run -d bfirsh/reticulate-splines`:

<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-rundetach-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-rundetach-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-rundetach-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-rundetach-python">
{% highlight python %}
import docker
client = docker.from_env()
container = client.containers.run("alpine", ["echo", "hello", "world"], detach=True)
container = client.containers.run("bfirsh/reticulate-splines", detach=True)
print container.id
{% endhighlight %}
</section>
Expand Down Expand Up @@ -140,6 +158,16 @@ func main() {
}
{% endhighlight %}
</section>
<section class="content" id="tab-rundetach-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" \
-d '{"Image": "bfirsh/reticulate-splines"}' \
-X POST http:/v1.24/containers/create
{"Id":"1c6594faf5","Warnings":null}

$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/start
{% endhighlight %}
</section>
</div>

## Listing and managing containers
Expand All @@ -149,6 +177,7 @@ Like `docker ps`, we can use the API to list containers that are running:
<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-listcontainers-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-listcontainers-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-listcontainers-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-listcontainers-python">
Expand Down Expand Up @@ -186,6 +215,18 @@ func main() {
fmt.Println(container.ID)
}
}
{% endhighlight %}
</section>
<section class="content" id="tab-listcontainers-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json
[{
"Id":"ae63e8b89a26f01f6b4b2c9a7817c31a1b6196acf560f66586fbc8809ffcd772",
"Names":["/tender_wing"],
"Image":"bfirsh/reticulate-splines",
...
}]

{% endhighlight %}
</section>
</div>
Expand All @@ -195,6 +236,7 @@ Now we know what containers exist, we can perform operations on them. For exampl
<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-stopcontainers-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-stopcontainers-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-stopcontainers-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-stopcontainers-python">
Expand Down Expand Up @@ -234,6 +276,21 @@ func main() {
}
}
}
{% endhighlight %}
</section>
<section class="content" id="tab-stopcontainers-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json
[{
"Id":"ae63e8b89a26f01f6b4b2c9a7817c31a1b6196acf560f66586fbc8809ffcd772",
"Names":["/tender_wing"],
"Image":"bfirsh/reticulate-splines",
...
}]

$ curl --unix-socket /var/run/docker.sock \
-X POST http:/v1.24/containers/ae63e8b89a26/stop

{% endhighlight %}
</section>
</div>
Expand All @@ -243,6 +300,7 @@ We can also perform actions on individual containers. For example, to print the
<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-containerlogs-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-containerlogs-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-containerlogs-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-containerlogs-python">
Expand Down Expand Up @@ -283,6 +341,16 @@ func main() {
}
{% endhighlight %}
</section>
<section class="content" id="tab-containerlogs-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock "http:/v1.24/containers/ca5f55cdb/logs?stdout=1"
Reticulating spline 1...
Reticulating spline 2...
Reticulating spline 3...
Reticulating spline 4...
Reticulating spline 5...
{% endhighlight %}
</section>
</div>

## Managing images
Expand All @@ -292,6 +360,7 @@ Images are the basis of containers, and can be managed in a similar way. You can
<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-listimages-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-listimages-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-listimages-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-listimages-python">
Expand Down Expand Up @@ -331,13 +400,24 @@ func main() {
}
{% endhighlight %}
</section>
<section class="content" id="tab-listimages-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock http:/v1.24/images/json
[{
"Id":"sha256:31d9a31e1dd803470c5a151b8919ef1988ac3efd44281ac59d43ad623f275dcd",
"ParentId":"sha256:ee4603260daafe1a8c2f3b78fd760922918ab2441cbb2853ed5c439e59c52f96",
...
}]
{% endhighlight %}
</section>
</div>

You can pull images, like `docker pull`:

<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-pullimages-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-pullimages-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-pullimages-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-pullimages-python">
Expand Down Expand Up @@ -377,13 +457,24 @@ func main() {
}
{% endhighlight %}
</section>
<section class="content" id="tab-pullimages-curl">
{% highlight bash %}
$ curl --unix-socket /var/run/docker.sock \
-X POST "http:/v1.24/images/create?fromImage=alpine"
{"status":"Pulling from library/alpine","id":"3.1"}
{"status":"Pulling fs layer","progressDetail":{},"id":"8f13703509f7"}
{"status":"Downloading","progressDetail":{"current":32768,"total":2244027},"progress":"[\u003e ] 32.77 kB/2.244 MB","id":"8f13703509f7"}
...
{% endhighlight %}
</section>
</div>

And commit containers to create images from their contents:

<dl class="horizontal tabs" data-tab>
<dd class="active"><a href="#tab-commit-python" class="noanchor">Python</a></dd>
<dd><a href="#tab-commit-go" class="noanchor">Go</a></dd>
<dd><a href="#tab-commit-curl" class="noanchor">curl</a></dd>
</dl>
<div class="tabs-content">
<section class="content active" id="tab-commit-python">
Expand Down Expand Up @@ -432,7 +523,7 @@ func main() {
panic(err)
}

commitResp, err := cli.ContainerCommit(ctx, createResp.ID, types.ContainerCommitOptions{})
commitResp, err := cli.ContainerCommit(ctx, createResp.ID, types.ContainerCommitOptions{Reference: "helloworld"})
if err != nil {
panic(err)
}
Expand All @@ -441,4 +532,13 @@ func main() {
}
{% endhighlight %}
</section>
<section class="content" id="tab-commit-curl">
{% highlight bash %}
$ docker run -d alpine touch /helloworld
0888269a9d584f0fa8fc96b3c0d8d57969ceea3a64acf47cd34eebb4744dbc52
$ curl --unix-socket /var/run/docker.sock\
-X POST "http:/v1.24/commit?container=0888269a9d&repo=helloworld"
{"Id":"sha256:6c86a5cd4b87f2771648ce619e319f3e508394b5bfc2cdbd2d60f59d52acda6c"}
{% endhighlight %}
</section>
</div>

0 comments on commit c7b6169

Please sign in to comment.