Docker命令

1.Docker简介

  Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。

  Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

  容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

  Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。

 

2.Docker和virtual

 

 

 


 

 

1.帮助命令

[1].docker version

[[email protected] ~]# docker version

[2].docker info

[[email protected] ~]# docker info

[3].docker –help

[[email protected] ~]# docker –help

2.镜像命令

[1].docker images

[[email protected] ~]# docker images

 

# -a:列出本地所有的镜像(含中间映像层)

[[email protected] ~]# docker images -a

 

# -q :只显示镜像ID

[[email protected] ~]# docker images -q

 

# –digests :显示镜像的摘要信息

[[email protected] ~]# docker images –digests

 

# –no-trunc :显示完整的镜像信息

[[email protected] ~]# docker images –no-trunc

  

选项

说明

REPOSITORY

表示镜像的仓库源

TAG

镜像的标签

IMAGE ID

镜像ID

CREATED

镜像创建时间

SIZE

镜像大小

  同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,我们使用REPOSITORY:TAG 来定义不同的镜像。

  如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像

[2].docker search

  语法:docker search [OPTIONS] 镜像名字

//hub.docker.com

范例:使用docker search

# –no-trunc : 显示完整的镜像描述

[[email protected] ~]# docker search tomcat

 

# -s : 列出收藏数不小于指定值的镜像。

[[email protected] ~]# docker search -s 30 tomcat

 

# –automated : 只列出 automated build类型的镜像;

[[email protected] ~]# docker search -s 30 –automated tomcat

 

[3].docker pull

语法:docker pull 镜像名字[:TAG]

TAG:表示版本信息

如:docker pull tomcat<==>docker pull tomcat:latest

[[email protected] ~]# docker pull tomcat

 

[3].docker rmi

语法:docker rmi 某个XXX镜像名字ID

 

[[email protected] ~]# docker rmi hello-world

Error response from daemon: Conflict, cannot delete fd74c3615f76 because the container f2b76e509604 is using it, use -f to force

Error: failed to remove images: [hello-world]

 

# -f:强制删除

[[email protected] ~]# docker rmi -f  hello-world

Untagged: hello-world:latest

Deleted: fd74c3615f76e548000499194419d582abcb4d037e28f302da98e09fe6082b07

Deleted: 482c215f2373801ac5205df8397741792cc3a1f2f8e8a13e042463d93b3b8364

 

#删除全部

[[email protected] ~]# docker rmi -f $(docker images -qa)

3.容器命令

[1].docker run

语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

交互式容器

OPTIONS

说明

–name=”容器新名字

为容器指定一个名称

-d:

后台运行容器,并返回容器ID,也即启动守护式容器;

-i

以交互模式运行容器,通常与 -t 同时使用;

-t

为容器重新分配一个伪输入终端,通常与 -i 同时使用;

-P

随机端口映射;

-p

指定端口映射,有以下四种格式

ip:hostPort:containerPort|ip::containerPort|hostPort:containerPort|containerPort

 

[[email protected] ~]# docker pull centos

