docker命令详解

0、docker支持命令查看

使用命令查看一下docker都有那些命令:

docker -h

你将得到如下结果:

A self-sufficient runtime for linux containers.

 

Options:

 

  --api-cors-header=                   Set CORS headersin the remote API

  -b, --bridge=                        Attach containers to anetwork bridge

  --bip=                               Specify networkbridge IP

  -D, --debug=false                    Enable debug mode

  -d, --daemon=false                   Enable daemon mode

  --default-gateway=                   Container default gatewayIPv4 address

  --default-gateway-v6=                Container default gateway IPv6address

  --default-ulimit=[]                  Set default ulimitsfor containers

  --dns=[]                             DNS server to use

  --dns-search=[]                      DNS search domains to use

  -e, --exec-driver=native             Exec driver to use

  --exec-opt=[]                        Set exec driver options

  --exec-root=/var/run/docker          Root of the Docker execdriver

  --fixed-cidr=                        IPv4 subnetfor fixed IPs

  --fixed-cidr-v6=                     IPv6 subnetfor fixed IPs

  -G, --group=docker                  Groupfor the unix socket

  -g, --graph=/var/lib/docker          Root of the Docker runtime

  -H, --host=[]                        Daemon socket(s) toconnect to

  -h, --help=false                     Print usage

  --icc=true                           Enable inter-container communication

  --insecure-registry=[]               Enable insecure registry communication

  --ip=0.0.0.0                         Default IP whenbinding container ports

  --ip-forward=true                    Enable net.ipv4.ip_forward

  --ip-masq=true                       Enable IP masquerading

  --iptables=true                      Enable addition ofiptables rules

  --ipv6=false                         Enable IPv6 networking

  -l, --log-level=info                 Set the logging level

  --label=[]                           Set key=value labels to the daemon

  --log-driver=json-file               Default driverfor container logs

  --log-opt=map[]                      Set log driver options

  --mtu=0                              Set thecontainers network MTU

  -p, --pidfile=/var/run/docker.pid    Path to usefor daemon PIDfile

 --registry-mirror=[]                 Preferred Dockerregistry mirror

  -s, --storage-driver=                Storage driver to use

  --selinux-enabled=false              Enable selinuxsupport

  --storage-opt=[]                     Set storage driver options

  --tls=false                          Use TLS; implied by--tlsverify

  --tlscacert=~/.docker/ca.pem         Trust certs signed only by this CA

  --tlscert=~/.docker/cert.pem         Path to TLS certificatefile

 --tlskey=~/.docker/key.pem          Path to TLS keyfile

  --tlsverify=false                    Use TLS and verify theremote

  --userland-proxy=true                Use userland proxyfor loopback traffic

  -v, --version=false                  Print version information andquit

 

Commands:

    attach    Attach to a running container

    build     Build an image from a Dockerfile

    commit    Create a new image from a container‘s changes

    cp        Copyfiles/foldersfrom a container‘s filesystem to the host path

    create    Create a new container

    diff     Inspect changes on a container‘s filesystem

    events    Get real time events from the server

    exec      Run a command in a running container

    export    Stream the contents of a container as a tar archive

    history   Show the history of an image

    images    List images

    import    Create a new filesystem image from thecontents of a tarball

    info     Display system-wide information

    inspect   Return low-level information on a container or image

    kill     Kill a running container

    load      Load an image from a tar archive

    login    Register or login to a Docker registryserver

    logout    Log out from a Docker registry server

    logs      Fetch the logs of a container

    pause     Pause all processes within a container

    port      Lookup the public-facing port that is NAT-ed to PRIVATE_PORT

    ps        Listcontainers

    pull      Pull an image or a repository from a Docker registry server

    push      Push an image or a repository to a Dockerregistry server

    rename    Rename an existing container

    restart   Restart a running container

    rm        Remove oneormore containers

    rmi       Remove one or more images

    run       Run a command in a new container

    save      Save an image to a tar archive

    search    Search for an image on the Docker Hub

    start     Start a stopped container

    stats     Display a stream of a containers‘ resource usage statistics

    stop      Stop a running container

    tag       Tag an image into a repository

    top       Lookup the running processes of acontainer

    unpause   Unpause a paused container

    version   Show the Docker version information

    wait     Blockuntil a container stops,then print its exit code

 

