其他分享
首页 > 其他分享> > docker简单命令集

docker简单命令集

作者:互联网

容器命令

1. 下载centos镜像

Docker pull centos

 

2. 创建容器并启动

Docker run [可选参数] image

 

#参数说明

--name=test #容器名称

-d  #后台方式运行

-it #使用交互方式运行。进入容器查看内容

-p #指定容器端口

   -p 8000:8000 #主机端口:容器端口

   -p 8000      #容器端口

   -p           #随机指定端口

   -p 主机IP:主机端口:容器端口   #主机IP:主机端口:容器端口

-e #配置系统环境变量例如:EES_JAVA_OPTS=”Xms64m -Xmx512m”

例如:docker run -it tomcat:8 /bin/bash

启动并进入容器,并运行一个bash(前台应用,随便是上面都行),容器启动必须要有已经进程,不然马上会被kill itself,启动好后如下图:

 

到此就已经创建了一个liunx容器。

注意:使用-d后台启动的时候,一定要带上前台应用,docker发现容器没提供服务就会立刻停止服务。

3. 查询容器

Docker ps  #查询现在正在运行的容器.在主机菜单使用,容器中是不能使用。

-a   #当前运行+历史运行

-n=? #?为要显示的数字 例如:-n=1

 

 

 

4. 删除容器

Docker rm 容器ID   #删除指定容器

Docker rm $(docker ps -aq) #删除所有容器(不包含已启动的)

Docker rm -f $(docker ps -aq) #删除所有容器(包含已启动的)

 

 

5. 启动(停止)容器

Docker start 容器ID    #启动容器

Docker restart 容器ID  #重启容器

Docker stop 容器ID     #停止容器

Docker kill  容器ID    #强制停止容器

 

6. 查看日志

Docker log -t -f --tail 10 容器ID

-t          #显示时间戳

-f          #显示实时log

--tail 10   #实时显示行数

疑问:查询的是容器日志?显示的什么打印出来的日志?

 

7. 查看进程

Docker top 容器ID

8. 查看容器信息

Docker inspect 容器ID

