用于应用程序代码更新的远程API

时间:2016-06-03 18:05:28

标签: docker

到目前为止,我正在使用java客户端并使用远程api进行有限的实现。我的目标是使用自动图像来旋转多个容器,然后动态地使这些容器直接访问具有开发人员代码的主机。在这种情况下,它是一个运行Tomcat和.war文件的容器。

我不想在图像中包含.war文件,因为这会创建数百个图像。通过命令行,我可以将.war复制到一个容器中,这可能没问题但是在远程api中似乎没有选项。理想情况下,我想为容器提供可用的挂载点,并且开发人员可以不断更新其代码。如果这可能会很好,但到目前为止我还没有找到合适的组合。我尝试过HostConfig:绑定:[“/ host / path:/ container / path”]显示在容器信息中,但似乎没有按照我的意愿行事。任何和所有的帮助将非常感激。

下面是我用来创建容器的java方法。

public int doIt(){

    int ri = 0;

    JSONObject object = new JSONObject();
    JSONObject aObject = new JSONObject();
    JSONObject bObject = new JSONObject();

    JSONArray list = new JSONArray();

    try {
    object.put("Image", "test/tomcat");

    aObject.put("/home/mkwalter/tomcat/webapps", new JSONObject());
    object.put("Volumes", aObject);

    list.add("/home/mkwalter/tomcat/webapps:/usr/local/tomcat/webapps");
    bObject.put("Binds",list);
    object.put("HostConfig", bObject);



    System.out.println("object.toString before try " + object.toString());

    } catch (Exception e) {
        System.out.println(e);
    }

    try {

        HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead 

         HttpPost request = new HttpPost("http://192.168.1.97:4243/containers/create?name=");
         StringEntity params = new StringEntity(object.toString());
         request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            System.out.println("Response Code : " 
                    + response.getStatusLine().getStatusCode());
            ri = response.getStatusLine().getStatusCode();

    } catch (Exception ex){
        System.out.println(ex);
    }
    return ri;
}


下面是对方法创建的容器的检查。

