本文仅做简略教程和快速查阅使用,具体原理和详细步骤请参考Docker官方文档
在Ubuntu上安装Docker
卸载旧版Docker
如果之前安装过旧版的Docker需要先进行卸载
sudo apt autoremove --purge docker docker-engine docker.io containerd runc
设置Docker仓库
- 更新APT源,安装HTTPS相关依赖
sudo apt update sudo apt install \ ca-certificates \ curl \ gnupg \ lsb-release
- 添加Docker GPG key
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- 添加Docker仓库
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
下载Docker (包含Docker Compose)
sudo apt update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
测试
sudo docker run hello-world
权限设置
安装完成后使用Docker通常需要以sudo运行,不太方便,可以通过将当前用户加入Docker组中实现免sudo使用Docker
- 创建Docker组(如果没有)
sudo groupadd docker
- 将当前用户加入Docker组
sudo gpasswd -a ${USER} docker
- 重启Docker服务
sudo service docker restart
- 切换会话
newgrp - docker
- 测试
以当前用户运行 docker rundocker run hello-world
常用指令
下载镜像
Docker镜像下载可以通过 docker pull
指令
基于Docker的原理,下载一个镜像时其实是下载了多个layer,在其他镜像使用了相同layer时可以达到复用的效果,具体原理请参考官方文档,这里不再阐述
指令
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
OPTIONS参数
Name, shorthand | Default | Description |
---|---|---|
--all-tags , -a |
Download all tagged images in the repository | |
--disable-content-trust |
true |
Skip image verification |
--platform |
Set platform if server is multi-platform capable | |
--quiet , -q |
Suppress verbose output |
示例
- 从Docker Hub上拉取镜像
docker pull debian
- 使用TAG
通常如果不指明TAG,会默认使用latestdocker pull debian:jessie # TAG jessie docker pull debian:latest # TAG latest
- 从指定仓库拉取镜像
如果不致命具体仓库,会默认从Docker Hub上拉取docker pull myregistry.local:5000/testing/test-image
查看镜像
指令
docker images [OPTIONS] [REPOSITORY[:TAG]]
OPTIONS参数
Name, shorthand | Default | Description |
---|---|---|
--all-tags , -a |
Show all images (default hides intermediate images) | |
--digests |
Show digests | |
--filter , -f |
Filter output based on conditions provided | |
--format |
Pretty-print images using a Go template | |
--no-trunc |
Don't truncate output | |
--quiet , -q |
Only show image IDs |
示例
- 查看镜像
docker images
- 通过name和tag查看镜像
docker images java docker images java:8
- 通过
--filter
进行高级筛选
目前支持的filter
参数:- dangling (boolean - true or false)
- label (label=
or label= = ) - before (
[: ], or image@digest) - filter images created before given id or references - since (
[: ], or image@digest) - filter images created since given id or references - reference (pattern of an image reference) - filter images whose reference matches the specified pattern
docker images --filter "dangling=true" # 未使用的镜像 docker images --filter "label=com.example.version" # label为com.example.version的镜像 docker images --filter "before=image1" # 在image1镜像之前创建(创建时间早于image1)的镜像 docker images --filter "since=image3" # 在image1镜像之后创建(创建时间晚于image3)的镜像 docker images --filter=reference='busy*:*libc' # reference满足'busy*:*libc'模式的镜像
删除镜像
指令
docker rmi [OPTIONS] IMAGE [IMAGE...]
OPTIONS参数
Name, shorthand | Default | Description |
---|---|---|
--force , -f |
Force removal of the image | |
--no-prune |
Do not delete untagged parents |
示例
- 可以使用short/long ID、tag或digest来删除镜像
- 当镜像有引用它的tags时,必须在删除镜像之前删除所有tag
- 当通过tag删除镜像时,digest references会自动删除
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test1 latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
test latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
test2 latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
docker rmi fd484f19954f
Error: Conflict, cannot delete image fd484f19954f because it is tagged in multiple repositories, use -f to force
2013/12/11 05:47:16 Error: failed to remove one or more images
docker rmi test1:latest
Untagged: test1:latest
docker rmi test2:latest
Untagged: test2:latest
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
docker rmi test:latest
Untagged: test:latest
Deleted: fd484f19954f4920da7ff372b5067f5b7ddb2fd3830cecd17b96ea9e286ba5b8
也可以使用 -f
参数来untags和remove所有指定镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test1 latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
test latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
test2 latest fd484f19954f 23 seconds ago 7 B (virtual 4.964 MB)
docker rmi -f fd484f19954f
Untagged: test1:latest
Untagged: test:latest
Untagged: test2:latest
Deleted: fd484f19954f4920da7ff372b5067f5b7ddb2fd3830cecd17b96ea9e286ba5b8