[

    {

        "Id": "71b2bb92647487daa87db3b2c788c7bd2b9735c17e0e9698f9d256724b9c3671",

        "Created": "2020-08-22T07:59:35.223568424Z",

        "Path": "/bin/bash",

        "Args": [],

        "State": {

            "Status": "running",

            "Running": true,

            "Paused": false,

            "Restarting": false,

            "OOMKilled": false,

            "Dead": false,

            "Pid": 21589,

            "ExitCode": 0,

            "Error": "",

            "StartedAt": "2020-08-22T08:16:29.01258363Z",

            "FinishedAt": "2020-08-22T08:16:00.299929688Z"

        },

        "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",

        "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/71b2bb92647487daa87db3b2c788c7bd2b9735c17e0e9698f9d256724b9c3671/resolv.conf",

        "HostnamePath": "/mnt/sda1/var/lib/docker/containers/71b2bb92647487daa87db3b2c788c7bd2b9735c17e0e9698f9d256724b9c3671/hostname",

        "HostsPath": "/mnt/sda1/var/lib/docker/containers/71b2bb92647487daa87db3b2c788c7bd2b9735c17e0e9698f9d256724b9c3671/hosts",

        "LogPath": "/mnt/sda1/var/lib/docker/containers/71b2bb92647487daa87db3b2c788c7bd2b9735c17e0e9698f9d256724b9c3671/71b2bb92647487daa87db3b2c788c7bd2b9735c17e0e9698f9d256724b9c3671-json.log",

        "Name": "/sad_lewin",

        "RestartCount": 0,

        "Driver": "overlay2",

        "Platform": "linux",

        "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": null,

            "CapAdd": null,

            "CapDrop": null,

            "Capabilities": null,

            "Dns": [],

            "DnsOptions": [],

            "DnsSearch": [],

            "ExtraHosts": null,

            "GroupAdd": null,

            "IpcMode": "private",

            "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": [],

            "BlkioDeviceReadBps": null,

            "BlkioDeviceWriteBps": null,

            "BlkioDeviceReadIOps": null,

            "BlkioDeviceWriteIOps": null,

            "CpuPeriod": 0,

            "CpuQuota": 0,

            "CpuRealtimePeriod": 0,

            "CpuRealtimeRuntime": 0,

            "CpusetCpus": "",

            "CpusetMems": "",

            "Devices": [],

            "DeviceCgroupRules": null,

            "DeviceRequests": null,

            "KernelMemory": 0,

            "KernelMemoryTCP": 0,

            "MemoryReservation": 0,

            "MemorySwap": 0,

            "MemorySwappiness": null,

            "OomKillDisable": false,

            "PidsLimit": null,

            "Ulimits": null,

            "CpuCount": 0,

            "CpuPercent": 0,

            "IOMaximumIOps": 0,

            "IOMaximumBandwidth": 0,

            "MaskedPaths": [

                "/proc/asound",

                "/proc/acpi",

                "/proc/kcore",

                "/proc/keys",

                "/proc/latency_stats",

                "/proc/timer_list",

                "/proc/timer_stats",

                "/proc/sched_debug",

                "/proc/scsi",

                "/sys/firmware"

            ],

            "ReadonlyPaths": [

                "/proc/bus",

                "/proc/fs",

                "/proc/irq",

                "/proc/sys",

                "/proc/sysrq-trigger"

            ]

        },

        "GraphDriver": {

            "Data": {

                "LowerDir": "/mnt/sda1/var/lib/docker/overlay2/ec4e68489498b9dc361e1fe61e697c83e91665e6ad49212faa0c80fafd6c8a82-init/diff:/mnt/sda1/var/lib/docker/overlay2/0035cd873068e025c789ef48f606eb10ad4cabbf7d32c3a9856c3fb8150aefd6/diff",

                "MergedDir": "/mnt/sda1/var/lib/docker/overlay2/ec4e68489498b9dc361e1fe61e697c83e91665e6ad49212faa0c80fafd6c8a82/merged",

                "UpperDir": "/mnt/sda1/var/lib/docker/overlay2/ec4e68489498b9dc361e1fe61e697c83e91665e6ad49212faa0c80fafd6c8a82/diff",

                "WorkDir": "/mnt/sda1/var/lib/docker/overlay2/ec4e68489498b9dc361e1fe61e697c83e91665e6ad49212faa0c80fafd6c8a82/work"

            },

            "Name": "overlay2"

        },

        "Mounts": [],

        "Config": {

            "Hostname": "71b2bb926474",

            "Domainname": "",

            "User": "",

            "AttachStdin": true,

            "AttachStdout": true,

            "AttachStderr": true,

            "Tty": true,

            "OpenStdin": true,

            "StdinOnce": true,

            "Env": [

                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

            ],

            "Cmd": [

                "/bin/bash"

            ],

            "Image": "centos",

            "Volumes": null,

            "WorkingDir": "",

            "Entrypoint": null,

            "OnBuild": null,

            "Labels": {

                "org.label-schema.build-date": "20200809",

                "org.label-schema.license": "GPLv2",

                "org.label-schema.name": "CentOS Base Image",

                "org.label-schema.schema-version": "1.0",

                "org.label-schema.vendor": "CentOS"

            }

        },

        "NetworkSettings": {

            "Bridge": "",

            "SandboxID": "b9540cf00032776535847754200be8a8d9dc48ec22d2067d92071abc0449dedd",

            "HairpinMode": false,

            "LinkLocalIPv6Address": "",

            "LinkLocalIPv6PrefixLen": 0,

            "Ports": {},

            "SandboxKey": "/var/run/docker/netns/b9540cf00032",

            "SecondaryIPAddresses": null,

            "SecondaryIPv6Addresses": null,

            "EndpointID": "1aeec39251c06608668b3638975da54cfb72759866c46a1606c92347cc336c21",

            "Gateway": "172.17.0.1",

            "GlobalIPv6Address": "",

            "GlobalIPv6PrefixLen": 0,

            "IPAddress": "172.17.0.2",

            "IPPrefixLen": 16,

            "IPv6Gateway": "",

            "MacAddress": "02:42:ac:11:00:02",

            "Networks": {

                "bridge": {

                    "IPAMConfig": null,

                    "Links": null,

                    "Aliases": null,

                    "NetworkID": "69f9a94efc07843e61661ae5952b3a880cab7d14a23bb15a431d5fcd95179d74",

                    "EndpointID": "1aeec39251c06608668b3638975da54cfb72759866c46a1606c92347cc336c21",

                    "Gateway": "172.17.0.1",

                    "IPAddress": "172.17.0.2",

                    "IPPrefixLen": 16,

                    "IPv6Gateway": "",

                    "GlobalIPv6Address": "",

                    "GlobalIPv6PrefixLen": 0,

                    "MacAddress": "02:42:ac:11:00:02",

                    "DriverOpts": null

                }

            }

        }

    }

]

 