Run ‘docker COMMAND --helpformore information on a command.

下面来详细说明一些命令的使用,在此之前,说一下如果不理解理解 Docker和 Docker Hub 及两者关系,可以类比 Git 和 GitHub 理解。

0.1. 查看私有仓库中的镜像信息

在浏览器中输入http://10.1.245.31:5000/v1/search,其中IP及port为私有仓库地址。例如:

如果要查看详细的镜像内容,可以通过以下url查看:

http://10.1.245.31:5000/v1/repositories/library/hps_adb/tags

http://10.1.245.31:5000/v1/repositories/library/mydomain/tags


0.2. 查看已存在的Vlan

docker network ls

例如:

[root@hps1 app]# docker network ls
NETWORK ID          NAME                DRIVER
7e883d49c631        100                 overlay             
5cec71e3befd        host                host                
51f39158cd61        bridge              bridge              
47154d13751b        none                null


0.3. 创建overlay网络

docker network create --driver overlay 101 

例如:

[root@hps1 app]# docker network create --driver overlay 101
b7e3fc0a677d192095f41901bc72ac6e6b2067709066026d96ae071955e9eef6

[root@hps1 app]# docker network ls
NETWORK ID          NAME                DRIVER
7e883d49c631        100                 overlay             
b7e3fc0a677d        101                 overlay             
47154d13751b        none                null                
5cec71e3befd        host                host                
51f39158cd61        bridge              bridge 

1. docker version

显示Docker 版本信息。

2. docker info

显示 Docker 系统信息,包括镜像和容器数。

3. docker search

从Docker Hub 中搜索符合条件的镜像。

docker search -s 3 --automated --no-trunc django

上面命令的意思是搜索处收藏数不小于 3 ,并且能够自动化构建的 django 镜像,并且完整显示镜像描述。

参数:

  --automated=false   Only show automated builds 只列出 automated build类型的镜像--no-trunc=false    Don‘ttruncate output 显示完整的镜像描述

  -s, --stars=0       Only displays with at least x stars只列出不低于x个收藏的镜像

4. docker pull

从Docker Hub 中拉取或者更新指定镜像。

docker pull ubuntu:latest

上面命令的意思是拉取ubuntu最新的镜像。

参数:

-a, --all-tags=false    Download alltagged imagesin the repository拉取所有 tagged 镜像

5. docker login

按步骤输入在 Docker Hub 注册的用户名、密码和邮箱即可完成登录。

6. docker logout

运行后从指定服务器登出,默认为官方服务器。

7. docker images


列出本地所有镜像。对镜像名称进行关键词查询。

docker images ubuntu

上面命令的意思是列出本地镜像名为 ubuntu 的所有镜像。不加 ubuntu,就列出所有本地镜像。

参数:

  -a, --all=false     Show all images (default hides intermediate images) 列出所有镜像(含中间映像层,默认情况下,过滤掉中间映像层)

  --digests=false     Show digests 展示镜像的摘要

  -f, --filter=[]     Filter output based on conditions provided 过滤镜像,如: -f [‘dangling=true‘] 只列出满足dangling=true 条件的镜像

  --no-trunc=false    Don‘ttruncate output显示完整的镜像ID

  -q, --quiet=false   Only show numeric IDs仅列出镜像ID

8. docker ps

列出所有运行中容器。

参数:

  -a, --all=false      Show all containers (default shows just running) 列出所有容器(含沉睡镜像)

  --before=             Show only container created beforeId or Name 列出在某一容器之前创建的容器,接受容器名称和ID作为参数

  -f, --filter=[]      Filter output based on conditions provided-f [exited=<int>] 列出满足exited=<int> 条件的容器-l, --latest=false   Show the latest created container, include non-running 仅列出最新创建的一个容器

  -n=-1                 Show nlast created containers, include non-running 列出最近创建的n个容器

  --no-trunc=false     Don‘ttruncate output显示完整的容器ID

  -q, --quiet=false    Only display numeric IDs 仅列出容器ID

  -s, --size=false     Display totalfile sizes 显示容器大小

  --since=              Show created since Id or Name,include non-running列出在某一容器之后创建的容器,接受容器名称和ID作为参数

