Docker 的命令设计和 Linux bash shell 十分相似,有 Linux 使用经验的话各种操作都符合直觉,例如想要操作镜像,则可以用Docker image <command>
来实现各类操作,其中command
可以为 rm、ls、build 等。同理,操作容器可以使用Docker container <command>
。
tips
- 在使用 zsh 时,如果 docker 命令没有自动补全的话需要在
.zshrc
的plugin
里添加 docker 插件支持。 - Docker 所有命令都可以在其后添加–help 查看基本命令介绍。
镜像操作
从 Docker Hub 上获取镜像
从 Docker Hub 上获取镜像的命令类似 git,我们使用git pull
来获取镜像:
docker pull [选项] [Docker Registry 地址 [: 端口号]/] 仓库名 [: 标签]
-
Docker 镜像仓库地址:地址的格式一般是
<域名/IP>[: 端口号]
。默认地址是 Docker Hub(docker.io)。 -
仓库名格式为
<用户名>/<软件名>
。对于 Docker Hub,如果不给出用户名,则默认为library
,也就是官方镜像。
示例 1:拉取 ubuntu 20.04 镜像
❯ docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
83ee3a23efb7: Pulling fs layer
db98fc6f11f0: Downloading
f611acd52c6c: Downloading
20.04: Pulling from library/ubuntu
83ee3a23efb7: Pull complete
db98fc6f11f0: Pull complete
f611acd52c6c: Pull complete
Digest: sha256:703218c0465075f4425e58fac086e09e1de5c340b12976ab9eb8ad26615c3715
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
示例 2:拉取 nginx 镜像
❯ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
45b42c59be33: Pull complete
d0d9e9ea897e: Pull complete
66e650438339: Pull complete
76a3dfe4406b: Pull complete
410ff9d97480: Pull complete
Digest: sha256:8e10956422503824ebb599f37c26a90fe70541942687f70bbdb744530fc9eba4
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
tips
- 从上述示例的下载过程中可以看出 Docker 分层存储的概念,镜像是由多层存储所构成。下载也是一层层的去下载,并非单一文件。下载过程中给出了每一层的 ID 的前 12 位。并且下载结束后,给出该镜像完整的
sha256
的摘要,以确保下载一致性。 - 不清楚 tag 如何选时可以去 Docker Hub 官方仓库里查看,不同的 tag 区分不同版本的镜像,例如 Ubuntu:20.04 中
20.04
就是镜像对应的 tag。 - 如果拉取镜像时不添加 tag,则默认是 latest(自建镜像不添加 tag 默认也是 latest)。
列出所有镜像
❯ docker image ls #等价于 docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 298ec0e28760 7 days ago 133MB
ubuntu 20.04 f63181f19b2f 3 weeks ago 72.9MB
删除镜像
docker image rm [image id]
构建镜像
docker build [path to Dockerfile]
标记(tag)本地镜像并将其归入某一仓库
❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 298ec0e28760 7 days ago 133MB
ubuntu 20.04 f63181f19b2f 3 weeks ago 72.9MB
docker/getting-started latest 3c156928aeec 10 months ago 24.8MB
❯ docker tag ubuntu:20.04 haodong/ubuntu:v1
❯ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 298ec0e28760 7 days ago 133MB
haodong/ubuntu v1 f63181f19b2f 3 weeks ago 72.9MB
ubuntu 20.04 f63181f19b2f 3 weeks ago 72.9MB
docker/getting-started latest 3c156928aeec 10 months ago 24.8MB
上述操作对本地的 ubuntu:20.04 镜像标记了一个新 tag v1
并归入了haodong/ubuntu
仓库下。删除操作如下所示:
❯ docker image rm haodong/ubuntu:v1
Untagged: haodong/ubuntu:v1
对镜像的其余操作不再赘述,按照 linux 的使用经验和直觉自行探索,必要时候使用
--help
来查看即可。
tips
- build – Build an image from a Dockerfile
- history – Show the history of an image
- import – Import the contents from a tarball to create a filesystem image
- inspect – Display detailed information on one or more images
- load – Load an image from a tar archive or STDIN
- ls – List images
- prune – Remove unused images
- pull – Pull an image or a repository from a registry
- push – Push an image or a repository to a registry
- rm – Remove one or more images
- save – Save one or more images to a tar archive (streamed to STDOUT by default)
- tag – Tag an image into a repository
容器操作
对容器的操作与对上述镜像操作十分相似,docker 的命令十分好记且符合直觉。
根据镜像新建一个容器
docker 中使用docker run <command> [image][tag] <command>
的方式来根据镜像新建容器。新建的容器可以运行在前台,随着终端的退出而结束,也可以运行在后台。
示例 1:新建一个前台容器并与之交互
❯ docker run -it --rm ubuntu:20.04 bash
root@743c99fd3425:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
上述操作以 ubuntu20.04 镜像为基础新建了一个容器并打开了stdin
并分配了一个pseudo tty
同时运行了bash
命令启用了 bash 终端。
-
-it
:这是两个参数,一个是-i
:交互式操作,一个是-t
终端。我们这里打算进入bash
执行一些命令并查看返回结果,因此我们需要交互式终端。 -
--rm
:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动docker rm
。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用--rm
可以避免浪费空间。 -
bash
:放在镜像名后的是 命令,这里我们希望有个交互式 Shell,因此用的是bash
。
之后可以通过exit
命令来退出这个容器。
示例 2:新建一个运行在后台的容器
❯ docker run -dp 80:80 nginx
12c280a74dc43c166a502d36e93236e623fd4643a1955006d8cf2da8060af590
❯ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12c280a74dc4 nginx "/docker-entrypoint.…" 36 seconds ago Up 35 seconds 0.0.0.0:80->80/tcp musing_johnson
可以看到我们新建了一个运行 ngnix 的容器,并且将宿主机的 80 端口映射到和容器的 80 号端口。
其中:
-p
参数用来指定端口映射。-d
(detached mode)参数用来指定后台运行的容器。
这个时候在浏览器打开宿主机的 ip 对应的 80 端口即可看到 nginx 欢迎界面,如下所示:
之后可以通过stop
、start
、pause
、logs
等操作来操作容器。
tips
- attach – Attach to a running container
- commit – Create a new image from a container’s changes
- cp – Copy files/folders between a container and the local filesystem
- create – Create a new container
- diff – Inspect changes on a container’s filesystem
- exec – Run a command in a running container
- export – Export a container’s filesystem as a tar archive
- inspect – Display detailed information on one or more containers
- kill – Kill one or more running containers
- logs – Fetch the logs of a container
- ls – List containers
- pause – Pause all processes within one or more containers
- port – List port mappings or a specific mapping for the container
- prune – Remove all stopped containers
- rename – Rename a container
- restart – Restart one or more containers
- rm – Remove one or more containers
- run – Run a command in a new container
- start – Start one or more stopped containers
- stats – Display a live stream of container(s) resource usage statistics
- stop – Stop one or more running containers
- top – Display the running processes of a container
- unpause – Unpause all processes within one or more containers
- update – Update configuration of one or more containers
- wait – Block until one or more containers stop, then print their exit codes