实例学习ansible系列(12)常用模块之docker

知识点:ansible使用docker可以对其进行管理。基本接近docker-compose对docker的使用支持,非常接近。诸如从port的设定到volume_from都支持,但是需要docker-py0.3.0 以上的支持。

使用前提条件

需要使用docker的module的管理对象节点需要满足如下前提

Package 所需版本
python 2.6 以上
docker-py 0.3.0 以上
docker server 0.10.0 以上

安装docker-py

一般python等基本上无需意识,一般安装了docker-py本模块就能支持。

安装步骤
[root@host32 ~]# yum -y python-pip
[root@host31 ~]# yum -y install python-pip
[root@host31 ~]# pip install docker-py
  • 1
  • 2
  • 3
  • 4

使用docker模块启动container

事前确认

[root@host31 ~]# ansible host32 -m shell -a "docker images && docker ps -a"
host32 | SUCCESS | rc=0 >>
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/jenkins   latest              b4974ba62598        2 weeks ago         741 MB
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

启动container

[root@host31 ~]# ansible host32 -m docker -a "image=docker.io/jenkins ports=8080:8080"
host32 | SUCCESS => {
    "ansible_facts": {
        "docker_containers": [
            {
                "AppArmorProfile": "",
                "Args": [
                    "--",
                    "/usr/local/bin/jenkins.sh"
                ],
                "Config": {
                    "AttachStderr": false,
                    "AttachStdin": false,
                    "AttachStdout": false,
                    "Cmd": null,
                    "Domainname": "",
                    "Entrypoint": [
                        "/bin/tini",
                        "--",
                        "/usr/local/bin/jenkins.sh"
                    ],
                    "Env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "LANG=C.UTF-8",
                        "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64",
                        "JAVA_VERSION=8u91",
                        "JAVA_DEBIAN_VERSION=8u91-b14-1~bpo8+1",
                        "CA_CERTIFICATES_JAVA_VERSION=20140324",
                        "JENKINS_HOME=/var/jenkins_home",
                        "JENKINS_SLAVE_AGENT_PORT=50000",
                        "TINI_VERSION=0.9.0",
                        "TINI_SHA=fa23d1e20732501c3bb8eeeca423c89ac80ed452",
                        "JENKINS_VERSION=2.7.1",
                        "JENKINS_SHA=12d820574c8f586f7d441986dd53bcfe72b95453",
                        "JENKINS_UC=https://updates.jenkins.io",
                        "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log"
                    ],
                    "ExposedPorts": {
                        "50000/tcp": {},
                        "8080/tcp": {}
                    },
                    "Hostname": "283789b73a8e",
                    "Image": "docker.io/jenkins",
                    "Labels": {},
                    "OnBuild": null,
                    "OpenStdin": false,
                    "StdinOnce": false,
                    "Tty": false,
                    "User": "jenkins",
                    "Volumes": {
                        "/var/jenkins_home": {}
                    },
                    "WorkingDir": ""
                },
                "Created": "2016-07-31T03:03:38.350849615Z",
                "Driver": "devicemapper",
                "ExecIDs": null,
                "GraphDriver": {
                    "Data": {
                        "DeviceId": "25",
                        "DeviceName": "docker-253:0-19943542-b5917ff17a3050352a5cf430fa1449362166da5142d76e931553119acb600654",
                        "DeviceSize": "10737418240"
                    },
                    "Name": "devicemapper"
                },
                "HostConfig": {
                    "Binds": null,
                    "BlkioDeviceReadBps": null,
                    "BlkioDeviceReadIOps": null,
                    "BlkioDeviceWriteBps": null,
                    "BlkioDeviceWriteIOps": null,
                    "BlkioWeight": 0,
                    "BlkioWeightDevice": null,
                    "CapAdd": null,
                    "CapDrop": null,
                    "CgroupParent": "",
                    "ConsoleSize": [
                        0,
                        0
                    ],
                    "ContainerIDFile": "",
                    "CpuPeriod": 0,
                    "CpuQuota": 0,
                    "CpuShares": 0,
                    "CpusetCpus": "",
                    "CpusetMems": "",
                    "Devices": null,
                    "Dns": null,
                    "DnsOptions": null,
                    "DnsSearch": null,
                    "ExtraHosts": null,
                    "GroupAdd": null,
                    "IpcMode": "",
                    "Isolation": "",
                    "KernelMemory": 0,
                    "Links": null,
                    "LogConfig": {
                        "Config": {},
                        "Type": "journald"
                    },
                    "Memory": 0,
                    "MemoryReservation": 0,
                    "MemorySwap": 0,
                    "MemorySwappiness": -1,
                    "NetworkMode": "default",
                    "OomKillDisable": false,
                    "OomScoreAdj": 0,
                    "PidMode": "",
                    "PidsLimit": 0,
                    "PortBindings": {
                        "8080/tcp": [
                            {
                                "HostIp": "0.0.0.0",
                                "HostPort": "8080"
                            }
                        ]
                    },
                    "Privileged": false,
                    "PublishAllPorts": false,
                    "ReadonlyRootfs": false,
                    "RestartPolicy": {
                        "MaximumRetryCount": 0,
                        "Name": ""
                    },
                    "SecurityOpt": null,
                    "ShmSize": 67108864,
                    "UTSMode": "",
                    "Ulimits": null,
                    "VolumeDriver": "",
                    "VolumesFrom": null
                },
                "HostnamePath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hostname",
                "HostsPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hosts",
                "Id": "283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955",
                "Image": "sha256:b4974ba625983cc1250f3399a262d98398b914ff04d2ba360760b81d1c944983",
                "LogPath": "",
                "MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c122,c287",
                "Mounts": [
                    {
                        "Destination": "/var/jenkins_home",
                        "Driver": "local",
                        "Mode": "",
                        "Name": "2fdd9acb3d3b497ce12f9584afac3747a4baff991678df15ea3e9bbc97a0411f",
                        "Propagation": "",
                        "RW": true,
                        "Source": "/var/lib/docker/volumes/2fdd9acb3d3b497ce12f9584afac3747a4baff991678df15ea3e9bbc97a0411f/_data"
                    }
                ],
                "Name": "/goofy_spence",
                "NetworkSettings": {
                    "Bridge": "",
                    "EndpointID": "45fde805822822b90f6e8fabe865ae0ab7faa39ca9e1624e6dea614d4403a6e8",
                    "Gateway": "172.16.96.1",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "HairpinMode": false,
                    "IPAddress": "172.16.96.2",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "LinkLocalIPv6Address": "",
                    "LinkLocalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:10:60:02",
                    "Networks": {
                        "bridge": {
                            "Aliases": null,
                            "EndpointID": "45fde805822822b90f6e8fabe865ae0ab7faa39ca9e1624e6dea614d4403a6e8",
                            "Gateway": "172.16.96.1",
                            "GlobalIPv6Address": "",
                            "GlobalIPv6PrefixLen": 0,
                            "IPAMConfig": null,
                            "IPAddress": "172.16.96.2",
                            "IPPrefixLen": 24,
                            "IPv6Gateway": "",
                            "Links": null,
                            "MacAddress": "02:42:ac:10:60:02",
                            "NetworkID": "320ec600e4605955d8d83b717a5177b2eaf81c97615af4dc606cba34f3952b29"
                        }
                    },
                    "Ports": {
                        "50000/tcp": null,
                        "8080/tcp": [
                            {
                                "HostIp": "0.0.0.0",
                                "HostPort": "8080"
                            }
                        ]
                    },
                    "SandboxID": "8ebecd9887e411a4294a57e50da61c0e669dddaf193cebc0c2fe0c5a523df0e2",
                    "SandboxKey": "/var/run/docker/netns/8ebecd9887e4",
                    "SecondaryIPAddresses": null,
                    "SecondaryIPv6Addresses": null
                },
                "Path": "/bin/tini",
                "ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c122,c287",
                "ResolvConfPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/resolv.conf",
                "RestartCount": 0,
                "State": {
                    "Dead": false,
                    "Error": "",
                    "ExitCode": 0,
                    "FinishedAt": "0001-01-01T00:00:00Z",
                    "OOMKilled": false,
                    "Paused": false,
                    "Pid": 30604,
                    "Restarting": false,
                    "Running": true,
                    "StartedAt": "2016-07-31T03:03:39.630193596Z",
                    "Status": "running"
                }
            }
        ]
    },
    "changed": true,
    "msg": "started 1 container, created 1 container.",
    "reload_reasons": null,
    "summary": {
        "created": 1,
        "killed": 0,
        "pulled": 0,
        "removed": 0,
        "restarted": 0,
        "started": 1,
        "stopped": 0
    }
}
[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226

确认结果

[root@host31 ~]# ansible host32 -m shell -a "docker images && docker ps -a"
host32 | SUCCESS | rc=0 >>
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/jenkins   latest              b4974ba62598        2 weeks ago         741 MB
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
283789b73a8e        docker.io/jenkins   "/bin/tini -- /usr/lo"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->8080/tcp, 50000/tcp   goofy_spence

[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

jenkins login

停止container

实现docker stop 的操作

[root@host31 ~]# ansible host32 -m docker -a "image=docker.io/jenkins state=stopped"
host32 | SUCCESS => {
    "ansible_facts": {
        "docker_containers": [
            {
                "AppArmorProfile": "",
                "Args": [
                    "--",
                    "/usr/local/bin/jenkins.sh"
                ],
                "Config": {
                    "AttachStderr": false,
                    "AttachStdin": false,
                    "AttachStdout": false,
                    "Cmd": null,
                    "Domainname": "",
                    "Entrypoint": [
                        "/bin/tini",
                        "--",
                        "/usr/local/bin/jenkins.sh"
                    ],
                    "Env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "LANG=C.UTF-8",
                        "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64",
                        "JAVA_VERSION=8u91",
                        "JAVA_DEBIAN_VERSION=8u91-b14-1~bpo8+1",
                        "CA_CERTIFICATES_JAVA_VERSION=20140324",
                        "JENKINS_HOME=/var/jenkins_home",
                        "JENKINS_SLAVE_AGENT_PORT=50000",
                        "TINI_VERSION=0.9.0",
                        "TINI_SHA=fa23d1e20732501c3bb8eeeca423c89ac80ed452",
                        "JENKINS_VERSION=2.7.1",
                        "JENKINS_SHA=12d820574c8f586f7d441986dd53bcfe72b95453",
                        "JENKINS_UC=https://updates.jenkins.io",
                        "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log"
                    ],
                    "ExposedPorts": {
                        "50000/tcp": {},
                        "8080/tcp": {}
                    },
                    "Hostname": "283789b73a8e",
                    "Image": "docker.io/jenkins",
                    "Labels": {},
                    "OnBuild": null,
                    "OpenStdin": false,
                    "StdinOnce": false,
                    "Tty": false,
                    "User": "jenkins",
                    "Volumes": {
                        "/var/jenkins_home": {}
                    },
                    "WorkingDir": ""
                },
                "Created": "2016-07-31T03:03:38.350849615Z",
                "Driver": "devicemapper",
                "ExecIDs": null,
                "GraphDriver": {
                    "Data": {
                        "DeviceId": "25",
                        "DeviceName": "docker-253:0-19943542-b5917ff17a3050352a5cf430fa1449362166da5142d76e931553119acb600654",
                        "DeviceSize": "10737418240"
                    },
                    "Name": "devicemapper"
                },
                "HostConfig": {
                    "Binds": null,
                    "BlkioDeviceReadBps": null,
                    "BlkioDeviceReadIOps": null,
                    "BlkioDeviceWriteBps": null,
                    "BlkioDeviceWriteIOps": null,
                    "BlkioWeight": 0,
                    "BlkioWeightDevice": null,
                    "CapAdd": null,
                    "CapDrop": null,
                    "CgroupParent": "",
                    "ConsoleSize": [
                        0,
                        0
                    ],
                    "ContainerIDFile": "",
                    "CpuPeriod": 0,
                    "CpuQuota": 0,
                    "CpuShares": 0,
                    "CpusetCpus": "",
                    "CpusetMems": "",
                    "Devices": null,
                    "Dns": null,
                    "DnsOptions": null,
                    "DnsSearch": null,
                    "ExtraHosts": null,
                    "GroupAdd": null,
                    "IpcMode": "",
                    "Isolation": "",
                    "KernelMemory": 0,
                    "Links": null,
                    "LogConfig": {
                        "Config": {},
                        "Type": "journald"
                    },
                    "Memory": 0,
                    "MemoryReservation": 0,
                    "MemorySwap": 0,
                    "MemorySwappiness": -1,
                    "NetworkMode": "default",
                    "OomKillDisable": false,
                    "OomScoreAdj": 0,
                    "PidMode": "",
                    "PidsLimit": 0,
                    "PortBindings": {
                        "8080/tcp": [
                            {
                                "HostIp": "0.0.0.0",
                                "HostPort": "8080"
                            }
                        ]
                    },
                    "Privileged": false,
                    "PublishAllPorts": false,
                    "ReadonlyRootfs": false,
                    "RestartPolicy": {
                        "MaximumRetryCount": 0,
                        "Name": ""
                    },
                    "SecurityOpt": null,
                    "ShmSize": 67108864,
                    "UTSMode": "",
                    "Ulimits": null,
                    "VolumeDriver": "",
                    "VolumesFrom": null
                },
                "HostnamePath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hostname",
                "HostsPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hosts",
                "Id": "283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955",
                "Image": "sha256:b4974ba625983cc1250f3399a262d98398b914ff04d2ba360760b81d1c944983",
                "LogPath": "",
                "MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c122,c287",
                "Mounts": [
                    {
                        "Destination": "/var/jenkins_home",
                        "Driver": "local",
                        "Mode": "",
                        "Name": "2fdd9acb3d3b497ce12f9584afac3747a4baff991678df15ea3e9bbc97a0411f",
                        "Propagation": "",
                        "RW": true,
                        "Source": "/var/lib/docker/volumes/2fdd9acb3d3b497ce12f9584afac3747a4baff991678df15ea3e9bbc97a0411f/_data"
                    }
                ],
                "Name": "/goofy_spence",
                "NetworkSettings": {
                    "Bridge": "",
                    "EndpointID": "",
                    "Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "HairpinMode": false,
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "LinkLocalIPv6Address": "",
                    "LinkLocalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "Networks": {
                        "bridge": {
                            "Aliases": null,
                            "EndpointID": "",
                            "Gateway": "",
                            "GlobalIPv6Address": "",
                            "GlobalIPv6PrefixLen": 0,
                            "IPAMConfig": null,
                            "IPAddress": "",
                            "IPPrefixLen": 0,
                            "IPv6Gateway": "",
                            "Links": null,
                            "MacAddress": "",
                            "NetworkID": "320ec600e4605955d8d83b717a5177b2eaf81c97615af4dc606cba34f3952b29"
                        }
                    },
                    "Ports": null,
                    "SandboxID": "8ebecd9887e411a4294a57e50da61c0e669dddaf193cebc0c2fe0c5a523df0e2",
                    "SandboxKey": "/var/run/docker/netns/8ebecd9887e4",
                    "SecondaryIPAddresses": null,
                    "SecondaryIPv6Addresses": null
                },
                "Path": "/bin/tini",
                "ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c122,c287",
                "ResolvConfPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/resolv.conf",
                "RestartCount": 0,
                "State": {
                    "Dead": false,
                    "Error": "",
                    "ExitCode": 143,
                    "FinishedAt": "2016-07-31T03:10:05.113884817Z",
                    "OOMKilled": false,
                    "Paused": false,
                    "Pid": 0,
                    "Restarting": false,
                    "Running": false,
                    "StartedAt": "2016-07-31T03:03:39.630193596Z",
                    "Status": "exited"
                }
            }
        ]
    },
    "changed": true,
    "msg": "stopped 1 container.",
    "reload_reasons": null,
    "summary": {
        "created": 0,
        "killed": 0,
        "pulled": 0,
        "removed": 0,
        "restarted": 0,
        "started": 0,
        "stopped": 1
    }
}
[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218

停止确认

[root@host31 ~]# ansible host32 -m shell -a "docker images && docker ps"
host32 | SUCCESS | rc=0 >>
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/jenkins   latest              b4974ba62598        2 weeks ago         741 MB
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@host31 ~]#
[root@host31 ~]# ansible host32 -m shell -a "docker ps -a"
host32 | SUCCESS | rc=0 >>
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
283789b73a8e        docker.io/jenkins   "/bin/tini -- /usr/lo"   7 minutes ago       Exited (143) About a minute ago                       goofy_spence

