Docker的常用基本命令

帮助命令

  • docker version #显示docker的版本信息
[root@localhost xiezuotao]# docker version
Client: Docker Engine - Community
 Version:           25.0.3
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        4debf41
 Built:             Tue Feb  6 21:17:10 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          25.0.3
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       f417435
  Built:            Tue Feb  6 21:16:08 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
  • docker info       #显示docker的系统信息,包括镜像和容器的数量
  • docker 命令 --help    #帮助命令,可以看到docker的常用命令

镜像命令

  • docker images  #查看所有本地的主机上的镜像
  • -a  #显示全部的镜像
[root@localhost xiezuotao]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
mysql         latest    3218b38490ce   2 years ago   516MB
hello-world   latest    feb5d9fea6a5   2 years ago   13.3kB
  • -q  #只显示镜像的id
[root@localhost xiezuotao]# docker images -q
3218b38490ce
feb5d9fea6a5
  • docker search   #搜索镜像     --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
[root@localhost xiezuotao]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                      STARS     OFFICIAL
mysql     MySQL is a widely used, open-source relation…   14854     [OK]
mariadb   MariaDB Server is a high performing open sou…   5674      [OK]
  • docker pull        #下载镜像
    #通过指定版本下载镜像
    
    [root@localhost xiezuotao]# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    72a69066d2fe: Already exists 
    93619dbc5b36: Already exists 
    99da31dd6142: Already exists 
    626033c43d70: Already exists 
    37d5d7efb64e: Already exists 
    ac563158d721: Already exists 
    d2ba16033dad: Already exists 
    0ceb82207cd7: Pull complete 
    37f2405cae96: Pull complete 
    e2482e017e53: Pull complete 
    70deed891d42: Pull complete 
    Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
    Status: Downloaded newer image for mysql:5.7
    docker.io/library/mysql:5.7
    
        
    [root@localhost xiezuotao]# docker pull mysql
    Using default tag: latest       #如果不写tag,默认就是 latest
    latest: Pulling from library/mysql  
    72a69066d2fe: Pull complete     #分层下载,docker image的核心   联合文件系统
    93619dbc5b36: Pull complete 
    99da31dd6142: Pull complete 
    626033c43d70: Pull complete 
    37d5d7efb64e: Pull complete 
    ac563158d721: Pull complete 
    d2ba16033dad: Pull complete 
    688ba7d5c01a: Pull complete 
    00e060b6d11d: Pull complete 
    1c04857f594f: Pull complete 
    4d7cfa90e6ea: Pull complete 
    e0431212d27d: Pull complete 
    Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest   #mysql真实地址
    
    #docker pull mysql 等价于  docker pull docker.io/library/mysql:latest
  • docker rmi            #删除镜像
docker rmi -f 镜像id   #强行删除指定镜像
[root@localhost xiezuotao]# docker rmi -f 3218b38490ce
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff

docker rmi -f 镜像id 镜像id 镜像id    #删除多个镜像

docker rmi -f $(docker images -aq)  #删除全部镜像

[root@localhost xiezuotao]# docker rmi $(docker images -aq)
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
Error response from daemon: conflict: unable to delete feb5d9fea6a5 (must be forced) - image is being used by stopped container fc89cb3194f3

容器命令

  •         下载一个centOS:7版本的镜像
[root@localhost xiezuotao]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
  • 新建容器并启动

docker run [可选参数] image

#参数说明

        --name="Name"      容器名字   

        -d                             后台方式运行

        -it                             使用交互方式运行,进入容器查看内容

        -p                             指定容器的端口   -p 8080:8080

        -P                             随机指定端口

#启动并进入容器

[root@localhost xiezuotao]# docker run -it centos /bin/bash
[root@c549c91c0d97 /]# 

exit    #退出容器                  Ctrl + P + Q # 容器不关闭退出

docker ps  命令


#查看正在运行的容器

[root@localhost xiezuotao]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES



docker ps -a   #列出当前正在运行的容器 + 历史运行过的容器

[root@localhost xiezuotao]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                      PORTS     NAMES
c549c91c0d97   centos        "/bin/bash"   2 minutes ago   Exited (0) 16 seconds ago             condescending_hypatia
ad459c62fee0   centos        "/bin/bash"   3 minutes ago   Exited (0) 3 minutes ago              silly_robinson
fc89cb3194f3   hello-world   "/hello"      2 hours ago     Exited (0) 2 hours ago                friendly_fermat



-n = ?   #显示最近创建的容器

[root@localhost xiezuotao]# docker ps -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
c549c91c0d97   centos    "/bin/bash"   6 minutes ago   Exited (0) 3 minutes ago             condescending_hypatia


-q    #只显示容器的编号
  • 删除容器
docker rm 容器id                #删除指定id容器  rm -f可以强制删除
docker rm -f $(docker ps -aq)   #删除所有容器
  • 启动和停止容器的操作
docker start 容器id      #启动容器
docker stop  容器id      #停止容器
docker restart 容器id    #重启容器
docker kill  容器id      #强制停止当前容器

你可能感兴趣的:(docker,容器,学习,笔记)