Docker

Posted by 杨一 on 2020-07-04

Docker

基础操作

1
docker image ls
  • REPOSITORY: 来自于哪个仓库;
  • TAG: 镜像的标签信息,比如 5.7、latest 表示不同的版本信息;
  • IMAGE ID: 镜像的 ID, 如果您看到两个 ID 完全相同,那么实际上,它们指向的是同一个镜像,只是标签名称不同罢了;
  • CREATED: 镜像最后的更新时间;
  • SIZE: 镜像的大小,优秀的镜像一般体积都比较小,这也是我更倾向于使用轻量级的 alpine 版本的原因;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
docker tag 命令功能为指定镜像添加快捷方式

docker inspect docker.io/mysql:5.7 docker inspect 命令,我们可以获取镜像的详细信息,其中,包括创建者,各层的数字摘要等

docker inspect -f {{".Size"}} docker.io/mysql:5.7 获取其中指定的一项内容,可以通过 -f 来指定,如获取镜像大小;

docker search--help
可以看到 search 支持的子命令有:
-f,--filter filter: 过滤输出的内容;
--limitint:指定搜索内容展示个数;
--no-index: 不截断输出内容;
--no-trunc:不截断输出内容
docker search --filter=is-official=true mysql
docker search --filter=stars=100 mysql

docker image rm [image]
docker rmi [image]
docker rmi -f docker.io/alpine 强制删除
docker rm 9d59e2278553 先删除容器ID
docker rmi 5cb3aa00f899 再删除镜像ID
docker image prune 清理镜像


docker container commit 基于已有的镜像创建
-a,--author="": 作者信息;
-c,--change=[]: 可以在提交的时候执行 Dockerfile 指令,如 CMD、ENTRYPOINT、ENV、EXPOSE、LABEL、ONBUILD、USER、VOLUME、WORIR 等;
-m,--message="": 提交信息;
-p,--pause=true: 提交时,暂停容器运行。

让我将它运行起来,并在其中创建一个 test.txt 文件:
docker run -it docker.io/ubuntu:latest /bin/bashroot@a0a0c8cfec3a:/# touch test.txtroot@a0a0c8cfec3a:/# exit
创建完 test.txt 文件后,需要记住标注的容器 ID: a0a0c8cfec3a, 用它来提交一个新的镜像(PS: 你也可以通过名称来提交镜像,这里只演示通过 ID 的方式)。
执行命令:
docker container commit -m "Added test.txt file" -a "Allen" a0a0c8cfec3a test:0.1
docker images 再次查看本地镜像

基于 Dockerfile 创建:
编写一个简单的 Dockerfile 文件,它描述了基于 Ubuntu 父镜像,安装 Python3 环境的镜像:
FROM docker.io/ubuntu:latest
LABEL version="1.0" maintainer="Allen <weiwosuo@github>"
RUN apt-get update && \ apt-get install -y python3 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*


docker image build -t python:3 . 通过这个 Dockerfile 文件,来构建新的镜像

docker save -o python_3.tar python:3 导出tar包镜像
docker load -i python_3.tar 加载导出的tar包

docker login 登录Docker Hub 上传本地镜像
docker tag python:3 weiwosuoai1991/python:3 我们对其打一个新的标签,前缀与我们新创建的 Docker ID 、仓库名保持一致;
docker images 查看本地标签打包成功
docker push weiwosuoai1991/python:3 开始上传!