[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

删除container

实现docker rm的操作

[root@host31 ~]# ansible host32 -m shell -a "docker ps -a"
host32 | SUCCESS | rc=0 >>
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
283789b73a8e        docker.io/jenkins   "/bin/tini -- /usr/lo"   8 minutes ago       Exited (143) 2 minutes ago                       goofy_spence

[root@host31 ~]#
[root@host31 ~]# ansible host32 -m docker -a "image=docker.io/jenkins state=absent"
host32 | SUCCESS => {
    "ansible_facts": {
        "docker_containers": [
            {
                "AppArmorProfile": "",
                "Args": [
                    "--",
                    "/usr/local/bin/jenkins.sh"
                ],
                "Config": {
                    "AttachStderr": false,
                    "AttachStdin": false,
                    "AttachStdout": false,
                    "Cmd": null,
                    "Domainname": "",
                    "Entrypoint": [
                        "/bin/tini",
                        "--",
                        "/usr/local/bin/jenkins.sh"
                    ],
                    "Env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "LANG=C.UTF-8",
                        "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64",
                        "JAVA_VERSION=8u91",
                        "JAVA_DEBIAN_VERSION=8u91-b14-1~bpo8+1",
                        "CA_CERTIFICATES_JAVA_VERSION=20140324",
                        "JENKINS_HOME=/var/jenkins_home",
                        "JENKINS_SLAVE_AGENT_PORT=50000",
                        "TINI_VERSION=0.9.0",
                        "TINI_SHA=fa23d1e20732501c3bb8eeeca423c89ac80ed452",
                        "JENKINS_VERSION=2.7.1",
                        "JENKINS_SHA=12d820574c8f586f7d441986dd53bcfe72b95453",
                        "JENKINS_UC=https://updates.jenkins.io",
                        "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log"
                    ],
                    "ExposedPorts": {
                        "50000/tcp": {},
                        "8080/tcp": {}
                    },
                    "Hostname": "283789b73a8e",
                    "Image": "docker.io/jenkins",
                    "Labels": {},
                    "OnBuild": null,
                    "OpenStdin": false,
                    "StdinOnce": false,
                    "Tty": false,
                    "User": "jenkins",
                    "Volumes": {
                        "/var/jenkins_home": {}
                    },
                    "WorkingDir": ""
                },
                "Created": "2016-07-31T03:03:38.350849615Z",
                "Driver": "devicemapper",
                "ExecIDs": null,
                "GraphDriver": {
                    "Data": {
                        "DeviceId": "25",
                        "DeviceName": "docker-253:0-19943542-b5917ff17a3050352a5cf430fa1449362166da5142d76e931553119acb600654",
                        "DeviceSize": "10737418240"
                    },
                    "Name": "devicemapper"
                },
                "HostConfig": {
                    "Binds": null,
                    "BlkioDeviceReadBps": null,
                    "BlkioDeviceReadIOps": null,
                    "BlkioDeviceWriteBps": null,
                    "BlkioDeviceWriteIOps": null,
                    "BlkioWeight": 0,
                    "BlkioWeightDevice": null,
                    "CapAdd": null,
                    "CapDrop": null,
                    "CgroupParent": "",
                    "ConsoleSize": [
                        0,
                        0
                    ],
                    "ContainerIDFile": "",
                    "CpuPeriod": 0,
                    "CpuQuota": 0,
                    "CpuShares": 0,
                    "CpusetCpus": "",
                    "CpusetMems": "",
                    "Devices": null,
                    "Dns": null,
                    "DnsOptions": null,
                    "DnsSearch": null,
                    "ExtraHosts": null,
                    "GroupAdd": null,
                    "IpcMode": "",
                    "Isolation": "",
                    "KernelMemory": 0,
                    "Links": null,
                    "LogConfig": {
                        "Config": {},
                        "Type": "journald"
                    },
                    "Memory": 0,
                    "MemoryReservation": 0,
                    "MemorySwap": 0,
                    "MemorySwappiness": -1,
                    "NetworkMode": "default",
                    "OomKillDisable": false,
                    "OomScoreAdj": 0,
                    "PidMode": "",
                    "PidsLimit": 0,
                    "PortBindings": {
                        "8080/tcp": [
                            {
                                "HostIp": "0.0.0.0",
                                "HostPort": "8080"
                            }
                        ]
                    },
                    "Privileged": false,
                    "PublishAllPorts": false,
                    "ReadonlyRootfs": false,
                    "RestartPolicy": {
                        "MaximumRetryCount": 0,
                        "Name": ""
                    },
                    "SecurityOpt": null,
                    "ShmSize": 67108864,
                    "UTSMode": "",
                    "Ulimits": null,
                    "VolumeDriver": "",
                    "VolumesFrom": null
                },
                "HostnamePath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hostname",
                "HostsPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/hosts",
                "Id": "283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955",
                "Image": "sha256:b4974ba625983cc1250f3399a262d98398b914ff04d2ba360760b81d1c944983",
                "LogPath": "",
                "MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c122,c287",
                "Mounts": [
                    {
                        "Destination": "/var/jenkins_home",
                        "Driver": "local",
                        "Mode": "",
                        "Name": "2fdd9acb3d3b497ce12f9584afac3747a4baff991678df15ea3e9bbc97a0411f",
                        "Propagation": "",
                        "RW": true,
                        "Source": "/var/lib/docker/volumes/2fdd9acb3d3b497ce12f9584afac3747a4baff991678df15ea3e9bbc97a0411f/_data"
                    }
                ],
                "Name": "/goofy_spence",
                "NetworkSettings": {
                    "Bridge": "",
                    "EndpointID": "",
                    "Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "HairpinMode": false,
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "LinkLocalIPv6Address": "",
                    "LinkLocalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "Networks": {
                        "bridge": {
                            "Aliases": null,
                            "EndpointID": "",
                            "Gateway": "",
                            "GlobalIPv6Address": "",
                            "GlobalIPv6PrefixLen": 0,
                            "IPAMConfig": null,
                            "IPAddress": "",
                            "IPPrefixLen": 0,
                            "IPv6Gateway": "",
                            "Links": null,
                            "MacAddress": "",
                            "NetworkID": "320ec600e4605955d8d83b717a5177b2eaf81c97615af4dc606cba34f3952b29"
                        }
                    },
                    "Ports": null,
                    "SandboxID": "8ebecd9887e411a4294a57e50da61c0e669dddaf193cebc0c2fe0c5a523df0e2",
                    "SandboxKey": "/var/run/docker/netns/8ebecd9887e4",
                    "SecondaryIPAddresses": null,
                    "SecondaryIPv6Addresses": null
                },
                "Path": "/bin/tini",
                "ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c122,c287",
                "ResolvConfPath": "/var/lib/docker/containers/283789b73a8e044943f5f6b719c6b769fc72b4c83902b908a8444cc77f85c955/resolv.conf",
                "RestartCount": 0,
                "State": {
                    "Dead": false,
                    "Error": "",
                    "ExitCode": 143,
                    "FinishedAt": "2016-07-31T03:10:05.113884817Z",
                    "OOMKilled": false,
                    "Paused": false,
                    "Pid": 0,
                    "Restarting": false,
                    "Running": false,
                    "StartedAt": "2016-07-31T03:03:39.630193596Z",
                    "Status": "exited"
                }
            }
        ]
    },
    "changed": true,
    "msg": "removed 1 container.",
    "reload_reasons": null,
    "summary": {
        "created": 0,
        "killed": 0,
        "pulled": 0,
        "removed": 1,
        "restarted": 0,
        "started": 0,
        "stopped": 0
    }
}
[root@host31 ~]#
[root@host31 ~]# ansible host32 -m shell -a "docker ps -a"
host32 | SUCCESS | rc=0 >>
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@host31 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed

猜你喜欢

转载自www.cnblogs.com/firsttry/p/10166992.html