3.2 centos6配置Http Api远程操作Docker镜

2018-10-17  本文已影响0人  Guoyubo

目的

配置docker所在的服务器,使得可通过http远程访问操作服务器docker

环境

Linux:CentOS release 6.10 (Final)
docker: 1.7

环境很重要,CentOS 6和CentOS 7的配置文件路径是不一样的,有些配置文件找不到,配置也不一样!!!
本篇说的是CentOS6


图片.png

配置方法

1修改配置文件/etc/sysconfig/docker里的配置
vim /etc/sysconfig/docker
把原来的other_args=
改为
other_args="-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375"

图片.png
网上说的啥配置DOCKER_OPTS="-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375"的都没用

2 重启docker使配置生效
service docker restart
即使此时你有docker镜像正在运行,也可以重启docker的,不影响,不会把你正在运行的docker断掉的
检查下2375端口是不是在监听状态,有就对了,外网访问,防火墙注意下
netstat -nap | grep 2375

图片.png
3 浏览器访问服务器的docker
http://xxxxx.com:2375/info
2375端口是你配置的端口,可以随便改的
图片.png

HTTP API 如何创建镜像?

官网API:https://docs.docker.com/engine/api/v1.29/#section/Versioning
1.25版本之后的api样式和原来不一样了,最新的更好看点

docker版本与api版本对应关系

我应该用1.26的


图片.png
例子:

前提 Content-Type: application/json

1创建容器

POST /containers/create
创建镜像其实主要是配置HostConfig
配置10000端口映射docker内部的8080端口,得配ExposedPorts属性
配置挂载目录/tmp:/tmp,后面是docker内部的目录,且必须都是绝对路径
官方解释:host-src:container-dest to bind-mount a host path into the container. Both host-src, and container-dest must be an absolute path.

{
    "MemorySwap": 0,
    "AttachStdin": false,
    "AttachStdout": true,
    "AttachStderr": true,
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": null,
    "Image": "docker.io/tomcat:latest",
    "WorkingDir": "",
    "ExposedPorts": {"8080/tcp": { }},
    "HostConfig": {
        "Binds": ["/test:/usr/local/tomcat/logs"],
        "PortBindings": {
            "8080/tcp": [ {"HostPort": "5005"} ]
        }
    }
}
图片.png

2容器重命名

我在创建的时候没找到直接命名的参数,所以只能先创建,再重命名
POST
http://192.168.0.243:2375/v1.13/containers/658b5801d60ecd4dd1fc2a9416bbdf626f6fbac5f20db432c910e01dd41e2d30/rename?name=tomcat5000

3查看容器详情

GET
http://192.168.0.243:2375/v1.25/containers/tomcat5000/json
结果