9. docker rmi


从本地移除一个或多个指定的镜像。

docker rmi nginx:latest ubuntu:14.04

上面命令的意思是移除 nginx 最新版本的镜像和ubuntu 14.04 版本的镜像。

参数:

  -f, --force=false   Force removal of the image 强行移除该镜像,即使其正被使用

  --no-prune=false    Do not delete untagged parents不移除该镜像的过程镜像,默认移除

10. docker rm

从本地移除一个或多个指定的容器。

docker rm harrysun/lnmp

docker rm -l webapp/redis

上面命令的意思分别是移除 harrysun/lnmp 的本地容器和移除 webapp/redis 容器的网络连接。

参数:

  -f, --force=false     Force the removal of a running container (uses SIGKILL) 强行移除该容器,即使其正在运行-l, --link=false      Remove the specified link 移除容器间的网络连接,而非容器本身

  -v, --volumes=false   Remove the volumes associated with the container移除与容器关联的空间

11. docker history

查看指定镜像的创建历史。

docker history -H harrysun/lnmp:0.1

上面命令的意思是查看 harrysun/lnmp:0.1 镜像的历史。

  -H, --human=true    Print sizes and datesin human readableformat 以可读的格式打印镜像大小和日期

  --no-trunc=false    Don‘ttruncate output显示完整的提交记录

  -q, --quiet=false   Only show numeric IDs仅列出提交记录ID

12. docker start|stop|restart

启动、停止和重启一个或多个指定容器。

docker start -i b5e08e1435b3

上面命令的意思是启动一个 ID 为 b5e08e1435b3 的容器,并进入交互模式。

参数:

  -a, --attach=false        Attach STDOUT/STDERR and forward signals 启动一个容器并打印输出结果和错误

  -i, --interactive=false   Attach container‘sSTDIN启动一个容器并进入交互模式

  -t, --time=10     Seconds towaitfor stop before killing the container停止或者重启容器的超时时间(秒),超时后系统将杀死进程。

13. docker kill

杀死一个或多个指定容器进程。

docker kill -s KILL94c6b3c3f04a

上面命令的意思是杀死一个 ID 为 94c6b3c3f04a 的容器,并向容器发送 KILL 信号。

参数:

  -s,--signal=KILL    Signal to send to thecontainer自定义发送至容器的信号

14. docker events

从服务器拉取个人动态,可选择时间区间。

docker events --since="20150720" --until="20150808"

上面命令的意思是拉取个人从 2015/07/20 到 2015/08/08 的个人动态。

参数:

  -f, --filter=[]   Filter output based on conditions provided--since=          Show all events created since timestamp 开始时间

  --until=           Stream eventsuntil this timestamp结束时间

15. docker save

将指定镜像保存成 tar 归档文件, docker load 的逆操作。保存后再加载(saved-loaded)的镜像不会丢失提交历史和层,可以回滚。

docker save -o ubuntu14.04.tar ubuntu:14.04

上面命令的意思是将镜像 ubuntu:14.04 保存为 ubuntu14.04.tar 文件。

参数:

  -o,--output=      Write to an file, instead of STDOUT输出到的文件

16. docker load

从tar 镜像归档中载入镜像, docker save 的逆操作。保存后再加载(saved-loaded)的镜像不会丢失提交历史和层,可以回滚。

docker load -i ubuntu14.04.tar

上面命令的意思是将 ubuntu14.04.tar 文件载入镜像中。

参数:

  -i, --input=       Read from a tar archivefile, instead of STDIN加载的tar文件

17. docker export

将指定的容器保存成 tar 归档文件, docker import 的逆操作。导出后导入(exported-imported))的容器会丢失所有的提交历史,无法回滚。

