五、常用Docker命令

                            常用Docker命令

1、帮助命令

docker version  --版本信息
docker info     --完整信息
docker help     --帮助信息
[root@hadoop18 桌面]# docker help
Usage: docker [OPTIONS] COMMAND [arg...]

A self-sufficient runtime for linux containers.

Options:

  --api-cors-header=                   Set CORS headers in the remote API
  -b, --bridge=                        Attach containers to a network bridge
  --bip=                               Specify network bridge IP
  -D, --debug=false                    Enable debug mode
  -d, --daemon=false                   Enable daemon mode
  --default-gateway=                   Container default gateway IPv4 address
  --default-gateway-v6=                Container default gateway IPv6 address
  --default-ulimit=[]                  Set default ulimits for 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 subnet for fixed IPs
  --fixed-cidr-v6=                     IPv6 subnet for fixed IPs
  -G, --group=docker                   Group for the unix socket
  -g, --graph=/var/lib/docker          Root of the Docker runtime
  -H, --host=[]                        Daemon socket(s) to connect 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 when binding container ports
  --ip-forward=true                    Enable net.ipv4.ip_forward
  --ip-masq=true                       Enable IP masquerading
  --iptables=true                      Enable addition of iptables 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 driver for container logs
  --log-opt=map[]                      Set log driver options
  --mtu=0                              Set the containers network MTU
  -p, --pidfile=/var/run/docker.pid    Path to use for daemon PID file
  --registry-mirror=[]                 Preferred Docker registry mirror
  -s, --storage-driver=                Storage driver to use
  --selinux-enabled=false              Enable selinux support
  --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 certificate file
  --tlskey=~/.docker/key.pem           Path to TLS key file
  --tlsverify=false                    Use TLS and verify the remote
  --userland-proxy=true                Use userland proxy for loopback traffic
  -v, --version=false                  Print version information and quit

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        Copy files/folders from 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 the contents 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 log in to a Docker registry server
    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        List containers
    pull      Pull an image or a repository from a Docker registry server
    push      Push an image or a repository to a Docker registry server
    rename    Rename an existing container
    restart   Restart a running container
    rm        Remove one or more 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 a container
    unpause   Unpause a paused container
    version   Show the Docker version information
    wait      Block until a container stops, then print its exit code
docker命令参数        Usage: docker [OPTIONS] COMMAND [arg...]
OPTIONS:选项,可有可无
COMMAND:命令
arg:参数,可有可无

2、镜像命令

镜像信息显示:

docker images -a                 列出本地所有镜像
docker images -q                 只显示镜像ID
docker images -digests           显示镜像摘要信息
docker images -no-trunce         显示镜像完整信息

查找镜像:

docker search tomcat                         --在docker的远程仓库上查找tomcat镜像
docker search -s 30 tomcat                   --查找start超过30的tomcat
docker search -s 30 --no-trunc tomcat        --no-trunc:查看IMAGE ID没有被截断的镜像

docker search -s 30 --no-trunc --automated tomcat
                                             --只列出 automated build类型的镜像

拉取镜像:

docker pull tomcat     --从阿里云远程仓库上下载tomcat最新版本

删除镜像:

docker rmi tomcat						--删除

docker rmi  -f tomcat                     --强制删除

docker rmi 镜像名1:TAG  镜像名2:TAG        --删除多个镜像

docker rmi $(docker images -qa)          --删除全部镜像

3、容器命令

3.1、容器运行操作

新建、启动容器:

docker run [OPTIONS] IAMGE [COMMAND] [ARGS...]

OPTIONS选项解析:

--name              为容器起一个别名
-d                  后台运行容器(d是deamon守护进程),返回容器ID
-i                  以交互形式运行容器,interactive
-t                  为容器分配一个伪输入终端, terminal 
-P                  随机分配端口
-p                  指定分配端口

列出正在运行的所有容器:

docker  ps   [OPTIONS]

OPTIONS选项解析:

-a           列出历史上运行过、当前正在运行的容器
-l           最近运行的容器
-n           后面跟数字,表示最近运行的几个容器
-q           静默模式,只显示容器的编号 
--no-trunc   不截断输出

结果:

[root@hadoop18 桌面]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bd4d89eb50e9        centos              "/bin/bash"         3 minutes ago       Up 3 minutes                            adoring_lumiere  

容器退出:

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

启动容器:

docker start 容器id或者容器名

重启容器:

docker restart   容器id或者容器名

容器停止:

docker stop    容器id或者容器名

强制停止容器:

docker kill 容器id或者容器名

删除已经停止的容器:

docker rm 容器id

docker rm -f  容器id           强制删除

docker rm -f $(docker ps -aq)   删除所有容器
3.2、容器功能命令

以后台进程的方式启动容器:

[root@hadoop18 桌面]# docker run -d centos
2f070f8210672314ce8801d0cdac04f9fe2137504e1d3456ba5b1ac659d80273
[root@hadoop18 桌面]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@hadoop18 桌面]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
2f070f821067        centos              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       distracted_curie    
bd4d89eb50e9        centos              "/bin/bash"         3 hours ago         Exited (137) 3 hours ago                       adoring_lumiere     
b7539f04835d        hello-world         "/hello"            45 hours ago        Exited (0) 45 hours ago                        adoring_euclid      
0975772a14a1        hello-world         "/hello"            45 hours ago        Exited (0) 45 hours ago  

原理讲解:

docker run -d centos启动完成后,docker ps发现后台进程没有运行
原因:docker容器自动关闭该进程,因为他觉得没人用它,避免资源浪费
      docker容器后台运行,必须有一个前台进程,否则会被自动关闭

查看容器日志:

docker -f -t --tail 容器ID

-f              跟随最新的日志打印
-t 		        加入时间戳
--tail          显示最后多少条

查看docker容器内运行的进程:

docker top 容器ID

查看容器内部细节:

docker inpsect 容器ID

进入容器:

docker attach 容器ID

直接向容器内部执行命令:

docker exec -it 容器ID  待执行的命令

从容器内拷贝文件到宿主机:

docker cp 容器ID:容器内文件路径   宿主机保存文件路径
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值