How to build an Image using Docker API?

I am trying to build a docker Image using the APIs mentioned in the official link.

But I am unable to get what the document says:
Example request:

POST /v1.24/build HTTP/1.1


{{ TAR STREAM }}

There are few things which are confusing me here,

  1. Where to mention the path for the Dockerfile?
  2. "The archive must include a build instructions file, typically called Dockerfile at the archive’s root." This statement from docker docs, the confusion here is what is archive’s root?
  3. What is the command in detail for building an Image?

This has worked for me, docker version : 17.04
Sample Dockerfile:

# cat Dockerfile

FROM ubuntu:14.04
RUN mkdir demo
RUN apt-get update
RUN apt-get -y install vim

Create a tar file which includes your Dockerfile.

# tar -cvf Dockerfile.tar.gz Dockerfile 

Execute the API as below and for more options, refer this.

# curl -v -X POST -H "Content-Type:application/tar" --data-binary '@Dockerfile.tar.gz' http://127.0.0.1:5000/build?t=build_test
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /build?t=build_test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Type:application/tar
> Content-Length: 10240
> Expect: 100-continue
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Api-Version: 1.28
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
* Server Docker/17.04.0-ce (linux) is not blacklisted
< Server: Docker/17.04.0-ce (linux)
< Date: Mon, 01 May 2017 09:56:54 GMT
< Transfer-Encoding: chunked
< 
{"stream":"Step 1/4 : FROM ubuntu:14.04\n"}
{"stream":" ---\u003e 302fa07d8117\n"}
{"stream":"Step 2/4 : RUN mkdir demo\n"}
{"stream":" ---\u003e Running in 5941f911099e\n"}
{"stream":" ---\u003e bccbffdd776a\n"}
{"stream":"Removing intermediate container 5941f911099e\n"}
{"stream":"Step 3/4 : RUN apt-get update\n"}
{"stream":" ---\u003e Running in 7aefd190a165\n"}
{"stream":"Removing intermediate container 7aefd190a165\n"}
{"stream":"Step 4/4 : RUN apt-get -y install vim\n"}
{"stream":" ---\u003e Running in e9ddad5a4695\n"}
{"stream":"Removing intermediate container e9ddad5a4695\n"}
{"stream":"Successfully built b1736dd9b698\n"}
*  Connection #0 to host 127.0.0.1 left intact

Check the docker images after the image is successfully built.

# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
build_test                       latest              b1736dd9b698        8 seconds ago  

Removed some unnecessary output.

To configure docker daemon port , refer this

1 Like

You’ve called your file Dockerfile.tar.gz but in reality you’re command creates an uncompressed TAR file. This is the correct procedure but the filename can mislead people into thinking you’ve gzipped the archive.