docker export -o ubuntu14.04.tar 94c6b3c3f04a

上面命令的意思是将 ID 为 94c6b3c3f04a 容器保存为 ubuntu14.04.tar 文件。

参数:

  -o,--output=      Write to a file, instead of STDOUT

18. docker import

从归档文件(支持远程文件,.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz)创建一个镜像, export 的逆操作,可为导入镜像打上标签。导出后导入(exported-imported))的容器会丢失所有的提交历史,无法回滚。

cat ./ubuntu14.04.tar |sudo docker import - ubuntu:14.04

上面命令的意思是使用 ./ubuntu14.04.tar 文件创建 ubuntu:14.04的镜像,默认会从远端拉取文件。

19. docker top

查看一个正在运行容器进程,支持 ps 命令参数。

20. docker inspect

检查镜像或者容器的参数,默认返回 JSON 格式。 (Template

docker inspect --format ‘{{.DockerVersion}} ubuntu:14.04

上面命令的意思是返回 ubuntu:14.04  镜像的 docker 版本

参数:

  -f,--format=      Format the output usingthe given go template指定返回值的模板文件

21. docker pause

暂停某一容器的所有进程。

22. docker unpause

恢复某一容器的所有进程。

23. docker tag

标记本地镜像,将其归入某一仓库。

sudo docker tag 5db5f8471261 harrysun/lnmp:0.2

上面命令的意思是将 ID 为 5db5f8471261 的容器标记为 harrysun/lnmp:0.2 镜像。

参数:

  -f, --force=false   Force会覆盖已有标记

24. docker push

将镜像推送至远程仓库,默认为 Docker Hub

docker push harrysun/lnmp:0.2

上面命令的意思是将 harrysun/lnmp:0.2 镜像推送到远端。

25. docker logs

获取容器运行时的输出日志。

docker logs -f --tail10 94c6b3c3f04a

上面命令的意思是将追踪 ID 为 94c6b3c3f04a 的容器最新的10条日志。

参数:

  -f, --follow=false       Follow log output 跟踪容器日志的最近更新

  --since=                  Show logs since timestamp 开始时间

  -t, --timestamps=false   Show timestamps 显示容器日志的时间戳

  --tail=all                Number oflines to show from the end of the logs 仅列出最新n条容器日志

26. docker run

启动一个容器,在其中运行指定命令。

docker run -i -t ubuntu:14.04 /bin/bash

上面命令的意思是以 ubuntu:14.04 镜像启动一个容器,以交互模式运行,并为容器重新分配一个伪输入终端。

参数:(这个命令的参数有点多,只说其中一部分) 

  -a, --attach=[]             Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项

  --add-host=[]               Add a custom host-to-IP mapping (host:ip)

  --blkio-weight=0            Block IO (relativeweight), between 10 and1000

  -c, --cpu-shares=0         CPU shares (relativeweight)

  --cap-add=[]                Add Linux capabilities

  --cap-drop=[]               Drop Linux capabilities

  --cgroup-parent=           Optional parent cgroup for the container

  --cidfile=                  Write the container ID to thefile

  --cpu-period=0              Limit CPU CFS (Completely FairScheduler) period

  --cpu-quota=0               Limit the CPU CFS quota

  --cpuset-cpus=              CPUs inwhich to allow execution(0-3,0,1) 绑定容器到指定CPU运行

  --cpuset-mems=              MEMs inwhich to allow execution(0-3,0,1) 绑定容器到指定MEM运行

  -d, --detach=false         Run containerin background and printcontainer ID 后台运行容器,并返回容器ID

  --device=[]                 Add a host device to thecontainer

  --dns=[]                    Set custom DNS servers 指定容器使用的DNS服务器,默认和宿主一致

  --dns-search=[]             Set custom DNS search domains 指定容器DNS搜索域名,默认和宿主一致

  -e, --env=[]                Set environment variables 设置环境变量

  --entrypoint=               Overwrite the default ENTRYPOINTof the image

  --env-file=[]               Readin a file of environmentvariables 从指定文件读入环境变量

  --expose=[]                 Expose a port or a range ofports

  -h, --hostname=             Container host name 指定容器的hostname

  --help=false                Print usage

  -i, --interactive=false    Keep STDIN open evenif not attached 以交互模式运行容器,通常与 -t 同时使用

  --ipc=                      IPC namespace to use

  -l, --label=[]              Set meta data on a container

  --label-file=[]             Readin aline delimited file of labels

  --link=[]                   Add link to anothercontainer

  --log-driver=               Logging driverfor container

  --log-opt=[]                Log driver options

  --lxc-conf=[]               Add custom lxc options

  -m, --memory=               Memory limit

  --mac-address=              Container MAC address (e.g.92:d0:c6:0a:29:33)

  --memory-swap=              Total memory (memory + swap),‘-1‘ to disable swap

  --name=                     Assign a name to thecontainer 为容器指定一个名称

  --net=bridge                Set the Network modefor the container  指定容器的网络连接类型,支持 bridge/host/none/container:<name|id>四种类型

  --oom-kill-disable=false   Disable OOM Killer

  -P, --publish-all=false    Publish all exposed ports to random ports

  -p, --publish=[]            Publish a container‘s port(s) to the host

  --pid=                      PID namespace to use

  --privileged=false         Give extended privileges to this container

  --read-only=false           Mount the container‘s root filesystem asread only

  --restart=no                Restart policy to apply when acontainer exits

  --rm=false                  Automatically remove thecontainer when it exits

  --security-opt=[]           Security Options

  --sig-proxy=true           Proxy received signals to the process

  -t, --tty=false             Allocate a pseudo-TTY 为容器重新分配一个伪输入终端,通常与 -i 同时使用

  -u, --user=                 Username or UID (format:<name|uid>[:<group|gid>])

  --ulimit=[]                 Ulimit options

  --uts=                      UTS namespace to use

  -v, --volume=[]             Bind mount a volume

  --volumes-from=[]           Mount volumes from the specified container(s)

  -w, --workdir=             Working directory inside the container

例如,在其他机器上远程启动10.1.245.31主机上面的仓库,可以使用:

docker -H 10.1.245.31:2375 run  -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /data/docker/registry5001:/tmp/registry -p 5001:5000 registry

其中,

-H 10.1.245.31:2375指定远程主机以及远程主机上docker守护程序的端口;

-d表示后台运行;

-e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry 表示容器启动时,配置容器内的环境变量SETTINGS_FLAVOR=dev和STORAGE_PATH=/tmp/registry,环境变量该怎么配置需要看容器运行的程序识别什么样的环境变量,例如tomcat容器就识别CATALINA_HOME这个环境变量,其他的容器就不见得识别得了;

-v /data/docker/registry5001:/tmp/registry表示将容器内的/tmp/registry挂载到本地的 /data/docker/registry5001目录下;

-p 5001:5000指容器内的5000端口映射到主机上的5001端口,至于容器内哪些端口需要映射出来,还是要看容器内程序使用哪些端口对外提供服务,比如tomcat一般启动8080端口,这样就要将8080端口映射出来;

registry表示镜像名称,没有加标签,默认使用latest标签的镜像。

27、docker stats

持续查看容器运行状态,包括CPU使用率、内存已使用/限制使用、网络IO、存储设备IO,只显示最新的一行数据

docker stats 9e8b1357b01c

CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
9e8b1357b01c        0.08%               238.4 MB / 314.6 MB   75.80%              648 B / 648 B       43.35 MB / 0 B


CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
9e8b1357b01c        0.07%               238.4 MB / 314.6 MB   75.80%              648 B / 648 B       43.35 MB / 0 B

上面命令的意思是查看容器9e8b1357b01c的运行状态

28、docker exec

该命令能够通过交互方式进入到运行的容器中进行操作,例如运行的容器id是441ce760e233 ,可以执行:

docker exec -i -t 441ce760e233 /bin/bash

上述命令提供了一个伪输入终端,能够在容器内进行操作。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值