1. Docker容器的特殊规则:
- 一个容器可以运行多个进程,但是必须有一个超管进程(init),对容器内的进程进行管理;
- 如果容器内的进程出问题,多进程情况下查看日志将比较麻烦;
- 因为容器中运行的应用进程的日志不在写入日志文件中;
- 而是发送给容器控制台,Docker客户端在容器外通过命令访问控制台,查看容器内进程的日志;
- 所以一个容器中只运行一个进程及其子进程,可以省略启动超管进程;
- 容器启动直接自动开启应用进程,如果应用进程出问题,可直接查看控制台,便于分析问题;
- 这样容器管理也就被简化了。
2. 创建容器
[root@centos_7_1]:[~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@centos_7_1]:[~]# docker container run --name a1 -it alpine:3.8
/ #
/ # ls /
bin dev etc home lib media mnt proc root run sbin srv sys tmp usr var
/ #
#-------------------保持当前终端不退出,另开一个终端,查看当前主机有哪些容器-------------------
[root@centos_7_1]:[~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" About a minute ago Up About a minute a1
上述命令详细说明:
- docker container ls
- 查看当前主机有哪些容器正在运行,运行中的主机STATUS属性显示UP。
- docker container run
- 创建容器
- --name a1
- 指定容器名称为a1
- -i
- 选项,表示创建容器后打开交互式接口。
- -t
- 选项,表示为容器创建一个伪终端。
- 执行以上各种组合选项后,进入容器伪终端界面。
- 这时可以像在正常Linux虚拟中一样,执行各种Linux命令。
3. 退出容器
/ # ls /
bin dev etc home lib media mnt proc root run sbin srv sys tmp usr var
/ # exit
[root@centos_7_1]:[~]#
#-----------------------------另开一个终端,查看当前有哪些终端-----------------------------
[root@centos_7_1]:[~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@centos_7_1]:[~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" 3 minutes ago Exited (0) 7 seconds ago
- exit
- 在容器中键入exit表示退出容器
- docker container ls -a
- -a选项与Linux大多数命令的选项表达的意思相同;
- 表示查看所有容器,包括已开启或者已停止的。
4. 打开已停止的容器
[root@centos_7_1]:[~]# docker container start a1
a1
[root@centos_7_1]:[~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" 26 minutes ago Up 2 seconds a1
[root@centos_7_1]:[~]# docker container attach a1
/ #
/ #
#-----------------------------分割线-----------------------------
# 使用快捷键剥离终端
[root@centos_7_1]:[~]#
[root@centos_7_1]:[~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" 15 hours ago Up 17 seconds a1
[root@centos_7_1]:[~]# docker container stop a1
a1
[root@centos_7_1]:[~]#
[root@centos_7_1]:[~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" 15 hours ago Exited (137) 9 seconds ago a1
** 上述命令详细说明:**
- docker container start a1
- 因为容器已常见,所以直接用start打开已停止的容器。
- 已关闭再次打开的情况下,start命令并不能进入容器的终端窗口中。
- docker container attach a1
- 关联到a1容器的终端窗口中。
- 想在容器终端窗口中,剥离终端:
- 使用快捷键:先按键盘:ctrl+p,松开键盘,再次按:ctrl+q。
- 其他Docker快捷键可以查询:https://docs.docker.com/engine/reference/commandline/attach/
- docker container stop a1
- 在宿主机上用stop命令终止运行中的容器
5. Docker容器命令的所有子命令
[root@centos_7_1]:[~]# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams 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 to files or directories 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
Run 'docker container COMMAND --help' for more information on a command.
6. Docker新版本与老版本命令对比
[root@centos_7_1]:[~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" 16 hours ago Up 3 seconds a1
[root@centos_7_1]:[~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
164b90dacf73 alpine:3.8 "/bin/sh" 16 hours ago Up 7 seconds a1
- Docker容器命令:
- 老版本:
- docker ps
- docker ps -a
- 新版本:
- docker container ls
- docker container ls -a
Docker分类---文章目录:
- 老版本:
| :----- | :-----: |
| 第一章 初识Docker | 点击此处 |
| 第二章 安装Docker以及简单配置 | 点击此处 |
| 第三章 Docker容器的生命周期 | 点击此处 |
| 第四章 Docker命令汇总 | 点击此处 |
| 第五章 Docker基础命令详解 | 点击此处 |
| 第六章 Docker---镜像的命令详解 | 点击此处 |
| 第七章 Docker---容器的命令详解 | 点击此处 |
| 第八章 Docker与Alpine不解之缘 | 点击此处 |
|未完待续| |