166.7. docker 命令

本文介绍了Docker的基本操作,包括运行、启动、停止、重启容器等常用命令,并展示了如何查看容器的日志、历史记录和镜像信息。通过具体示例,帮助读者掌握Docker的基础使用。

166.7.1. run

run

$ sudo docker run ubuntu:14.04 /bin/echo 'Hello world'
Hello world
			

166.7.2. start / stop / restart

sudo docker start silly_bohr
silly_bohr

$ sudo docker stop silly_bohr
silly_bohr

$ sudo docker restart silly_bohr
silly_bohr
			

166.7.3. ps

sudo docker ps
			
$ sudo docker ps -l
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
84391d1de0fc        ubuntu:14.04        /bin/echo Hello worl   31 minutes ago      Exit 0                                  romantic_ritchie
			

166.7.4. top

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
13b2a4a31455        ubuntu:14.04        /bin/bash           3 hours ago         Up 3 hours                              silly_bohr

$ sudo docker top silly_bohr
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                23225               22908               0                   12:17               pts/14              00:00:00            /bin/bash
			

166.7.5. inspect

			
$ sudo docker inspect silly_bohr
[{
    "ID": "13b2a4a3145528d087c9d1580fa78aaa52e8a9bb973c9da923bceb9f9b9e7e5a",
    "Created": "2014-07-17T04:17:45.262480632Z",
    "Path": "/bin/bash",
    "Args": [],
    "Config": {
        "Hostname": "13b2a4a31455",
        "Domainname": "",
        "User": "",
        "Memory": 0,
        "MemorySwap": 0,
        "CpuShares": 0,
        "AttachStdin": true,
        "AttachStdout": true,
        "AttachStderr": true,
        "PortSpecs": null,
        "ExposedPorts": null,
        "Tty": true,
        "OpenStdin": true,
        "StdinOnce": true,
        "Env": [
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd": [
            "/bin/bash"
        ],
        "Dns": [
            "8.8.8.8",
            "8.8.4.4"
        ],
        "Image": "ubuntu",
        "Volumes": null,
        "VolumesFrom": "",
        "WorkingDir": "",
        "Entrypoint": null,
        "NetworkDisabled": false,
        "OnBuild": null
    },
    "State": {
        "Running": true,
        "Pid": 23225,
        "ExitCode": 0,
        "StartedAt": "2014-07-17T04:17:45.672269614Z",
        "FinishedAt": "0001-01-01T00:00:00Z",
        "Ghost": false
    },
    "Image": "e54ca5efa2e962582a223ca9810f7f1b62ea9b5c3975d14a5da79d3bf6020f37",
    "NetworkSettings": {
        "IPAddress": "172.17.0.2",
        "IPPrefixLen": 16,
        "Gateway": "172.17.42.1",
        "Bridge": "docker0",
        "PortMapping": null,
        "Ports": {}
    },
    "ResolvConfPath": "/var/lib/docker/containers/13b2a4a3145528d087c9d1580fa78aaa52e8a9bb973c9da923bceb9f9b9e7e5a/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/13b2a4a3145528d087c9d1580fa78aaa52e8a9bb973c9da923bceb9f9b9e7e5a/hostname",
    "HostsPath": "/var/lib/docker/containers/13b2a4a3145528d087c9d1580fa78aaa52e8a9bb973c9da923bceb9f9b9e7e5a/hosts",
    "Name": "/silly_bohr",
    "Driver": "aufs",
    "ExecDriver": "native-0.1",
    "Volumes": {},
    "VolumesRW": {},
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "LxcConf": [],
        "Privileged": false,
        "PortBindings": {},
        "Links": null,
        "PublishAllPorts": false
    }
}]
			
			
$ sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' silly_bohr
			

166.7.6. images

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.10               58faa899733f        2 weeks ago         196 MB
ubuntu              utopic              58faa899733f        2 weeks ago         196 MB
ubuntu              precise             ea7d6801c538        3 weeks ago         127.5 MB
ubuntu              12.04               ea7d6801c538        3 weeks ago         127.5 MB
ubuntu              12.10               c5881f11ded9        4 weeks ago         172.2 MB
ubuntu              quantal             c5881f11ded9        4 weeks ago         172.2 MB
ubuntu              13.04               463ff6be4238        4 weeks ago         169.4 MB
ubuntu              raring              463ff6be4238        4 weeks ago         169.4 MB
ubuntu              13.10               195eb90b5349        4 weeks ago         184.7 MB
ubuntu              saucy               195eb90b5349        4 weeks ago         184.7 MB
ubuntu              14.04               e54ca5efa2e9        4 weeks ago         276.5 MB
ubuntu              latest              e54ca5efa2e9        4 weeks ago         276.5 MB
ubuntu              trusty              e54ca5efa2e9        4 weeks ago         276.5 MB
ubuntu              10.04               3db9c44f4520        12 weeks ago        183 MB
ubuntu              lucid               3db9c44f4520        12 weeks ago        183 MB
			