[
{
    "Id": "d725ec70fd0edb012850f031faf6e50c7362491db1165c7b69684259529d124c",
    "Created": "2016-06-05T00:40:56.0745768Z",
    "Path": "catalina.sh",
    "Args": [
        "run"
    ],
    "State": {
        "Status": "created",
        "Running": false,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 0,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "0001-01-01T00:00:00Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "sha256:50630603d091a8a26abbf7e136a588a956a236a3f0ee4433d3bdb4968987b22a",
    "ResolvConfPath": "",
    "HostnamePath": "",
    "HostsPath": "",
    "LogPath": "",
    "Name": "/romantic_dijkstra",
    "RestartCount": 0,
    "Driver": "aufs",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": {
        "Binds": [
            "/home/mkwalter/tomcat/webapps:/usr/local/tomcat/webapps"
        ],
        "ContainerIDFile": "",
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "NetworkMode": "default",
        "PortBindings": null,
        "RestartPolicy": {
            "Name": "",
            "MaximumRetryCount": 0
        },
        "AutoRemove": false,
        "VolumeDriver": "",
        "VolumesFrom": null,
        "CapAdd": null,
        "CapDrop": null,
        "Dns": null,
        "DnsOptions": null,
        "DnsSearch": null,
        "ExtraHosts": null,
        "GroupAdd": null,
        "IpcMode": "",
        "Cgroup": "",
        "Links": null,
        "OomScoreAdj": 0,
        "PidMode": "",
        "Privileged": false,
        "PublishAllPorts": false,
        "ReadonlyRootfs": false,
        "SecurityOpt": null,
        "StorageOpt": null,
        "UTSMode": "",
        "UsernsMode": "",
        "ShmSize": 67108864,
        "ConsoleSize": [
            0,
            0
        ],
        "Isolation": "",
        "CpuShares": 0,
        "Memory": 0,
        "CgroupParent": "",
        "BlkioWeight": 0,
        "BlkioWeightDevice": null,
        "BlkioDeviceReadBps": null,
        "BlkioDeviceWriteBps": null,
        "BlkioDeviceReadIOps": null,
        "BlkioDeviceWriteIOps": null,
        "CpuPeriod": 0,
        "CpuQuota": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "Devices": null,
        "DiskQuota": 0,
        "KernelMemory": 0,
        "MemoryReservation": 0,
        "MemorySwap": 0,
        "MemorySwappiness": -1,
        "OomKillDisable": false,
        "PidsLimit": 0,
        "Ulimits": null,
        "CpuCount": 0,
        "CpuPercent": 0,
        "BlkioIOps": 0,
        "BlkioBps": 0,
        "SandboxSize": 0
    },
    "GraphDriver": {
        "Name": "aufs",
        "Data": null
    },
    "Mounts": [
        {
            "Source": "/home/mkwalter/tomcat/webapps",
            "Destination": "/usr/local/tomcat/webapps",
            "Mode": "",
            "RW": true,
            "Propagation": "rprivate"
        },
        {
            "Name": "0bc51a207c05837c276dce77d697412715616c8353cb8d6fef3cfcb3ba5b3d3e",
            "Source": "/var/lib/docker/volumes/0bc51a207c05837c276dce77d697412715616c8353cb8d6fef3cfcb3ba5b3d3e/_data",
            "Destination": "/home/mkwalter/tomcat/webapps",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],
    "Config": {
        "Hostname": "d725ec70fd0e",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": {
            "8080/tcp": {}
        },
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "PATH=/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=C.UTF-8",
            "JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre",
            "JAVA_VERSION=7u101",
            "JAVA_DEBIAN_VERSION=7u101-2.6.6-1~deb8u1",
            "CATALINA_HOME=/usr/local/tomcat",
            "TOMCAT_MAJOR=7",
            "TOMCAT_VERSION=7.0.69",
            "TOMCAT_TGZ_URL=https://www.apache.org/dist/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz"
        ],
        "Cmd": [
            "catalina.sh",
            "run"
        ],
        "Image": "test/tomcat",
        "Volumes": {
            "/home/mkwalter/tomcat/webapps": {}
        },
        "WorkingDir": "/usr/local/tomcat",
        "Entrypoint": null,
        "OnBuild": null,
        "Labels": {}
    },
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": null,
        "SandboxKey": "",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "",
        "Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "",
        "IPPrefixLen": 0,
        "IPv6Gateway": "",
        "MacAddress": "",
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "",
                "EndpointID": "",
                "Gateway": "",
                "IPAddress": "",
                "IPPrefixLen": 0,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": ""
            }
        }
    }
}
]

抱歉占用了这么多空间。

java创建的字符串在下面。

{"Image":"test\/tomcat","HostConfig":{"Binds":["\/home\/mkwalter\/tomcat\/webapps:\/usr\/local\/tomcat\/webapps"]},"Volumes":{"\/home\/mkwalter\/tomcat\/webapps":{}}}

回应代码:201

1 个答案:

答案 0 :(得分:0)

您当前的JSON格式为

{
  "Image": "test/tomcat",
  "HostConfig": {
    "Binds": [
      "/home/mkwalter/tomcat/webapps:/usr/local/tomcat/webapps"
    ]
  },
  "Volumes": {
    "/home/mkwalter/tomcat/webapps": {

    }
  }
}

您应该发布格式为

的请求
{
  "Image": "test/tomcat",
  "HostConfig": {
    "Binds": [
      "/home/mkwalter/tomcat/webapps:/usr/local/tomcat/webapps"
    ]
  },
  "Volumes": {
    "/usr/local/tomcat/webapps":{}
  }
}

这是您需要修复此代码的更新

JSONObject object = new JSONObject();
    JSONObject aObject = new JSONObject();
    JSONObject bObject = new JSONObject();

    JSONArray list = new JSONArray();

    try {
    object.put("Image", "test/tomcat");

    aObject.put("/usr/local/tomcat/webapps", new JSONObject());
    object.put("Volumes", aObject);

    list.add("/home/mkwalter/tomcat/webapps:/usr/local/tomcat/webapps");
    bObject.put("Binds",list);
    object.put("HostConfig", bObject);