{
    "Id": "98a9ae018a03b471be2c1415498f876c461beedb9d3d703bd61330be374aa742",
    "Created": "2018-11-12T08:08:28.47363772Z",
    "Path": "catalina.sh",
    "Args": [
        "run"
    ],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 22403,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2018-11-12T08:19:58.858983084Z",
        "FinishedAt": "2018-11-12T08:19:44.6429067Z"
    },
    "Image": "sha256:6759d91a032b85a78353424a993101154f9696ced5be11718dd52879efc7e1c1",
    "ResolvConfPath": "/var/lib/docker/containers/98a9ae018a03b471be2c1415498f876c461beedb9d3d703bd61330be374aa742/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/98a9ae018a03b471be2c1415498f876c461beedb9d3d703bd61330be374aa742/hostname",
    "HostsPath": "/var/lib/docker/containers/98a9ae018a03b471be2c1415498f876c461beedb9d3d703bd61330be374aa742/hosts",
    "LogPath": "",
    "Name": "/tomcat5000",
    "RestartCount": 0,
    "Driver": "overlay2",
    "MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c29,c330",
    "ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c29,c330",
    "AppArmorProfile": "",
    "ExecIDs": [
        "7d7d6cf51d1f617bb886b29eccb20f74b05fe440f3365821b29af27b01a1ef22"
    ],
    "HostConfig": {
        "Binds": [
            "/tmp/host:/tmp"
        ],
        "ContainerIDFile": "",
        "LogConfig": {
            "Type": "journald",
            "Config": {}
        },
        "NetworkMode": "default",
        "PortBindings": {
            "8080/tcp": [
                {
                    "HostIp": "",
                    "HostPort": "5000"
                }
            ]
        },
        "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,
        "UTSMode": "",
        "UsernsMode": "",
        "ShmSize": 67108864,
        "Runtime": "docker-runc",
        "ConsoleSize": [
            0,
            0
        ],
        "Isolation": "",
        "CpuShares": 0,
        "Memory": 0,
        "NanoCpus": 0,
        "CgroupParent": "",
        "BlkioWeight": 0,
        "BlkioWeightDevice": null,
        "BlkioDeviceReadBps": null,
        "BlkioDeviceWriteBps": null,
        "BlkioDeviceReadIOps": null,
        "BlkioDeviceWriteIOps": null,
        "CpuPeriod": 0,
        "CpuQuota": 0,
        "CpuRealtimePeriod": 0,
        "CpuRealtimeRuntime": 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,
        "IOMaximumIOps": 0,
        "IOMaximumBandwidth": 0
    },
    "GraphDriver": {
        "Name": "overlay2",
        "Data": {
            "LowerDir": "/var/lib/docker/overlay2/994e4c62e3f7af2704a350919bd6f652e4c808e0ce606bfc6dae77106ab468b2-init/diff:/var/lib/docker/overlay2/69303a74ba91199bff3d6a17831546adfc71a0aa32b7ea02e7ba691447eedd11/diff:/var/lib/docker/overlay2/9869e4fecbbf57601ce760a67512cc311fe5298a8e5cd171a6a849f4d04442c2/diff:/var/lib/docker/overlay2/740c060abf34f68fdde4b35ed64982a82d107b07f28dd3f681338bbe91dc64a9/diff:/var/lib/docker/overlay2/496ca9d192609293b1df2ba09dac3377dccd913b8af74f15340069d0b6b3eb57/diff:/var/lib/docker/overlay2/f38bc8eac5ef46af0b6298f73b8edb00eae2af0e90aa5ba4825a1ffca7f0a2ba/diff:/var/lib/docker/overlay2/9b3290aa7995d85837d0d85289a3077b5c01bae8f8c1a68372357c2d2bc71834/diff:/var/lib/docker/overlay2/2452ecb6e9842120fc7fe97903958ae0004ebdd805102fe5ee355896f191fd7b/diff:/var/lib/docker/overlay2/ce4c281daa1108a05497308f0f65cd7830b46d0d64dffcc2ea950c9d78631e3f/diff:/var/lib/docker/overlay2/38263d4aaa05e7505be072e05536e9aa24a03def6d1683a02c8a2feba37bf897/diff:/var/lib/docker/overlay2/0b18831500fdbb182dfb7025d30e3ce0e20e949e4fe99eb31b0ec54d726b2ad4/diff:/var/lib/docker/overlay2/59871599bdc603e0a002cfbb9759114f4c086321fb1f51bd73507a67d0c947dc/diff:/var/lib/docker/overlay2/7b1f5da7a77d77eb3f49f5117d3c1f52819b957ca2e448ef8db83e52b1848463/diff",
            "MergedDir": "/var/lib/docker/overlay2/994e4c62e3f7af2704a350919bd6f652e4c808e0ce606bfc6dae77106ab468b2/merged",
            "UpperDir": "/var/lib/docker/overlay2/994e4c62e3f7af2704a350919bd6f652e4c808e0ce606bfc6dae77106ab468b2/diff",
            "WorkDir": "/var/lib/docker/overlay2/994e4c62e3f7af2704a350919bd6f652e4c808e0ce606bfc6dae77106ab468b2/work"
        }
    },
    "Mounts": [
        {
            "Type": "bind",
            "Source": "/tmp/host",
            "Destination": "/tmp",
            "Mode": "",
            "RW": true,
            "Propagation": "rprivate"
        }
    ],
    "Config": {
        "Hostname": "98a9ae018a03",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": true,
        "AttachStderr": true,
        "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=/docker-java-home/jre",
            "JAVA_VERSION=8u181",
            "JAVA_DEBIAN_VERSION=8u181-b13-2~deb9u1",
            "CA_CERTIFICATES_JAVA_VERSION=20170531+nmu1",
            "CATALINA_HOME=/usr/local/tomcat",
            "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
            "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
            "OPENSSL_VERSION=1.1.0f-3+deb9u2",
            "GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23",
            "TOMCAT_MAJOR=8",
            "TOMCAT_VERSION=8.5.35",
            "TOMCAT_SHA512=1084c067eec2bd27ed2aa5a3729cdc27d0beddbc41968a7004754e75dd88a6703f5fc0e3a01f262cfe2b5d13ed58c95dca1c43bc5ae8789141a11e11f72edd89",
            "TOMCAT_TGZ_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz \thttps://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz \thttps://www.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz \thttps://archive.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz",
            "TOMCAT_ASC_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz.asc \thttps://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz.asc \thttps://www.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz.asc \thttps://archive.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz.asc"
        ],
        "Cmd": [
            "catalina.sh",
            "run"
        ],
        "ArgsEscaped": true,
        "Image": "docker.io/tomcat:latest",
        "Volumes": null,
        "WorkingDir": "/usr/local/tomcat",
        "Entrypoint": null,
        "OnBuild": null,
        "Labels": {}
    },
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "35d58d9a4a82884baea1244f267d6998702425a45f079ffaed93c5130bfd8ebb",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "8080/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "5000"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/netns/35d58d9a4a82",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "9ce27842046872561e33f1b188da5191abfbd6c07804b10b6b15e7b63f2de91f",
        "Gateway": "172.17.0.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.4",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "MacAddress": "02:42:ac:11:00:04",
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "c6bf9dd1099213784a17d2b3d3dbb88d8a0b141f34300a15e7096b066e507b9b",
                "EndpointID": "9ce27842046872561e33f1b188da5191abfbd6c07804b10b6b15e7b63f2de91f",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.4",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:04"
            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读