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

目的

配置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"
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容

  • 神说要有光,于是便有了光。 神说要有爱,于是便有了爱。 神说要分离,于是便留不住一切。 那些希冀美好都是转瞬即逝。...
    楚珺阅读 259评论 0 1
  • 上帝在我年幼懵懂的时候,为我种下了一棵桃花树,然后又发现我是个傻逼不懂得珍惜,就连根把树挖走了,以至于现今我还是孤...
    鱼刺S阅读 252评论 0 0
  • 架构之路(四):测试驱动-CSDN.NEThttp://www.csdn.net/article/2015-09-...
    葡萄喃喃呓语阅读 261评论 0 0