9. 进入当前正在运行的容器

方式一:

docker exec -it 71b2bb926474 /bin/bash #docker exec进入容器 -it交互模式,注意进入容器也需要带上一个应用

 

方式二:

 

 docker attach 容器ID

区别:

Exec:进入容器后打开了一个新的终端,可以在里面做操作

Attach:进入正在执行的终端,不会启动新的进程。

10. 退出容器,进入主机

Exit    #停止容器并退出

Ctrl+p+q  #容器不停止退出

11. 文件拷贝

从容器拷贝到宿主机上

docker cp 容器ID:容器中文件全路径(包括文件名和后缀) 主机上文件要拷贝的目标路径。

例如:docker cp 71b2bb926474:tmp/test f:/docker-tempFile  #此命令在Docker Quickstart Terminal中执行(直接拷贝到windows本地)

 

如果使用finalshell执行拷贝命令,会将文件拷贝到宿主机上(/home/docker),在通过sftp下载到windows本地。

 

12. 查看dockerCPU

Docker stats

 

13. Commit镜像

1、首先下载一个基础镜像。

2、对这个基础镜像按照自己的需求进行修改。

3、最后commit到本地镜像库中。

Docker commit -a=”tom” -m=”用于测试” 需要commit的容器ID 新镜像名称:新版本号

#-a :提交的镜像作者;

-c :使用Dockerfile指令来创建镜像;

-m :提交时的说明文字;

-p :在commit时,将容器暂停;

 

4、将本地镜像上传到服务器上。

14. 容器卷的使用

Windows主机与宿主机文件共享,以tomcat容器为例,实现将指定文件挂载(不是容器卷的形式)到指定容器上;在宿主机上可同步操作windows主机盘上数据(删除/home/docker-tempFile中内容。F://docker-tempFile下的文件也会删除)

windows系统将本地磁盘放到docker宿主机(每次宿主机重启后都需要重新挂载):

mkdir docker-tempFile

sudo mount -t vboxsf docker-tempFile /home/docker-tempFile

 

创建数据卷

创建docker容器与宿主机同步文件夹,就算容器未启动,也可以创建数据卷。

docker run -it -v /home/volume:/home/volume1 centos /bin/bash  

#创建centos容器,创建容器卷(匿名挂载),在宿主机上自动创建/home/volume目录,在容器home/volume1目录和宿主机/home/volume目录进行双向绑定,内容两边同步显示。

注意:如果容器文件夹(/home)中有文件,使用上面命令,会清除容器此文件夹(/home)中所有文件,所以只能对容器中空文件夹执行上面命令。

如果想将容器上文件拷贝到宿主机上可以使用下面命令:

docker cp 8700b1ef20d5:/usr/local/tomcat/conf/ /home/volume

#8700b1ef20d5 容器ID

#/usr/local/tomcat/conf/ 容器中文件夹

#/home/volume宿主机上文件夹

Docker inspect 容器ID  #查询容器详情中显示的卷信息

Docker volume inspect 卷名称  #查询容器卷详情

 

查询数据卷列表

匿名挂载

没有指定名字的挂载,不建议这样创建数据卷,例如上面创建数据卷就是匿名挂载

docker run -it -v /home/volume centos /bin/bash

具名挂载

没有指定主机目录的情况下,主机的数据卷都是在 /var/lib/docker/volumes/XXX

docker run -it -v guazai-name:/home/volume centos /bin/bash

#guazai-name 数据卷名称

#/home/volume 容器内数据卷路径

指定路径挂载

docker run -it -v /home/volume:/home/volume1 centos /bin/bash

挂载权限

 

 

标签:容器,proc,简单,home,docker,null,命令集,Docker
来源: https://www.cnblogs.com/pjuncool/p/16537687.html