[[email protected] ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

nginx               latest              9fcafc113f95        3 days ago          132.6 MB

tomcat              latest              74a339d90e37        11 days ago         647.4 MB

hello-world         latest              fd74c3615f76        7 months ago        13.34 kB

centos              6.8                 efa3cf7ee1f9        17 months ago       194.5 MB

 

#使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。

docker run -it centos /bin/bash

[[email protected] ~]# docker run -it efa3cf7ee1f9

[[email protected] /]#

[[email protected] /]# ls

bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var

[[email protected] /]# pwd

/

[[email protected] /]# ps -ef

UID         PID   PPID  C STIME TTY          TIME CMD

root          1      0  0 16:09 ?        00:00:00 /bin/bash

root         17      1  0 16:15 ?        00:00:00 ps -ef

[[email protected] /]#

  

  若运行docker pull centos时出现”FATAL: kernel too old”,则是pull centos的版本信息太高,只要指定版本信息即可解决

//blog.csdn.net/jia970426/article/details/104742818

[2].docker ps

语法:docker ps [OPTIONS]

 

[[email protected] ~]# docker ps

 

[3].docker exit

(1).exit

  容器停止退出

[[email protected] /]# exit

 

(2).ctrl+P+Q

  容器不停止退出

ctrl+P+Q

 

[4].docker start

语法:docker start 容器ID或者容器名

[[email protected] ~]# docker start f4883fc1b685

 

[5].docker restart

语法:docker restart 容器ID或者容器名

[[email protected] ~]# docker restart mycentos0115

 

[6].docker stop

语法:docker stop 容器ID或者容器名

[[email protected] ~]# docker stop 3b7f9ed258ec

[7].docker kill

语法:docker kill 容器ID或者容器名

[[email protected] ~]# docker kill f4883fc1b685

 

[8].docker rm

语法:docker rm 容器ID

[[email protected] ~]# docker rm f4883fc1b685

f4883fc1b685

  如图,因为f4883不存在了所以删除成功

 

范例:一次性删除多个容器

docker rm -f $(docker ps -a -q)

docker ps -a -q | xargs docker rm

[9].重要

(1).守护式容器

语法:docker run -d 容器名

 

(2).容器日志

语法:docker logs -f -t –tail 容器ID

 

[[email protected] ~]# docker run -d centos:6.8 /bin/sh -c “while true;do echo hello zzyy;sleep 2;done”

[[email protected] ~]# docker logs -t dd15d98a9800

[[email protected] ~]# docker logs -tf –tail 10 dd15d98a9800

 

(3).容器内进程

语法:docker top 容器ID

[[email protected] ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

dd15d98a9800        centos:6.8          “/bin/sh -c ‘while t   3 minutes ago       Up 3 minutes                            cranky_lalande        

e67ba0d33f76        centos:6.8          “/bin/sh -c ‘while t   5 minutes ago       Up 5 minutes                            compassionate_yalow   

[[email protected] ~]# docker top dd15d98a9800

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                8135                2906                0                   02:53               ?                   00:00:00            /bin/sh -c while true;do echo hello zzyy;sleep 2;done

 

(4).容器内细节

语法:docker inspect 容器ID

[[email protected] ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

dd15d98a9800        centos:6.8          “/bin/sh -c ‘while t   6 minutes ago       Up 6 minutes                            cranky_lalande        

e67ba0d33f76        centos:6.8          “/bin/sh -c ‘while t   7 minutes ago       Up 7 minutes                            compassionate_yalow   

[[email protected] ~]# docker inspect dd15d98a9800

 

(5).进入正在运行的容器并以命令交互

①.docker exec

语法:docker exec -it 容器ID bashShell

[[email protected] ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

17c2ee9ceeb5        centos:6.8          “/bin/bash”         22 seconds ago      Up 21 seconds                           high_banach         

[[email protected] ~]# docker exec -t 17c2ee9ceeb5 ls -l /tmp

②.docker attach

语法:docker attach 容器ID

[[email protected] ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

17c2ee9ceeb5        centos:6.8          “/bin/bash”         22 seconds ago      Up 21 seconds                           high_banach         

[[email protected] ~]# docker attach 17c2ee9ceeb5

 

exec和attach两者区别

 

(6).容器内拷贝文件到主机

语法:docker cp  容器ID:容器内路径 目的主机路径

[[email protected] ~]# docker ps

[[email protected] ~]# docker cp 17c2ee9ceeb5:/tmp/yum.log /root

 

4.总结

 

attach    Attach to a running container                 # 当前 shell attach 连接指定运行镜像

build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像

commit    Create a new image from a container changes   # 提交当前容器为新的镜像

cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中

create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器

diff      Inspect changes on a container’s filesystem   # 查看 docker 容器变化

events    Get real time events from the server          # docker 服务获取容器实时事件

exec      Run a command in an existing container        # 在已存在的容器上运行命令

export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

history   Show the history of an image                  # 展示一个镜像形成历史

images    List images                                   # 列出系统当前镜像

import    Create a new filesystem image from the contents of a tarball # tar包中的内容创建一个新的文件系统映像[对应export]

info      Display system-wide information               # 显示系统相关信息

inspect   Return low-level information on a container   # 查看容器详细信息

kill      Kill a running container                      # kill 指定 docker 容器

load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]

login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器

logout    Log out from a Docker registry server          # 从当前 Docker registry 退出

logs      Fetch the logs of a container                 # 输出当前容器日志信息

port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口

pause     Pause all processes within a container        # 暂停容器

ps        List containers                               # 列出容器列表

pull      Pull an image or a repository from the docker registry server   # docker镜像源服务器拉取指定镜像或者库镜像

push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器

restart   Restart a running container                   # 重启运行的容器

rm        Remove one or more containers                 # 移除一个或者多个容器

rmi       Remove one or more images             # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]

run       Run a command in a new container              # 创建一个新的容器并运行一个命令

save      Save an image to a tar archive                # 保存一个镜像为一个 tar [对应 load]

search    Search for an image on the Docker Hub         # docker hub 中搜索镜像

start     Start a stopped containers                    # 启动容器

stop      Stop a running containers                     # 停止容器

tag       Tag an image into a repository                # 给源中镜像打标签

top       Lookup the running processes of a container   # 查看容器中运行的进程信息

unpause   Unpause a paused container                    # 取消暂停容器

version   Show the docker version information           # 查看 docker 版本号

wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值

 

 

Tags: