This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

First, generate a mirror

Before setting up the container to run the project, we need to build an image using dockerFile.

This will be done using the Web, mimicking the Docker build.

tar cvf .. /Dockerfile.tar *Copy the code
  • This command is used to compress files in the Windows operating system. After being executed, all files in the current directory are compressed and stored in the parent directory.

  • In addition to Dockerfile, package the required configuration files or template files together.

(If you only have dockerfiles, package them into tar, otherwise the Docker Daemon won’t recognize them.)

Compressed files:

  • To get started, let’s do a request simulation using Postman:

  • Use POST request \build to generate an image:

  • In this request, the URL can be followed by parameters: for example, t represents the label and version number of the image in the format label:version. Remote can be downloaded from the far end (git) above Dockerfile file, and some set the parameters of CPU and memory, there won’t be opened, details refer to the document: docs.docker.com/engine/api/…

  • Upload the newly packaged tar compression package using a stream file. The content-type in the Header part is automatically changed to application/x-tar.

Send Sends the request, waits for some time (I have many configuration items and wait for more than two minutes), and returns 200.

Back on the server, look at the image using Docker Images and see that the image has been created successfully.

Now that we have successfully verified that the request is valid, it is easy to reproduce using Golang’s NET/HTTP library:

package main
​
import (
   "bytes"
   "fmt"
   "io/ioutil"
   "net/http"
)
​
func main(a) {
   url := "Http://192.168.104.124:6666/build? t=golang_image"
​
   content, err := ioutil.ReadFile("./dockerfile.tar")
   iferr ! =nil {
      panic(err)
   }
​
   req, err := http.NewRequest("POST", url, bytes.NewBuffer(content))
   req.Header.Set("Content-Type"."application/x-tar")
​
   client := &http.Client{}
   resp, err := client.Do(req)
   iferr ! =nil{
      panic(err)
   }
   defer resp.Body.Close()
​
   fmt.Println("status", resp.Status)
   body, _ := ioutil.ReadAll(resp.Body)
   fmt.Println("response Body:".string(body))
}
Copy the code

(Changed to an almost minimalist Dockerfile for testing, otherwise it would take time to hh)

The following packet result is returned:

Images also added successfully (without specifying the version number in the t argument, it defaults to latest).

2. Generate the container

  • The request address to generate the container is /containers/create, generated using a POST request, with only optional input in the URL parameter section, which specifies the name of the container (the name can only consist of underscores, numbers, and letters).

  • The other parameters are sent in JSON format. The longest parameter used is Image, which specifies the base Image used to build the container.

  • Depending on your needs, you may also use Entrypoint, Env, and other parameters for container configuration.

  • Use the function of each parameter can reference: docs.docker.com/engine/api/…

POST http://192.168.104.124:6666/containers/create?name=golang_container HTTP / 1.1
Content-Type: application/json
​
Image=golang_image
Copy the code
  • Send an HTTP request like this (the code is much the same as above, so I won’t show it), and docker generates a container based on the image:

  • As you can see, in the returned packet, the Id of the newly generated container is also returned.
status 201 Created
{
  "Id":"7415ed4549b9aba8503edaaf7872e0a5c99304304e804a08c2362a2a6c95c201",
  "Warnings":[]
}
Copy the code
  • However, this container is not available yet and needs to be started to run:

The POST request /containers/{id}/start will return 204 if the operation is correct:

status 204 No Content
Copy the code
  • A personal test: it is possible to write the first character of the ID without any conflict.

  • It is also possible to successfully replace the id with the container name (just like on the Docker command line).

The above operation is equivalent todocker runDistributed execution of commands.

Third, further expansion

  • docker runThe command format is as follows:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Copy the code
  • Sometimes we need to configure the container with the [OPTIONS] and [COMMAND] arguments.

    • OPTIONS can be found in the request case (except for the –name in the URL parameter) given by the specification.

    • The COMMAND argument is much simpler, passing in the request argument Cmd an array of strings for each COMMAND argument.

In THE GO language, packet generation can be realized quickly by converting struct to JSON:

Container := container {Image: "golang_image:latest", Privileged: true, Dns: []string{"8.8.8.8"}, Cmd: []string{"/dev/ttyUSB1"}, } content, _ := json.Marshal(container)Copy the code

The sending request is the same as before, so I won’t mention it.