堕落不振功业废,勤耕不辍日月新

docker基础知识(一)

运维 hailen 54℃

安装docker

$ sudo apt install docker docker.io    #适用ubuntu 16.04

一,images管理
搜索kms相关的image

$ sudo docker search kms
NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
luodaoyi/kms-server                          kms server                                      21                   [OK]
lunker/kms                                   ubuntu image repositoiry for kms                1                    
leon0516/py-kms                              this is beta version!!!                         1                    [OK]
captbrick/py-kms                             Py-KMS                                          1                    [OK]
gkuzet/kms                                   KMS Server written in C                         1                    
openvidu/openvidu-server-kms                 OpenVidu platform service. It includes Ope...   1                    

比如第1个luodaoyi/kms-server,也可以用https://hub.docker.com/r/luodaoyi/kms-server/的方式查看相关信息.

下载想要的image(不必需)

$ sudo docker pull luodaoyi/kms-server

查看本地的images

$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
luodaoyi/kms-server   latest              4f86d964ac52        4 months ago        4.09 MB

$ sudo docker image ls
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
luodaoyi/kms-server   latest              4f86d964ac52        4 months ago        4.09 MB

重命名image,重命名完成以后原来的image名字仍然存在

$ sudo docker tag luodaoyi/kms-server mykms_image

$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
luodaoyi/kms-server   latest              4f86d964ac52        4 months ago        4.09 MB
mykms_image           latest              4f86d964ac52        4 months ago        4.09 MB

删除某个image

$ sudo docker rmi luodaoyi/kms-server    #删除本地某个image.注意如果该image有对应的container在运行,则需要先docker stop并docker rm对应的container再删除image.

二,container管理

相关命令
docker run image 创建并启动一个container,如果本地不存在该image则会从network下载之
docker create image 创建一个container(创建完是停止状态),如果本地不存在该image则会从network下载之
docker start/stop container_id 启动/停止container

$ sudo docker create test_image
Unable to find image 'test_image' locally

$ sudo docker create mykms_image
c2d1095f5c94d25992b913f49c85cf18c029cfc72d2fbc17d81e882939525240

$ sudo docker ps      #查看running的container(default shows just running),显示为空,因为没有start
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

$ sudo docker ps -a   #查看all container
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   16 seconds ago      Created                                 tender_mclean

$ sudo docker start c2d1095f5c94  #启动container
c2d1095f5c94

$ sudo docker ps    #查看running的container
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   2 minutes ago       Up 4 seconds        1688/tcp            tender_mclean

$ sudo docker ps -q   #查看running的container ID(-q表示只显示container ID)
c2d1095f5c94
#再次创建并运行一个container
#-p表示将host的1688端口转到container的1688端口
#-d表示Run container in background and print container ID
$ sudo docker run -itd -p 1688:1688 mykms_image
c2d83d2a33964868699dad4de80c4f097f4b3d01f53b4d3020f6f849df78fb1f

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3e4f50cdec9d        mykms_image         "/bin/sh -c 'vlmcs..."   3 minutes ago       Up 3 minutes        0.0.0.0:1688->1688/tcp   fervent_brattain
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   20 minutes ago      Up 3 seconds        1688/tcp                 tender_mclean

$ sudo docker stop 3e4f50cdec9d    #停止container(实测如果stop以后再start,那么刚才指定的端口转发依然有效)
3e4f50cdec9d

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   6 minutes ago       Up 4 minutes        1688/tcp            tender_mclean

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3e4f50cdec9d        mykms_image         "/bin/sh -c 'vlmcs..."   5 minutes ago       Exited (137) 2 seconds ago                       fervent_brattain
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   22 minutes ago      Up 2 minutes                 1688/tcp            tender_mclean

$ sudo docker rm 3e4f50cdec9d     #删除container(需要先stop之)
3e4f50cdec9d

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   9 minutes ago       Up 6 minutes        1688/tcp            tender_mclean

三,container进入与退出

进入container主要有以下2个命令
docker attach(不建议)
docker exec

1,使用docker attach进入container

