docker 1.2 之docker基本用法

語言: CN / TW / HK

1.docker的基本用法

映象相關操作:dockerhub查詢映象,例如查詢centos的映象

[[email protected] ~]# docker search centos
NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                       The official build of CentOS.                   7142      [OK]       
centos/systemd                               systemd enabled base container.                 108                  [OK]
centos/mysql-57-centos7                      MySQL 5.7 SQL database server                   94                   
centos/postgresql-96-centos7                 PostgreSQL is an advanced Object-Relational …   45                   
kasmweb/centos-7-desktop                     CentOS 7 desktop for Kasm Workspaces            19                   
kasmweb/core-centos-7                        CentOS 7 base image for Kasm Workspaces         3                    
continuumio/centos5_gcc5_base                                                                3                    
couchbase/centos7-systemd                    centos7-systemd images with additional debug…   0                    [OK]
datadog/centos-i386                                                                          0                    
couchbase/centos-72-jenkins-core                                                             0                    
couchbase/centos-72-java-sdk                                                                 0                    
fnndsc/centos-python3                        Source for a slim Centos-based Python3 image…   0                    [OK]
bitnami/centos-extras-base                                                                   0                    
couchbase/centos-69-sdk-build                                                                0                    
couchbase/centos-70-sdk-build                                                                0                    
spack/centos6                                CentOS 6 with Spack preinstalled                0                    
spack/centos-stream                                                                          0                    
starlingx/stx-centos                         StarlingX centos                                0                    
ibmcom/fhe-toolkit-centos-amd64              The IBM Fully Homomorphic Encryption (FHE) T…   0                    
ibmcom/fhe-toolkit-centos                    The IBM Fully Homomorphic Encryption (FHE) T…   0                    
apache/couchdbci-centos                      Apache CouchDB CI CentOS                        0                    
silintl/openldap                             OpenLDAP base image on Centos 6                 0                    [OK]
bitnami/centos-base-buildpack                Centos base compilation image                   0                    [OK]
spack/centos7                                CentOS 7 with Spack preinstalled                0                    
couchbase/centos-69-sdk-nodevtoolset-build                                                   0                    

2.下載映象,下載centos的

[[email protected] ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

3.檢視本地映象:

[[email protected] ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   8 months ago   231MB

4.把映象做成離線壓縮包,格式:docker save -o   壓縮包格式   壓縮包名

[[email protected] ~]# docker save -o centos.tar.gz centos

5.解壓離線映象包

[[email protected] ~]# docker load -i centos.tar.gz
Loaded image: centos:latest

6.刪除映象

[[email protected] ~]# docker rmi -f centos
Untagged: centos:latest
Untagged: [email protected]:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff8

7.啟動docker

(1)以互動式方式啟動並進去容器

[[email protected] ~]# docker run --name=hello centos /bin/bash

輸入exit,退出容器,退出之後容器也會停止,不會再前臺執行

#docker run執行並建立容器

--name 容器的名字

-i 互動式

-d  後臺執行docker

-t 分配偽終端

centos: 啟動docker需要的映象

/bin/bash說明你的shell型別為bash

(2)以守護程序方式啟動容器

[[email protected] ~]# docker run --name=zhao -td centos  /bin/bash
4c74154517e15ed57c9066789bbde0b01c9f67fd72366e1e84dfb7e40114b443

8.檢視容器是否啟動

[[email protected] ~]# docker ps |grep zhao
4c74154517e1   centos    "/bin/bash"   47 seconds ago   Up 46 seconds

9.登入到容器中

[[email protected] ~]# docker exec -it zhao /bin/bash
[[email protected] /]#          顯示前面主機名更換了說明就是登入到容器中了

 
exit                            #退出容器


[[email protected] ~]# docker ps         #檢視正在執行的容器
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
4c74154517e1   centos    "/bin/bash"   3 minutes ago   Up 3 minutes 



[[email protected] ~]# docker ps -a       #檢視所有容器,包含正在執行和不在執行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c74154517e1 centos "/bin/bash" 4 minutes ago Up 4 minutes zhao
19b4cafa3d13 centos "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago hello
 
[[email protected] ~]# docker stop zhao    #停止容器
 
[[email protected] ~]# docker start zhao   #啟動已經停止的容器
 
[[email protected] ~]# docker rm -f hello  #刪除容器