DOCKER 从入门到放弃(三)

使用docker create [image-name] 创建一个容器

创建一个nginx镜像的容器,由于没有指定各项参数,容器实用默认参数,创建后并不会启动,并将容器的ID输出到终端,如果本地没有镜像,将从网络下载镜像。

[root@docker ~]# docker create nginx
63dd9de3c7280959175223c32253623ac0c0ec7680f1051391b62adc126d464f

使用docker run [image-name] 创建一个容器并运行该容器

创建一个nginx镜像的容器,由于没有指定各项参数,容器实用默认参数,创建并启动运行,并将容器的ID输出到终端,如果本地没有镜像,将从网络下载镜像。但是请使用-d 参数,让容器运行在后台,不然该终端将会被占用无法使用。

[root@docker ~]# docker run -d nginx
593d0b66c5b44b2b810076469872d1032376116e2352846aa60a190f7365d914

实用docker ps -a 命令查看当前docker创建的容器,并显示运行状态

[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
63dd9de3c728        nginx               "nginx -g 'daemon ..."   3 minutes ago       Created                                 gifted_minsky

启动一个创建好的或者被停止的容器

使用docker start [container id] 启动容器

[root@docker ~]# docker start 63dd9de3c728
63dd9de3c728
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
63dd9de3c728        nginx               "nginx -g 'daemon ..."   7 minutes ago       Up 4 seconds        80/tcp              gifted_minsky

停止一个正在运行的容器

使用docker stop [container id] 启动容器

[root@docker ~]# docker stop 63dd9de3c728
63dd9de3c728
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
63dd9de3c728        nginx               "nginx -g 'daemon ..."   9 minutes ago       Exited (0) 4 seconds ago                       gifted_minsky

使用docker rm 删除一个容器

如果被删除的容器处于运行状态,将无法删除

[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
63dd9de3c728        nginx               "nginx -g 'daemon ..."   11 minutes ago      Up 2 seconds        80/tcp              gifted_minsky
[root@docker ~]# docker rm 63dd9de3c728
Error response from daemon: You cannot remove a running container 63dd9de3c7280959175223c32253623ac0c0ec7680f1051391b62adc126d464f. Stop the container before attempting removal or force remove

停止要删除后的容器然后在删除容器

[root@docker ~]# docker stop 63dd9de3c728
63dd9de3c728
[root@docker ~]# docker rm 63dd9de3c728
63dd9de3c728
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

创建容器的参数

OPTIONS 说明
-a stdin 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
-d 后台运行容器,并返回容器ID
-i 以交互模式运行容器,通常与 -t 同时使用
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用
--name="nginx-lb" 为容器指定一个名称
--dns 8.8.8.8 指定容器使用的DNS服务器,默认和宿主一致
--dns-search example.com 指定容器DNS搜索域名,默认和宿主一致
-h "mars" 指定容器的hostname
-e username="ritchie" 设置环境变量
--env-file=[] 从指定文件读入环境变量
--cpuset="0-2" or --cpuset="0,1,2" 绑定容器到指定CPU运行
-m 设置容器使用内存最大值
--net="bridge" 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型
--link=[] 添加链接到另一个容器
--expose=[] 开放一个端口或一组端口

-it

-it 参数一般连用,在docker run 启动一个容器后提供一个容器的终端,如果容器中没开启shell进程,也无法对容器进行交互

[root@docker ~]# docker run -it centos
[root@a8c4541d52fc /]# 

退出容器请使用CTRL + D ;CTRL + C 只会发送给容器的shell,不会退出容器

--name="container name"

--name 将会为容器指定一个名称,没有这个参数,容器的名称将由系统分配

[root@docker ~]# docker create --name="centos-1" centos
eb96e184b3840979de8c9e66adda190c8465fc73dfae65418d8bc0c9c62f491c
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
eb96e184b384        centos              "/bin/bash"         25 seconds ago      Created                                 centos-1

-p

-p 参数将一个docker主机的端口映射到容器中,有如下几种格式:
ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | [range hostPort]:containerPort |[range hostPort]:[range containerPort]

[root@docker ~]# docker run -it -d -p 192.168.0.141:10000:80 --name=c-1 centos
37a154d52dc218499e535e2264753006dc29e40e7d33f35be420d38b3bfb4847
[root@docker ~]# docker run -it -d -p 192.168.0.141::80 --name=n-1 centos
1acf8de94a8e485a19be3e5be0735f4e88807cee8c0f466d18eb1bf2c633ab44
[root@docker ~]# docker run -it -d -p 10001:80 --name=a-1 centos
bbb33f76794dd059a96872c1ff2add28719df15b326fb87864284d10d5e9d470
[root@docker ~]# docker run -it -d -p 10010-10020:80-90 --name=q-1 centos
3877df081cea0903ba91910bf4ee5150cac55d337435f66075c237956350bd42
[root@docker ~]# docker run -it -d -p 10030-10040:80 --name=w-1 centos
8296c4196bfaae55d7d7ec145086bf31b257aea733567501a61d57dcd553a320
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                                                                                                                                                                                                                                         NAMES
8296c4196bfa        centos              "/bin/bash"         3 seconds ago       Up 2 seconds        0.0.0.0:10031->80/tcp                                                                                                                                                                                                                                         w-1
3877df081cea        centos              "/bin/bash"         11 seconds ago      Up 10 seconds       0.0.0.0:10010->80/tcp, 0.0.0.0:10011->81/tcp, 0.0.0.0:10012->82/tcp, 0.0.0.0:10013->83/tcp, 0.0.0.0:10014->84/tcp, 0.0.0.0:10015->85/tcp, 0.0.0.0:10016->86/tcp, 0.0.0.0:10017->87/tcp, 0.0.0.0:10018->88/tcp, 0.0.0.0:10019->89/tcp, 0.0.0.0:10020->90/tcp   q-1
bbb33f76794d        centos              "/bin/bash"         19 seconds ago      Up 18 seconds       0.0.0.0:10001->80/tcp                                                                                                                                                                                                                                         a-1
1acf8de94a8e        centos              "/bin/bash"         27 seconds ago      Up 26 seconds       192.168.0.141:32769->80/tcp                                                                                                                                                                                                                                   n-1
37a154d52dc2        centos              "/bin/bash"         39 seconds ago      Up 39 seconds       192.168.0.141:10000->80/tcp                                                                                                                                                                                                                                   c-1


-v 挂载数据卷

将本地文件/root/pan1/pan.txt 挂载到容器/etc/resolv.conf

[root@docker ~]# docker run -d  --name web-1 -v /root/pan1/pan.txt:/etc/resolv.conf  nginx
d01e19d1c6284a7a5dfc344c7f4ee27da5c099d7ebd95048efea3e6c7b038d55

将本地目录/root/pan1挂载到容器/etc/pan1 下

[root@docker ~]# docker run -d  --name web-2 -v /root/pan1:/etc/pan1  nginx
52a1a7df2964e62a11cf53778e3d461e21090857b03d277db36509c94b3be660

创建一个数据卷,数据卷不会随着容器被删除而被删除

[root@docker ~]# docker create -v /shiyanloudata --name shiyan -it  centos
46fa93471dda1814781ba57727e12e7678ca243b281ed928b64752ab6b2f6031
[root@docker ~]# docker volume ls
DRIVER              VOLUME NAME
local               f8bb990062eebe25d7fdfaa76db5130d89ebacd50a18e25992e83e97ba862123

数据卷可以被多个容器共享,容器挂载其他容器的数据卷

[root@docker ~]# docker run --volumes-from shiyan --name web12345 -d nginx
8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103

查看容器的信息

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

[root@docker ~]# docker inspect web12345
[
    {
        "Id": "8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103",
        "Created": "2017-07-05T15:53:39.588794033Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 11693,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-07-05T15:53:39.755359815Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:c246cd3dd41d35f9deda43609cdeaa9aaf04d3658f9c5e38aad25c4ea5efee10",
        "ResolvConfPath": "/var/lib/docker/containers/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103/hostname",
        "HostsPath": "/var/lib/docker/containers/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103/hosts",
        "LogPath": "/var/lib/docker/containers/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103-json.log",
        "Name": "/web12345",
        "RestartCount": 0,
        "Driver": "overlay",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [
                "shiyan"
            ],
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "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": [],
            "DeviceCgroupRules": 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": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay/ff7668f4f80e6c01e2d669be8303b33191da439384e3212eb4c6dde337897e5d/root",
                "MergedDir": "/var/lib/docker/overlay/f37762cd46028d515d9076e386f725a0d28ced80f16b7fc6983b7e7bcf57d85c/merged",
                "UpperDir": "/var/lib/docker/overlay/f37762cd46028d515d9076e386f725a0d28ced80f16b7fc6983b7e7bcf57d85c/upper",
                "WorkDir": "/var/lib/docker/overlay/f37762cd46028d515d9076e386f725a0d28ced80f16b7fc6983b7e7bcf57d85c/work"
            },
            "Name": "overlay"
        },
        "Mounts": [
            {
                "Name": "07a47233da2320118f5fd04621bede68f178b460bf045c72b602c0f58881620e",
                "Source": "/var/lib/docker/volumes/07a47233da2320118f5fd04621bede68f178b460bf045c72b602c0f58881620e/_data",
                "Destination": "/shiyan",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "8b6cb7beed18",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.13.1-1~stretch",
                "NJS_VERSION=1.13.1.0.1.10-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "9f43f43c72567aaad066ba824cf33a5349c95a5f99b5c771c558fedcdbe396f4",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/9f43f43c7256",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "1c1efb2dc8b892d45dbb5a45ca1a1b7477f61fc76c7ed58f3fc71207e36fef2d",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.9",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:09",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "6536d99d4f08428535db0de618aa15b0ee2663c50b197ec4dca77b9a1210f9e4",
                    "EndpointID": "1c1efb2dc8b892d45dbb5a45ca1a1b7477f61fc76c7ed58f3fc71207e36fef2d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.9",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:09"
                }
            }
        }
    }
]

查看指定的信息使用-f参数:

[root@docker ~]# docker inspect -f {{.LogPath}} web12345
/var/lib/docker/containers/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103/8b6cb7beed18e2070fea14b00db89c7b1b7491ed40f1850cef9e5e83c9859103-json.log

[root@docker ~]# docker inspect -f {{.Config.Image}} web12345
nginx

参考资料:https://docs.docker.com/engine/reference/commandline/inspect/#examples

参考文档

https://docs.docker.com/engine/reference/run

https://docs.docker.com/engine/reference/commandline/create/

posted @ 2017-07-06 00:04  长风七万里  阅读(1283)  评论(0编辑  收藏  举报