The second common commands bomb Docker

Docker common commands

The underlying principle

  • How Docker works

    Docker is a Client-Server system structure, Docker daemon running on the host, and then connect the access from the client through the Socket, the daemon accepts commands from the client to run and manage the container on the host.
    Container is a runtime environment.

  • Why Docker faster than VM

(1) docker has less than a virtual machine abstraction layer. Hardware resources by the right foot docker do not need Hypervisor virtualization, hardware resources are actual physical machine to run the program on a docker container directly. So docker will have obvious advantages in efficiency on the CPU, memory utilization.

(2) docker host is using the kernel, without the need for Guest OS. Therefore, when a new container, docker and do not need to reload the virtual machine as an operating system kernel. Still seeking to avoid lead, load the operating system kernel returned a relatively time-consuming and resource-intensive process, when a new virtual machine, the virtual machine software to be loaded Guest OS, a new process is returned to minute levels. Because of the direct use of docker host operating system, then a return process is omitted, so a new container docker takes only a few seconds.

Common Commands

Help command

查看docker的版本
docker version

查看docker的详细信息
docker info

查看docker的帮助信息
docker --help

Mirroring Command

  • Glossary
  1. REPOSITORY: represents the repository of the source image
  2. TAG: Mirror labels
  3. IMAGE ID: Mirror
  4. IDCREATED: image creation time
  5. SIZE: image size

The same warehouse source can have multiple TAG, on behalf of the warehouse source different versions, we use REPOSITORY: TAG to define different image.
If you do not specify a mirrored version of the label, for example, you only use ubuntu, docker will default ubuntu: latest image

列出本地主机上的镜像
    docker images [OPTIONS]

    OPTIONS说明:
        -a :列出本地所有的镜像(含中间映像层)
        -q :只显示镜像ID。
        --digests :显示镜像的摘要信息
        --no-trunc :显示完整的镜像信息


搜索某个镜像
    docker search [OPTIONS]  某个XXX镜像名字

    在这个网站上搜索的,该网站类似github是docker的仓库
        https://hub.docker.com
        OPTIONS说明:
            --no-trunc : 显示完整的镜像描述
            --automated : 只列出 automated build类型的镜像;
            --help: 显示帮助

下载镜像
    docker pull 某个XXX镜像名字
    docker pull 镜像名字[:TAG]

    注:[:TAG] 为版本号码,可以不用写,默认为最新版本 latest

    例如:
        docker pull tomcat
        docker pull centos

删除镜像

 删除单个
    docker rmi 某个XXX镜像名字或ID

 删除多个
    docker rmi  -f 镜像ID

 删除全部
    docker rmi -f 镜像名1:TAG 镜像名2:TAG
    docker rmi -f $(docker images -qa)

Container command

有镜像才能创建容器,这是根本前提(下载一个CentOS镜像)
    docker pull centos


新建并启动容器
    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

    OPTIONS说明(常用):有些是一个减号,有些是两个减号

        --name="容器新名字": 为容器指定一个名称;
        -d: 后台运行容器,并返回容器ID,也即启动守护式容器;
        -i:以交互模式运行容器,通常与 -t 同时使用;
        -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
        -P: 随机端口映射;
        -p: 指定端口映射,有以下四种格式
              ip:hostPort:containerPort
              ip::containerPort
              hostPort:containerPort
              containerPort

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

列出当前所有正在运行的容器
    docker ps [OPTIONS]

    OPTIONS说明(常用):
        -a :列出当前所有正在运行的容器+历史上运行过的    docker ps -a
        -l :显示最近创建的容器。      docker ps -l
        -n:显示最近n个创建的容器。    docker ps -n 10  
        -q :静默模式,只显示容器编号。 docker ps -q

退出容器
    exit    容器停止退出
    ctrl+P+Q    容器不停止退出


启动容器
    docker start 容器ID或者容器名

重启容器
    docker restart 容器ID或者容器名

停止容器
    docker stop 容器ID或者容器名

强制停止容器
    docker kill 容器ID或者容器名

删除已停止的容器
    docker rm 容器ID
    一次性删除多个容器
        docker rm -f $(docker ps -a -q)
        docker ps -a -q | xargs docker rm



启动守护式容器
    docker run -d 容器名
查看容器日志
    docker logs -f -t --tail 容器ID
        *   -t 是加入时间戳
        *   -f 跟随最新的日志打印
        *   --tail 数字 显示最后多少条
查看容器内运行的进程
    docker top 容器ID
查看容器内部细节
    docker inspect 容器ID
    
进入正在运行的容器并以命令行交互
    docker exec -it 容器ID bashShell
        :exec命令可以在宿主主机外对docker的容器进行执行shell命令
        :例如:docker exec -it 容器ID  ls -l /
    
    重新进入docker attach 容器ID
    上述两个区别
        attach 直接进入容器启动命令的终端,不会启动新的进程
        exec 是在容器中打开新的终端,并且可以启动新的进程

从容器内拷贝文件到主机上
    docker cp  容器ID:容器内路径 目的主机路径

Commonly used commands cheat sheet

command is cn
attach Attach to a running container Run # current mirror connection designation attach the shell
build Build an image from a Dockerfile By Dockerfile customized image #
commit Create a new image from a container changes # Submit a new image for the current container
cp Copy files/folders from the containers filesystem to the host path # Copies the specified file or directory from the container to the host
create Create a new container # Create a new container, with the run, but do not start container
diff Inspect changes on a container's filesystem # View changes docker container
events Get real time events from the server # Get real-time events from the docker container service
exec Run a command in an existing container # Run command on an existing container
export Stream the contents of a container as a tar archive # Export the contents of the container as a tar archive stream [corresponding to Import]
history Show the history of an image # Show a mirror image formation history
images List images # List the current system image
import Create a new filesystem image from the contents of a tarball # Create a new file system image from the contents of a tar package [corresponding to export]
info Display system-wide information # Display information about the system
inspect Return low-level information on a container # View container details
kill Kill a running container # Kill specify docker container
load Load an image from a tar archive # Image from a tar loading a package [corresponding to save]
login Register or Login to the docker registry server # Register or log a docker source server
logout Log out from a Docker registry server # Exit from the current Docker registry
logs Fetch the logs of a container # Output current container logs
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # View port mapping the source port corresponding to the inside of the container
pause Pause all processes within a container # Pause container
ps List containers # List the container list
pull Pull an image or a repository from the docker registry server # Docker mirror from the source server specified pull or mirror the mirror
push Push an image or a repository to the docker registry server # Push specified mirror or mirrored to the library server docker source
restart Restart a running container # Restart operation of the vessel
rm Remove one or more containers Removing one or more containers #
rmi Remove one or more images # Remove one or more mirrors [no containers can delete the image, or need to delete the associated container in order to continue or forced to delete -f]
run Run a command in a new container # Create a new container and run a command
save Save an image to a tar archive # Tar save a mirror of a bag [corresponding to the 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 # 截取容器停止时的退出状态值

Guess you like

Origin www.cnblogs.com/upuptop/p/12013985.html