【学习笔记】02.Docker
这篇文章发布于 748 天前,可能包含过期内容、失效链接、中二言论等,请谨慎甄别后引用。

02.Docker (2022.04.09)

弱小和无知不是生存的障碍,傲慢才是
即使再小的帆,也能远航

本章目录

  • Docker命令
    • 帮助命令
    • 镜像命令
      • 搜索镜像
      • 镜像下载
      • 删除镜像
    • 容器命令
      • 新建容器并启动
      • 列出所有容器
      • 退出容器
      • 删除容器
      • 启动和停止容器
    • 常用的其他命令
      • 后台启动容器
      • 查看日志
      • 查看容器中的进程信息
      • 查看容器的元信息
      • 进入当前正在运行的容器
      • 从容器拷贝文件到宿主
    • 小节

导图

Mind


Docker的常用命令

帮助命令

docker version         #版本查询命令
docker info            #详细信息的命令
docker Command --help  #help命令

镜像命令

[root@miaomc-210916 ~] docker images
REPOSITORY                        TAG       IMAGE ID       CREATED         SIZE
prom/prometheus                   latest    514e6a882f6e   6 weeks ago     204MB
ubuntu                            20.04     d13c942271d6   3 months ago    72.8MB
whyour/qinglong                   latest    5997b3824cbe   3 months ago    434MB
docker                            latest    15a9bc7c6340   3 months ago    216MB
prom/node-exporter                latest    1dbe0e931976   4 months ago    20.9MB
portainer/portainer-ee            latest    12043031b306   4 months ago    261MB
hello-world                       latest    feb5d9fea6a5   6 months ago    13.3kB

#解释
REPOSITORY  镜像仓库源
TAG         镜像标签
IMAGE ID    镜像ID
CREATED     镜像创建时间
SIZE        镜像大小

#可选项
[root@miaomc-210916 ~] docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
  #列出Docker中的所有镜像
      --digests         Show digests
      #展示镜像的摘要
  -f, --filter filter   Filter output based on conditions provided
  #筛选镜像
      --format string   Pretty-print images using a Go template
      #格式化字符串
      --no-trunc        Dont truncate output
      #不要截断输出
  -q, --quiet           Only show image IDs
  #仅显示镜像 ID

搜索镜像

[root@miaomc-210916 ~] docker search mysql
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                            MySQL is a widely used, open-source relation…   12377     [OK]       
mariadb                          MariaDB Server is a high performing open sou…   4762      [OK]       

#可以通过可选项来过滤镜像
    --filter=STARTS=3000 #搜索STARTS大于3000的镜像

#命令长这样
docker search mysql --filter=STARTS=3000

镜像下载

#使用Docker pull 镜像名称:Tag进行镜像的下载
[root@miaomc-210916 ~] docker pull mysql
Using default tag: latest #如果不写就是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 #真实地址

#若我期望下载MYSQL5.7版本
[root@miaomc-210916 ~] docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists #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@miaomc-210916 ~] docker rmi -f 3218b38490ce #删除ID为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

#批量删除所有的镜像
[root@miaomc-210916 ~] docker rmi -f 镜像ID1 (镜像ID2) #可同时删除多个镜像
[root@miaomc-210916 ~] docker rmi -f $(docker images -aq) #删除docker images -aq列出的所有镜像

容器命令

说明:现有镜像才能创建容器

新建容器并启动

docker run [设置] IMAGE [命令] [ARG...]

#参数说明
--name="DisplayName" #重启名称,用于区分容器
-d                   #以后台的方式运行,类似nohup
-it                  #使用交互方式运行
-p                   #指定容器的端口 -p 80:80
    -p ip:主机端口:容器端口
    -p 主机端口:容器端口 (常用)
    -p 容器端口
    容器端口

-P                   #随机指定容器端口

#启动并进入容器
[root@miaomc-210916 ~] docker run -it centos:centos7.9.2009 /bin/bash
[root@45ecb97d75ce /] ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

#退出容器
[root@45ecb97d75ce /] exit

列出所有容器

docker ps [设置] [命令]

[none]    #列出所有运行中的容器
-a        #列出所有容器,包括未运行的
-n=[int]  #列出最近运行的[int]个容器
-q        #列出所有运行中的容器编号

#列出所有运行中的容器
[root@miaomc-210916 ~] docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED        STATUS        PORTS                                                                                                                               NAMES

#列出所有容器,包括未运行的
[root@miaomc-210916 ~] docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS                     PORTS                                                                                                                               NAMES
45ecb97d75ce   centos:centos7.9.2009           "/bin/bash"              3 minutes ago   Exited (0) 3 minutes ago                                                                                                                                       charming_lichterman

退出容器

exit
#若使用exit退出容器的话,容器会停止运行

Ctrl + P + Q
#容器不停止运行退出容器

删除容器

docker rm 容器ID (容器ID2)       #删除指定的容器(不能删除正在运行的容器 ,强制删除需要添加 -f 参数)
docker rm -f $(docker ps -aq)   #递归删除所有的容器
docker ps -aq|xargs docker rm   #递归删除所有的容器

启动和停止容器

docker start [容器ID]    #启动容器
docker restart [容器ID]  #重启容器
docker stop [容器ID]     #停止容器
docker kill [容器ID]     #强制重启容器

常用的其他命令

后台启动容器

#命令 docker run -d 镜像名称
[root@miaomc-210916 ~] docker run -d centos

#需要注意的是,Docker使用后台运行就必须要有一个前台进程,Docker发现没有应用,就会自动杀死容器
#Nginx,容器启动后,发现自己没有提供服务,就会自杀 (发现自己是个乐色,就自杀了,真是个悲伤的故事)

查看日志

#查看帮助
[root@miaomc-210916 ~] docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

docker logs 
    -tf             #显示日志 f代表输出最新产生的日志 t表示日志的时间戳
    --tail [number] #展示指定条数的日志

#类似这样
[root@miaomc-210916 ~] docker logs -tf --tail 10 e9b637f542c7

查看容器中的进程信息


docker top [容器ID]

[root@miaomc-210916 ~] docker top e9b637f542c7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                27557               27510               0                   4月08                ?                   00:00:16

查看容器的元信息

docker inspect [容器ID]

进入当前正在运行的容器

通常情况下,容器都是使用后台的方式运行的,如果需要进入容器,修改一些配置的话

#方式一
docker exec [设置] [容器ID] [命令] [ARG...]

#实践
[root@miaomc-210916 ~] docker exec -it 45ecb97d75ce /bin/bash
[root@45ecb97d75ce /] ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@45ecb97d75ce /]

#方式二
docker attach [设置] [容器ID]
#实践
[root@miaomc-210916 ~] docker attach 45ecb97d75ce
正在执行代码....

docker exec    #进入容器后会开启一个新得终端,可以在里面操作
docker attach  #进入容器正在执行的终端,不会启动新的进程

从容器拷贝文件到宿主

docker cp 容器ID:容器内路径 宿主路径
尝试将所有命令再执行一遍,加深印象

小节

Docker

【学习笔记】02.Docker
站点链接:https://ali-a.cn/s/notes-02docker
如需转载请标明出处,谢谢您的配合。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