1.Docker镜像
Docker运行容器前需要本地存在对应的镜像,如果镜像不存在,会尝试从远端镜像仓库中拉取
1.1 拉取镜像
# 完整写法
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
# 简略写法
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
docker pull hello-world:latest
NAME
:表示镜像仓库名称
TAG
:表示镜像的标签,通常用来表示镜像的版本信息
通常情况下,描述一个镜像需要包括名称和标签。如果在拉取镜像不显示的指定TAG,则会默认选择latest
标签,这会拉取镜像仓库中最新版本的镜像
1.2 查看镜像信息
1.2.1 列出所有镜像
docker image ls [OPTIONS] [REPOSITORY[:TAG]]
docker images [OPTIONS] [REPOSITORY[:TAG]]
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
字段说明:
REPOSITORY
:镜像仓库
TAG
:镜像标签,通常用于表示版本号
IMAGE ID
:镜像id,镜像唯一标识,如果两个镜像的镜像id相同,说明他们都指向同一个镜像,只是镜像标签不同
CREATED
:镜像的创建时间
SIZE
:镜像的大小,只是表示了该镜像的逻辑体积,实际上由于相同的镜像层在本地只会存储一份,因此本地占用的存储空间会小于各镜像逻辑体积之和。
1.2.2 列出部分镜像
根据仓库名列出镜像
root@stone:~# docker image ls ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 20.04 ba6acccedd29 12 months ago 72.8MB
1.2.3 列出虚悬镜像
docker image ls -f dangling=true
1.3 删除镜像
docker image rm [OPTIONS] IMAGE [IMAGE...]
docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image(强制删除镜像,当有该镜像创建的容器存在时,镜像文件默认无法删除)
--no-prune Do not delete untagged parents(不清理未带标签的父镜像)
1.3.1 使用镜像名称删除
docker rmi REPOSITORY:TAG [REPOSITORY:TAG]
当前我们存在两个镜像,它们的镜像id相同,tag不同
REPOSITORY TAG IMAGE ID CREATED SIZE
my-world 1.0 feb5d9fea6a5 13 months ago 13.3kB
my-world 3.0 feb5d9fea6a5 13 months ago 13.3kB
执行删除命令,可以看到删除了镜像feb5d9fea6a5一个标签副本,并没有真正的删除镜像
root@stone:~# docker rmi my-world:3.0
Untagged: my-world:3.0
如果镜像只剩下一个标签的时候,该镜像将会被删除
1.3.2 使用镜像id删除
docker rmi IMGAE_ID [IMAGE_ID...]
直接使用镜像id删除,会先untagged指向该镜像的标签,然后删除掉镜像本身
1.3.3 删除无用镜像
docker image prune
1.4 创建镜像
1.4.1 基于已有的容器创建
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
docker container commit -m "test create image" 0714643ba560 my-world:2.0
1.4.2 基于Dockerfile创建
Dockerfile是一个文本文件,利用给定的指令描述基于某个父镜像创建新镜像的过程
docker [image] build -t python:3 .
docker [image] build -t python:3 . -f /file/Dockerfile
.
:这里的.
是在指定构建上下文目录,即指定当前目录为构建上下文- 如果没有显示指定
Dockerfile
文件的路径,会从构建上下文目录中查找,如果构建上下文没有Dockerfile
,会报错。可以使用-f
指定Dockerfile
文件的路径
1.4.3
基于已经存在的镜像,创建不同tag的镜像
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
1.4.4 导出导入
导出
root@stone:~# docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
导入
root@stone:~# docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
1.5 上传镜像
docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
-a, --all-tags Push all tagged images in the repository
--disable-content-trust Skip image signing (default true)
-q, --quiet
# registry.cn-hangzhou.aliyuncs.com/stonetest表示镜像仓库地址,test1表示镜像仓库名称
docker push registry.cn-hangzhou.aliyuncs.com/stonetest/test1:[镜像版本号]
将镜像上传的Docker镜像仓库,例如阿里云的镜像仓库,我们需要登录Docker镜像仓库
docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
docker login --username=xxx registry.cn-hangzhou.aliyuncs.com
1.6 虚悬镜像
虚悬镜像是指既仓库名、标签均为
<none>
的镜像。docker pull和docker build都有可能导致这种现象。由于新旧镜像同名,旧镜像名称被取消,从而出现仓库名、标签均为<none>
的镜像。
查看所有虚悬镜像
docker image ls -f dangling=true
删除无用镜像
docker image prune
2.Docker容器
容器是镜像的一个运行实例
2.1 新建容器
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new container
# 选择部分常用选项
Options:
-d, --detach 是否在后台运行容器
--entrypoint string 镜像存在entrypoint命令时,覆盖为新的entrypoint命令
-e, --env list 指定容器内的环境变量
--env-file list 从文件中读取环境变量到容器内
--expose list 指定容器要暴露出来的端口或端口范围
-h, --hostname string 指定容器内的主机名
-i, --interactive 保持标准输入打开
--link list 链接到其他容器
--name string 指定容器的名称
-p, --publish list 映射容器端口到宿主机端口
--restart string 容器重启策略。no、always等
-t, --tty 是否分配一个伪终端。
-v, --volume list 挂载宿主机的数据卷到容器内
--volumes-from list 挂载其他容器的数据卷
docker create -it my-world:2.0
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bec37ce844b8 my-world:2.0 "/hello" 2 minutes ago Created
2.2 运行容器
docker start [container id]
2.3 新建并运行容器
# 等价于先执行创建容器再启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
当执行docker run来创建并启动容器时,Docker在后台会执行一下操作:
- 检查本地是否存在指定的镜像,不存在则从镜像仓库下载
- 使用镜像创建一个容器,并启动该容器
- 分配一个文件系统给容器,并在只读的镜像层外面挂载一层可读写层
- 从宿主机配置的网桥接口中桥接一个虚拟接口到容器中
- 从网桥的地址池配置一个IP地址给容器
- 执行用户指定的应用程序
- 执行完毕后容器被自动终止
2.3 暂停容器
docker pause CONTAINER [CONTAINER...]
2.3 停止容器
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
2.4 进入容器
docker exec -it CONTAINER bash
2.5 查看容器
查看容器详情
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
查看容器内进程
docker top CONTAINER [ps OPTIONS]
Display the running processes of a container
查看统计信息
显示CPU、内存、存储、网络等使用情况的统计信息
docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output
2.6 删除容器
删除指定容器
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
删除所有停止运行的容器
docker container prune [OPTIONS]
Remove all stopped containers
Options:
--filter filter Provide filter values (e.g. 'until=<timestamp>')
-f, --force Do not prompt for confirmation
2.7 查看容器运行日志
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)
2.8 导出和导入
导出:
docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
-o, --output string Write to a file, instead of STDOUT
导入:
导出的文件可以进行导入变成镜像
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
--platform string Set platform if server is multi-platform capable