$ sudo docker run -itd ubuntu-16 /bin/bash
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
aff533b466a7        ubuntu-16           "/bin/bash -e /ini..."   3 seconds ago       Up 2 seconds                            elastic_feynman

$ sudo docker attach aff533b466a7    #进入container
root@aff533b466a7:/#
root@aff533b466a7:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

#注意,此时如果执行exit或者按Ctrl+d退出,都会导致container被stop.
#正确的退出方法是: Ctrl+p+q(注意按键顺序,先按住Ctrl,再按住p,最后按一下q即可),这样退出以后container仍是running的状态.

2,使用docker exec进入container

$ sudo docker exec -it aff533b466a7 /bin/bash   #进入container内部去操作
root@aff533b466a7:/# 
root@aff533b466a7:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

此时,不论使用exit命令或者Ctrl+d(又或者Ctrl+p+q)任何方式退出container,该container都会保持running的状态.

$ sudo docker exec -it aff533b466a7 cat /etc/lsb-release #不进入container获得返回结果
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

四,docker相关信息查看

$ sudo docker system info
Containers: 8
 Running: 1
 Paused: 0
 Stopped: 7
Images: 3
Server Version: 17.03.2-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 26
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: N/A (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.13.0-32-generic
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.925 GiB
Name: bear-vm
ID: F4DM:C2XH:VX45:ZMNU:NW4E:F32J:BRY4:LSEG:CKMJ:AS5F:XMU7:SXNS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support
$ sudo docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              3                   3                   491.7 MB            0 B (0%)
Containers          8                   1                   284.2 MB            5.481 kB (0%)
Local Volumes       0                   0                   0 B                 0 B
$ sudo docker system df -v
Images space usage:

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE                SHARED SIZE         UNIQUE SIZE         CONTAINERS
1and1internet/ubuntu-16   latest              dc6431d887ca        4 days ago          483.5 MB            0 B                 483.5 MB            2
alpine                    latest              3fd9065eaf02        4 weeks ago         4.148 MB            0 B                 4.148 MB            4
mykms_image               latest              4f86d964ac52        4 months ago        4.094 MB            0 B                 4.094 MB            2

Containers space usage:

CONTAINER ID        IMAGE               COMMAND                  LOCAL VOLUMES       SIZE                CREATED             STATUS                           NAMES
7bef191b8e90        alpine              "/bin/sh"                0                   284 MB              22 minutes ago      Up 22 minutes                    jovial_hamilton
4b221cefcecb        ubuntu-16           "/bin/bash -e /ini..."   0                   2.69 kB             27 minutes ago      Exited (0) 26 minutes ago        festive_nobel
73dd66854efc        alpine              "/bin/sh"                0                   0 B                 52 minutes ago      Exited (137) 26 minutes ago      serene_northcutt
750f7f1a214f        alpine              "/bin/sh"                0                   49 B                53 minutes ago      Exited (137) 22 minutes ago      angry_easley
b01629f9cef7        alpine              "/bin/bash"              0                   0 B                 53 minutes ago      Created                          priceless_meninsky
aff533b466a7        ubuntu-16           "/bin/bash -e /ini..."   0                   2.74 kB             About an hour ago   Exited (0) 26 minutes ago        elastic_feynman
92af0b667ce2        mykms_image         "/bin/sh -c 'vlmcs..."   0                   0 B                 2 hours ago         Exited (137) 2 hours ago         relaxed_perlman
c2d1095f5c94        mykms_image         "/bin/sh -c 'vlmcs..."   0                   0 B                 2 hours ago         Exited (137) About an hour ago   tender_mclean

Local Volumes space usage:

VOLUME NAME         LINKS               SIZE

相关说明:
1,SHARED SIZE is the amount of space that an image shares with another one (i.e. their common data)
2,UNIQUE SIZE is the amount of space that is only used by a given image
3,SIZE is the virtual size of the image, it is the sum of SHARED SIZE and UNIQUE SIZE

转载请注明:我是IT » docker基础知识(一)

喜欢 (0)or分享 (0)