166.7.6.1. Getting a new image
$ sudo docker pull centos
Pulling repository centos
b7de3133ff98: Pulling dependent layers
5cc9e91966f7: Pulling fs layer
511136ea3c5a: Download complete
ef52fb1fe610: Download complete
				

166.7.7. logs

显示容器运行日志,用于排查异常情况

例如下面是nginx容易启动出错日志

[root@netkiller]# docker logs my-nginx-container   
nginx: [emerg] invalid server name or wildcard "www.*.com" on 0.0.0.0:80
nginx: [emerg] invalid server name or wildcard "www.*.com" on 0.0.0.0:80
nginx: [emerg] invalid server name or wildcard "www.*.com" on 0.0.0.0:80
nginx: [emerg] invalid server name or wildcard "www.*.com" on 0.0.0.0:80
nginx: [emerg] invalid server name or wildcard "www.*.com" on 0.0.0.0:80
nginx: [emerg] invalid server name or wildcard "www.*.com" on 0.0.0.0:80
		

166.7.8. history

镜像历史纪录

		
# docker history centos:tomcat
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
2faf9a2d2bdc        22 hours ago        /bin/sh -c #(nop)  CMD ["catalina.sh" "run"]    0 B                 
8e12c1e8fd89        22 hours ago        /bin/sh -c #(nop)  EXPOSE 8080/tcp              0 B                 
35158d8231c5        22 hours ago        /bin/sh -c #(nop)  VOLUME [/srv/tomcat/temp]    0 B                 
4302c5c13241        22 hours ago        /bin/sh -c #(nop)  VOLUME [/srv/tomcat/work]    0 B                 
53537696aa19        22 hours ago        /bin/sh -c #(nop) ADD file:ac42f23f37092b9...   298 B               
be04ba27a9ae        23 hours ago        /bin/sh -c set -x   && wget -O tomcat.tar....   8.75 MB             
847be662a35f        5 days ago          /bin/sh -c #(nop)  ENV TOMCAT_ASC_URL=http...   0 B                 
ac6550346558        5 days ago          /bin/sh -c #(nop)  ENV TOMCAT_TGZ_URL=http...   0 B                 
50c12be7ca48        5 days ago          /bin/sh -c #(nop)  ENV TOMCAT_VERSION=8.5.15    0 B                 
89c44758e4ae        5 days ago          /bin/sh -c #(nop)  ENV TOMCAT_MAJOR=8           0 B                 
560ad98c1b23        5 days ago          /bin/sh -c yum install -y java-1.8.0-openj...   236 MB              
befeedbb7dc7        5 days ago          /bin/sh -c #(nop) WORKDIR /srv/tomcat           0 B                 
c85cf394faf8        5 days ago          /bin/sh -c mkdir -p "$CATALINA_HOME"            0 B                 
debf78012b2c        5 days ago          /bin/sh -c #(nop)  ENV PATH=/srv/tomcat/bi...   0 B                 
ccc27f4f3bcf        5 days ago          /bin/sh -c #(nop)  ENV CATALINA_HOME=/srv/...   0 B                 
8f351964d568        6 days ago          /bin/sh -c #(nop)  MAINTAINER Netkiller <n...   0 B                 
3bee3060bfc8        9 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
<missing>           9 days ago          /bin/sh -c #(nop)  LABEL name=CentOS Base ...   0 B                 
<missing>           9 days ago          /bin/sh -c #(nop) ADD file:d22a9c627d1d1f3...   193 MB		
		
		




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

train@DESKTOP-TCOC9P2:~$ sudo apt-get autoremove docker docker-ce docker-engine docker.io containerd runc Reading package lists... Done Building dependency tree... Done Reading state information... Done Package 'docker' is not installed, so not removed Package 'docker-engine' is not installed, so not removed Package 'docker.io' is not installed, so not removed Package 'containerd' is not installed, so not removed Package 'runc' is not installed, so not removed The following packages will be REMOVED: docker-ce 0 upgraded, 0 newly installed, 1 to remove and 166 not upgraded. After this operation, 90.1 MB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 41335 files and directories currently installed.) Removing docker-ce (5:28.3.3-1~ubuntu.24.04~noble) ... invoke-rc.d: could not determine current runlevel * Stopping Docker: docker start-stop-daemon: warning: failed to kill 209: No such process No process in pidfile '/var/run/docker-ssd.pid' found running; none killed. invoke-rc.d: initscript docker, action "stop" failed. dpkg: error processing package docker-ce (--remove): installed docker-ce package pre-removal script subprocess returned error exit status 1 dpkg: too many errors, stopping dpkg: error while cleaning up: installed docker-ce package post-installation script subprocess returned error exit status 1 Errors were encountered while processing: docker-ce Processing was halted because there were too many errors. E: Sub-process /usr/bin/dpkg returned an error code (1)
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值