1、什么是容器
容器就类似于我们运行起来的一个操作系统,而且这个操作系统启动了某些服务。这里的容器指的是运行起来的一个Docker镜像
2、容器相关命令
]# docker help | grep container
A self-sufficient runtime for containers
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
container Manage containers
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
export Export a container's filesystem as a tar archive
kill Kill one or more running containers
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
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
3、容器命令实战
3.1、查看容器【ps -a】
3.1.1、查看所有的容器
3.1.2、过滤查看部分容器
docker ps --filter status=exited -q
# 属性解析
status=exited # 过滤条件
-q # 显示容器ID
3.2、启动容器【run】
3.2.1、命令语法解析
docker run -itd <image_name>
常见参数:
-e, --env list # 设置环境变量
--env-file list # 从文件获取环境变量
-i, --interactive # 打开标准输入
-t, --tty # 分配一个tty
-d, --detach # 后台运行,并且打印容器ID
--name string # 容器的名字
-h, --hostname string # 容器主机名
--rm # 容器停止,则删除运行容器的运行记录
--restart string # 容器退出时要应用的重新启动策略(默认为“否”)
3.2.2、使用容器ID启动【start】
docker start [container_id]
3.3、关闭容器【stop】
docker stop [container_id]
3.4、删除容器【rm】
docker rm [-f] [container_id]
批量删除的技巧:
docker rm -f $(docker ps -a -q)
3.5、进入容器【exec】
docker exec -it [container_id] /bin/bash
属性解析
-i: 则让容器的标准输入保持打开。
-t: 让docker分配一个伪终端,并绑定到容器的标准输入上
/bin/bash: 执行一个命令
3.6、退出容器【exit】
3.7、提交容器【commit】
docker commit -m '改动信息' -a "作者信息" [container_id] [new_image:tag]
3.8、查看容器的日志【log】
docker logs [container_id]
3.9、查看容器的属性【inspect】
3.9.1、语法
docker inspect [container_id]
3.9.2、格式化输出属性
专用格式:docker inspect -f '{{json .NetworkSettings.Ports}}' [container_id]
定制格式:docker inspect -f 'Hello from container {{.Name}}' [container_id]
3.9.3、高阶格式信息输出
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' [container_id]
3.9.4、属性解析
{{}} 表示模板指令,里面存放属性的名称
“.” 表示“当前上下文,表示容器元数据的整个数据结构,两个属性间的. 代表上下级
range 用于遍历结构内返回值的所有数据