zoukankan      html  css  js  c++  java
  • docker 常用操作

    docker 操作

    docker

    [root@localhost ~]# docker --version
    Docker version 20.10.2, build 2291f61
    
    [root@localhost ~]# docker info
    Client:
     Context:    default
     Debug Mode: false
     Plugins:
      app: Docker App (Docker Inc., v0.9.1-beta3)
      buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
    
    Server:
     Containers: 5
      Running: 1
      Paused: 0
      Stopped: 4
     Images: 3
     Server Version: 20.10.2
     Storage Driver: overlay2
      Backing Filesystem: xfs
      Supports d_type: true
      Native Overlay Diff: true
     Logging Driver: json-file
     Cgroup Driver: cgroupfs
     Cgroup Version: 1
     Plugins:
      Volume: local
      Network: bridge host ipvlan macvlan null overlay
      Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
     Swarm: inactive
     Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
     Default Runtime: runc
     Init Binary: docker-init
     containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
     runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
     init version: de40ad0
     Security Options:
      seccomp
       Profile: default
     Kernel Version: 3.10.0-957.el7.x86_64
     Operating System: CentOS Linux 7 (Core)
     OSType: linux
     Architecture: x86_64
     CPUs: 1
     Total Memory: 1.953GiB
     Name: localhost.localdomain
     ID: LJBA:GLRT:HWNT:67PT:RKZX:OMMI:BUHA:S34N:DB3I:UFMC:L7DR:GVPU
     Docker Root Dir: /var/lib/docker
     Debug Mode: false
     Registry: https://index.docker.io/v1/
     Labels:
     Experimental: false
     Insecure Registries:
      127.0.0.0/8
     Registry Mirrors:    # 镜像加速
      http://hub-mirror.c.163.com/
     Live Restore Enabled: false
    

    image

    container基础

    验证container 的start、stop、run、ls 、ls -a

    # 当前有三个镜像
    [root@localhost ~]# docker image ls
    REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
    star7th/showdoc   latest    621a7b64fc9d   20 hours ago   669MB
    busybox           latest    a77dce18d0ec   9 days ago     1.24MB
    nginx             latest    ae2feff98a0c   3 weeks ago    133MB
    
    # 一个容器都没有启动
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    # 启动一个容器,这个说法并不对,启动容器之前应该先创建一个容器,但创建就是创建,并不会启动
    [root@localhost ~]# docker container create nginx
    1cdb6125ed01fb2f5a798452809ad2c1bea0a378bb887cf79919e44d1f58916c
    
    #查看容器,一个容器当前状态是 created
    [root@localhost ~]# docker container ls -a
    ed01  nginx   "/docker-entrypoint.…"     状态 Created  competent_buck 
    
    # 启动容器,启动容器时的名字要用names
    [root@localhost ~]# docker container start nginx
    Error response from daemon: No such container: nginx
    Error: failed to start containers: nginx
    
    [root@localhost ~]# docker container start  competent_buck
    competent_buck
    
    
    # ls只会识别到已经启动的,ls -a 会识别到所有的,包括没有启动的;
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS            
    5ed01   nginx     "/docker-entrypoint.…"   4  Up    80/tcp    competent_buck
    
    # 通过docker container run 等于create+start
    
    [root@localhost ~]# docker container stop competent_buck
    competent_buck
    
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    [root@localhost ~]# docker container run nginx
    
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
    1d3446faa119   nginx     "/docker-entrypoint.…"   31 seconds ago   Up 31 seconds   80/tcp    dazzling_joliot
    
    # 验证容器stop、pause、物理机重启、kill、之后,内部的资料是否还存在
    
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
    1d3446faa119   nginx     "/docker-entrypoint.…"   57 minutes ago   Up 57 minutes   80/tcp    dazzling_joliot
    
    [root@localhost ~]# docker exec -it dazzling_joliot /bin/bash
    root@1d3446faa119:/# touch 8888888888
    root@1d3446faa119:/# echo 888888 > 8888888888 
    root@1d3446faa119:/# exit
    exit
    
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS             PORTS     NAMES
    1d3446faa119   nginx     "/docker-entrypoint.…"   About an hour ago   Up About an hour  
    80/tcp    dazzling_joliot
    
    [root@localhost ~]# docker container stop dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container start dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container start dazzling_joliot
    dazzling_joliot
    
    #经过验证stop然后start之后依然存在
    [root@localhost ~]# docker container exec -it dazzling_joliot /bin/bash
    root@1d3446faa119:/# ls
    8888888888 
    
    # 经过验证pause和unpause之后依然存在
    [root@localhost ~]# docker container pause dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                  PORTS     NAMES
    1d3446faa119   nginx     "/docker-entrypoint.…"   About an hour ago   Up 5 minutes (Paused)   80/tcp    dazzling_joliot
    [root@localhost ~]# docker container unpause dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container exec -it dazzling_joliot /bin/bash
    root@1d3446faa119:/# ls
    8888888888
    
    # 经过验证,kill之后,通过start启动之后,依然存在
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS         PORTS     NAMES
    1d3446faa119   nginx     "/docker-entrypoint.…"   About an hour ago   Up 7 minutes   80/tcp    dazzling_joliot
    [root@localhost ~]# docker container kill dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    [root@localhost ~]# docker container ls -a | grep dazzling_joliot
    1d3446faa119   nginx             "/docker-entrypoint.…"   About an hour ago        Exited (137) 22 seconds ago                     dazzling_joliot
    [root@localhost ~]# docker container start dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container exec -it dazzling_joliot /bin/bash
    root@1d3446faa119:/# ls
    8888888888
    
    # 验证docker容器的删除
    [root@localhost ~]# docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS         PORTS     NAMES
    1d3446faa119   nginx     "/docker-entrypoint.…"   About an hour ago   Up 2 minutes   80/tcp    dazzling_joliot
    
    # 容器在启动状态下无法删除
    [root@localhost ~]# docker container rm dazzling_joliot
    Error response from daemon: You cannot remove a running container 1d3446faa11959c0dd73b0e1d5ee91e46e4678668b5261baf7e3a9a47a1710af. Stop the container before attempting removal or force remove
    
    [root@localhost ~]# docker container stop dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container rm dazzling_joliot
    dazzling_joliot
    [root@localhost ~]# docker container ls -a | grep dazzling_joliot   # 没有了
    
    # 那删除容器之后,再通过镜像启动一个容器,那之前修改的东西还吗?不在了,因为镜像是只读的,下面就来验证一下;
    
    # 删除之后再启动同样的镜像,数据就会没有了,如下所示:
    [root@localhost ~]# docker container run -d nginx
    16b3893fa42895ac7e977e601d88a1b1d8f7cf0cf12b4e8d18fcc63758e60e80
    
    [root@localhost ~]# docker container run -d nginx
    16b3893fa42895ac7e977e601d88a1b1d8f7cf0cf12b4e8d18fcc63758e60e80
    [root@localhost ~]# docker container ls 
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
    16b3893fa428   nginx     "/docker-entrypoint.…"   31 seconds ago   Up 30 seconds   80/tcp    laughing_dhawan
    [root@localhost ~]# docker container exec -it laughing_dhawan /bin/bash
    root@16b3893fa428:/# ls
    
    # 删除操作,删除所有的容器
    [root@localhost ~]# docker container ls -a
    CONTAINER ID   IMAGE             COMMAND                  CREATED                  STATUS                                PORTS     NAMES
    dbec26507405   star7th/showdoc   "docker-php-entrypoi…"   Less than a second ago   Exited (137) Less than a second ago             heuristic_buck
    16b3893fa428   nginx             "/docker-entrypoint.…"   6 minutes ago            Exited (0) 39 seconds ago                       laughing_dhawan
    1cdb6125ed01   nginx             "/docker-entrypoint.…"   2 hours ago              Exited (0) About an hour ago                    competent_buck
    22bda9eb539c   nginx             "/docker-entrypoint.…"   2 hours ago              Exited (0) 2 hours ago                          sleepy_greider
    6ebc79a349b4   busybox:latest    "sh"                     15 hours ago             Exited (0) 15 hours ago                         b1
    
    
    [root@localhost ~]# docker container ls -a
    CONTAINER ID   IMAGE             COMMAND                  CREATED                  STATUS                                PORTS     NAMES
    dbec26507405   star7th/showdoc   "docker-php-entrypoi…"   Less than a second ago   Exited (137) Less than a second ago             heuristic_buck
    16b3893fa428   nginx             "/docker-entrypoint.…"   6 minutes ago            Exited (0) 39 seconds ago                       laughing_dhawan
    1cdb6125ed01   nginx             "/docker-entrypoint.…"   2 hours ago              Exited (0) About an hour ago                    competent_buck
    22bda9eb539c   nginx             "/docker-entrypoint.…"   2 hours ago              Exited (0) 2 hours ago                          sleepy_greider
    6ebc79a349b4   busybox:latest    "sh"                     15 hours ago             Exited (0) 15 hours ago                         b1
    [root@localhost ~]# docker container rm heuristic_buck
    heuristic_buck
    [root@localhost ~]# docker container rm laughing_dhawan
    laughing_dhawan
    [root@localhost ~]# docker container rm competent_buck
    competent_buck
    [root@localhost ~]# docker container rm sleepy_greider
    sleepy_greider
    [root@localhost ~]# docker container rm b1
    b1
    [root@localhost ~]# docker container ls -a
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
  • 相关阅读:
    (网页)中的简单的遮罩层
    (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
    (网页)jQuery的时间datetime控件在AngularJs中使用实例
    Maven Myeclipse 搭建项目
    MyBatis 环境搭建 (一)
    java 常用方法
    XML 基础
    JS BOM
    js 事件
    js 的使用原则
  • 原文地址:https://www.cnblogs.com/yizhangheka/p/14251284.html
Copyright © 2011-2022 走看看