首页 > 代码库 > docker命令详解

docker命令详解

docker命令详解

 

  1 # docker --help  2 Usage: docker [OPTIONS] COMMAND [arg...]  3        docker daemon [ --help | ... ]  4        docker [ -h | --help | -v | --version ]  5   6 A self-sufficient runtime for containers.  7   8 Options:  9  10   --config=~/.docker              Location of client config files 11   -D, --debug=false               Enable debug mode 12   -H, --host=[]                   Daemon socket(s) to connect to 13   -h, --help=false                Print usage 14   -l, --log-level=info            Set the logging level 15   --tls=false                     Use TLS; implied by --tlsverify 16   --tlscacert=~/.docker/ca.pem    Trust certs signed only by this CA 17   --tlscert=~/.docker/cert.pem    Path to TLS certificate file 18   --tlskey=~/.docker/key.pem      Path to TLS key file 19   --tlsverify=false               Use TLS and verify the remote 20   -v, --version=false             Print version information and quit 21  22 Commands: 23     attach    Attach to a running container   24               --将终端依附到容器上 25               1> 运行一个交互型容器 26                  [root@localhost ~]# docker run -i -t centos /bin/bash 27                  [root@f0a02b473067 /]#  28               2> 在另一个窗口上查看该容器的状态 29                  [root@localhost ~]# docker ps -a 30                  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS      PORTS       NAMES 31                  d4a75f165ce6        centos              "/bin/bash"         5 seconds ago       Up 5 seconds            cranky_mahavira 32               3> 退出第一步中运行的容器 33                  [root@d4a75f165ce6 /]# exit 34                   exit 35               4> 查看该容器的状态 36                  [root@localhost ~]# docker ps -a 37                  CONTAINER ID        IMAGE           COMMAND           CREATED             STATUS                  PORTS    NAMES 38                  d4a75f165ce6        centos          "/bin/bash"       2 minutes ago       Exited (0) 23 seconds ago        cranky_mahavira 39                  可见此时容器的状态是Exited,那么,如何再次运行这个容器呢?可以使用docker start命令 40               5> 再次运行该容器 41                  [root@localhost ~]# docker start cranky_mahavira 42                  cranky_mahavira 43               6> 再次查看该容器的状态 44                  [root@localhost ~]# docker ps -a 45                  CONTAINER ID        IMAGE          COMMAND             CREATED             STATUS              PORTS      NAMES 46                  d4a75f165ce6        centos         "/bin/bash"         6 minutes ago       Up 29 seconds                  cranky_mahavira 47                  因为该容器是交互型的,但此刻我们发现没有具体的终端可以与之交互,这时可使用attach命令。 48               7> 通过attach命令进行交互 49                  [root@localhost ~]# docker attach cranky_mahavira 50                  [root@d4a75f165ce6 /]#  51  52     build     Build an image from a Dockerfile 53               --通过Dockerfile创建镜像 54  55     commit    Create a new image from a containers changes 56               --通过容器创建本地镜像 57               注意:如果是要push到docker hub中,注意生成镜像的命名 58                [root@localhost ~]# docker commit centos_v1 centos:v1 59                68ad49c999496cff25fdda58f0521530a143d3884e61bce7ada09bdc22337638 60                [root@localhost ~]# docker push centos:v1 61                You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: <user>/centos) 62                用centos:v1就不行,因为它push到docker hub中时,是推送到相应用户下,必须指定用户名。譬如我的用户名是ivictor,则新生成的本地镜像命名为: 63                docker push victor/centos:v1,其中v1是tag,可不写,默认是latest  64                65     cp        Copy files/folders from a container to a HOSTDIR or to STDOUT 66               --在宿主机和容器之间相互COPY文件 67               cp的用法如下: 68               Usage:    docker cp [OPTIONS] CONTAINER:PATH LOCALPATH|- 69                         docker cp [OPTIONS] LOCALPATH|- CONTAINER:PATH 70               需要注意的是-的用法,我在容器新建了两个文本文件,其中一个为test.txt,内容如下: 71               root@839866a338db:/# cat test.txt  72               456 73               另一个文件为test1,txt,内容为: 74               root@839866a338db:/# cat test1.txt 75               helloworld 76               用法一的结果如下: 77               [root@localhost ~]# docker cp mysqldb:/test.tar - 78               test.tar0100644000000000000000000002400012573523153010736 0ustar0000000000000000test.txt000064400000000000000000000000141257352311101              1267 0ustar  rootroot123 79               789 80               test1.txt0000644000000000000000000000001312573523124011353 0ustar  rootroothelloworld 81               用法二的结果如下: 82               [root@localhost ~]# cat test.tar |docker cp - mysqldb:/ 83               [root@localhost ~]# docker exec -it mysqldb /bin/bash 84               root@839866a338db:/# ls 85               bin   dev              entrypoint.sh  home  lib64  mnt  proc  run   selinux    sys      test.txt   tmp  var 86               boot  docker-entrypoint-initdb.d  etc         lib   media  opt  root  sbin  srv    test.tar  test1.txt  usr 87               --容器内新增了两个文件,test.txt和test1.txt,而这正是test.tar里打包的文件 88  89     create    Create a new container   90               --创建一个新的容器,注意,此时,容器的status只是Created 91  92     diff      Inspect changes on a containers filesystem 93               --查看容器内发生改变的文件,以我的mysql容器为例 94                [root@localhost ~]# docker diff mysqldb 95                C /root 96                A /root/.bash_history 97                A /test1.txt 98                A /test.tar 99                A /test.txt100                C /run101                C /run/mysqld102                A /run/mysqld/mysqld.pid103                A /run/mysqld/mysqld.sock104                不难看出,C对应的均是目录,A对应的均是文件105 106     events    Get real time events from the server107               --实时输出Docker服务器端的事件,包括容器的创建,启动,关闭等。108               譬如:109               [root@localhost ~]# docker events110               2015-09-08T17:40:13.000000000+08:00 d2a2ef5ddb90b505acaf6b59ab43eecf7eddbd3e71f36572436c34dc0763db79: (from wordpress) create111               2015-09-08T17:40:14.000000000+08:00 d2a2ef5ddb90b505acaf6b59ab43eecf7eddbd3e71f36572436c34dc0763db79: (from wordpress) die112               2015-09-08T17:42:10.000000000+08:00 839866a338db6dd626fa8eabeef53a839e4d2e2eb16ebd89679aa722c4caa5f7: (from mysql) start113 114     exec      Run a command in a running container115               --用于容器启动之后,执行其它的任务116               通过exec命令可以创建两种任务:后台型任务和交互型任务117               后台型任务:docker exec -d cc touch 123  其中cc是容器名118               交互型任务:119               [root@localhost ~]# docker exec -i -t cc /bin/bash120               root@1e5bb46d801b:/# ls121  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var122 123     export    Export a containers filesystem as a tar archive124               --将容器的文件系统打包成tar文件125               有两种方式:126               docker export -o mysqldb1.tar mysqldb127               docker export mysqldb > mysqldb.tar128 129     history   Show the history of an image130               --显示镜像制作的过程,相当于dockfile131 132     images    List images   133               --列出本机的所有镜像134 135     import    Import the contents from a tarball to create a filesystem image136               --根据tar文件的内容新建一个镜像,与之前的export命令相对应137              [root@localhost ~]# docker import mysqldb.tar mysql:v1138              eb81de183cd94fd6f0231de4ff29969db822afd3a25841d2dc9cf3562d135a10139              [root@localhost ~]# docker images140              REPOSITORY                 TAG                 IMAGE ID            CREATED              VIRTUAL SIZE141              mysql                      v1                  eb81de183cd9        21 seconds ago       281.9 MB142 143     info      Display system-wide information144               --查看docker的系统信息145               [root@localhost ~]# docker info146               Containers: 3    --当前有3个容器147               Images: 298      148               Storage Driver: devicemapper149                Pool Name: docker-253:0-34402623-pool150                Pool Blocksize: 65.54 kB151                Backing Filesystem: xfs152                Data file: /dev/loop0153                Metadata file: /dev/loop1154                Data Space Used: 8.677 GB     --对应的是下面Data loop file大小155                Data Space Total: 107.4 GB156                Data Space Available: 5.737 GB157                Metadata Space Used: 13.4 MB  --对应的是下面Metadata loop file大小158                Metadata Space Total: 2.147 GB159                Metadata Space Available: 2.134 GB160                Udev Sync Supported: true161                Deferred Removal Enabled: false162                Data loop file: /var/lib/docker/devicemapper/devicemapper/data163                Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata164                Library Version: 1.02.93-RHEL7 (2015-01-28)165               Execution Driver: native-0.2166               Logging Driver: json-file167               Kernel Version: 3.10.0-229.el7.x86_64168               Operating System: CentOS Linux 7 (Core)169               CPUs: 2170               Total Memory: 979.7 MiB171               Name: localhost.localdomain172               ID: TFVB:BXGQ:VVOC:K2DJ:LECE:2HNK:23B2:LEVF:P3IQ:L7D5:NG2V:UKNL173               WARNING: bridge-nf-call-iptables is disabled174               WARNING: bridge-nf-call-ip6tables is disabled175 176     inspect   Return low-level information on a container or image177               --用于查看容器的配置信息,包含容器名、环境变量、运行命令、主机配置、网络配置和数据卷配置等。178 179     kill      Kill a running container 180               --强制终止容器181               关于stop和kill的区别,docker stop命令给容器中的进程发送SIGTERM信号,默认行为是会导致容器退出,当然,182               容器内程序可以捕获该信号并自行处理,例如可以选择忽略。而docker kill则是给容器的进程发送SIGKILL信号,该信号将会使容器必然退出。183 184     load      Load an image from a tar archive or STDIN185               --与下面的save命令相对应,将下面sava命令打包的镜像通过load命令导入186 187     login     Register or log in to a Docker registry188               --登录到自己的Docker register,需有Docker Hub的注册账号189               [root@localhost ~]# docker login190               Username: ivictor191               Password: 192               Email: xxxx@foxmail.com193               WARNING: login credentials saved in /root/.docker/config.json194               Login Succeeded195 196     logout    Log out from a Docker registry197               --退出登录198               [root@localhost ~]# docker logout199               Remove login credentials for https://index.docker.io/v1/200 201     logs      Fetch the logs of a container202               --用于查看容器的日志,它将输出到标准输出的数据作为日志输出到docker logs命令的终端上。常用于后台型容器203 204     pause     Pause all processes within a container205               --暂停容器内的所有进程,206               此时,通过docker stats可以观察到此时的资源使用情况是固定不变的,207               通过docker logs -f也观察不到日志的进一步输出。208 209     port      List port mappings or a specific mapping for the CONTAINER210               --输出容器端口与宿主机端口的映射情况211               譬如:212               [root@localhost ~]# docker port blog213               80/tcp -> 0.0.0.0:80214               容器blog的内部端口80映射到宿主机的80端口,这样可通过宿主机的80端口查看容器blog提供的服务215 216     ps        List containers  217               --列出所有容器,其中docker ps用于查看正在运行的容器,ps -a则用于查看所有容器。218 219     pull      Pull an image or a repository from a registry220               --从docker hub中下载镜像221 222     push      Push an image or a repository to a registry223               --将本地的镜像上传到docker hub中224               前提是你要先用docker login登录上,不然会报以下错误225               [root@localhost ~]# docker push ivictor/centos:v1226               The push refers to a repository [docker.io/ivictor/centos] (len: 1)227               unauthorized: access to the requested resource is not authorized228 229     rename    Rename a container230               --更改容器的名字231 232     restart   Restart a running container 233               --重启容器234 235     rm        Remove one or more containers 236               --删除容器,注意,不可以删除一个运行中的容器,必须先用docker stop或docker kill使其停止。237               当然可以强制删除,必须加-f参数238               如果要一次性删除所有容器,可使用 docker rm -f `docker ps -a -q`,其中,-q指的是只列出容器的ID239 240     rmi       Remove one or more images   241               --删除镜像242 243     run       Run a command in a new container   244               --让创建的容器立刻进入运行状态,该命令等同于docker create创建容器后再使用docker start启动容器245 246     save      Save an image(s) to a tar archive247               --将镜像打包,与上面的load命令相对应248               譬如:249               docker save -o nginx.tar nginx250 251     search    Search the Docker Hub for images   252               --从Docker Hub中搜索镜像253 254     start     Start one or more stopped containers255               --启动容器256 257     stats     Display a live stream of container(s) resource usage statistics258               --动态显示容器的资源消耗情况,包括:CPU、内存、网络I/O259 260     stop      Stop a running container 261               --停止一个运行的容器262 263     tag       Tag an image into a repository264               --对镜像进行重命名265 266     top       Display the running processes of a container267               --查看容器中正在运行的进程268 269     unpause   Unpause all processes within a container270               --恢复容器内暂停的进程,与pause参数相对应271 272     version   Show the Docker version information 273               --查看docker的版本274 275     wait      Block until a container stops, then print its exit code276               --捕捉容器停止时的退出码277               执行此命令后,该命令会“hang”在当前终端,直到容器停止,此时,会打印出容器的退出码。278 279 Run docker COMMAND --help for more information on a command.

 

docker命令详解