一、容器Container、仓库Repository、镜像Image的理解
镜像就像我们伟大的袁隆平的杂交水稻的种子;
容器就像我们伟大的袁隆平的稻田;
仓库就想我们伟大的袁隆平的粮仓;
二、操作容器
NOTE: 操作容器就像在稻田里耕耘
NOTE: 容器的核心为所执行的应用程序,所需要的资源都是应用程序运行所必需的。除此之外,并没有其它的资源。
2.1 启动
$ docker run ubuntu:18.04 /bin/echo 'Hello world'
# 解释:在一个无名的稻田里面种了一颗名为乌邦图的种子,并在容器里面启动了一条命令`bin/echo 'Hello world'`
$ docker run -t -i ubuntu:18.04 /bin/bash
root@af8bae53bdd3:/#
# 解释:-t 分配一个伪终端(terminal),-i 标准输入(input)保持打开

2.2 守护运行
NOTE: 使用-d表示以守护(daemon)态运行,不使用-d,则会把结果STDOUT打印到宿主机上面
- 不使用
-d
参数
$ docker run ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
hello world
hello world
hello world
hello world
- 使用
-d
参数
$ docker run -d ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
77b2dc01fe0f3f1265df143181e7b9af5e05279a884f4776ee75350ea9d8017a
- 使用
docker logs
来查看STDOUT
$ docker container logs [container ID or NAMES]
hello world
hello world
hello world
. . .
2.3 终止
-
docker container stop
来停止容器 -
docker container start
来启动停止的容器 -
docker container restart
来重启容器(重启容器就像重启电脑)
2.4 进入容器
NOTE: 进入容器的命令
docker attach
和docker exec
,推荐使用docker exec
docker exec
2.5 导出和导入

2.6 删除
NOTE: 把下面的命令的
container
更换成image
就是镜像的删除
- 单个删除:使用
docker container rm 容器名
- 批量删除:使用
docker container prune
网友评论