04 容器管理
[TOC]
00X01 常用命令
管理容器常用命令
选项 | 描述 |
---|---|
inspect | 查看一个或多个容器详细信息 |
exec | 在运行容器中执行命令 |
commit | 创建一个新镜像来自一个容器 |
cp | 拷贝文件/文件夹到一个容器 |
logs | 获取一个容器日志 |
port | 列出或指定容器端口映射 |
top | 显示一个容器运行的进程 |
stats | 显示容器资源使用统计 |
stop/start/restart | 停止/启动/重启一个或多个容器 |
rm | 删除一个或多个容器 |
ps | 查看运行的容器 |
00X02 创建容器
docker run
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
选项 描述
-i, –interactive 交互式
-t, –tty 分配一个伪终端
-d, –detach 运行容器到后台
-e, –env 设置环境变量
-p, –publish list 发布容器端口到主机
-P, –publish-all 发布容器所有EXPOSE的端口到宿主机随机端口
–name string 指定容器名称
-h, –hostname 设置容器主机名
–ip string 指定容器IP,只能用于自定义网络
–network 连接容器到一个网络
–mount mount 将文件系统附加到容器
-v, –volume list 绑定挂载一个卷
–restart string 容器退出时重启策略,默认no,可选值:[always|on-failure]
00X021 以后台运行
[[email protected] ~]# docker run -itd busybox
ff289fdc76b7bee9f6724903c75777d54294e6a6c5c9fd350592c357185dbc55
00X03 查看容器
查看最新启动的容器
[[email protected] ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff289fdc76b7 busybox "sh" 42 seconds ago Up 41 seconds peaceful_ptolemy
查看所有运行的容器
docker ps
查看所有容器,包括已停止的容器
docker ps -a
00X04 查看容器运行进程
[[email protected] ~]# docker top ff289fdc76b7
UID PID PPID C STIME TTY TIME CMD
root 30147 30129 0 16:51 ? 00:00:00 sh
00X05 进入容器
可用 attach , exec
attach 以容器启动的终端进入,退出时候关闭这个终端运行的进程也跟着关闭即意味容器停止
exec 以一个新的终端进入,退出时候关闭这个终端.容器运行状态不受影响
docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
进入容器
[[email protected] ~]# docker exec -it ff289fdc76b7 sh
[[email protected] /]#whoami
root
exec 执行命令
[[email protected] ~]# docker exec -it ff289fdc76b7 sh -c whoami
root
00X06 查看日志
查看所有日志
docker logs ff289fdc76b7
实时查看 -f
docker logs -f ff289fdc76b7
常用组合
docker logs -f --tail=200 ff289fdc76b7
00X07 启动/停止/重启/删除 容器
docker start/stop/restart ff289fdc76b7
删除
删除前容器需停止
docker stop ff && docker rm ff
00X08 容器与宿主机之间文件拷贝
docker cp
docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
拷贝宿主机文件或目录到容器/tmp中
[[email protected] ~]# echo 'Hello World' > 1
[[email protected] ~]# docker cp 1 ff:/tmp
[[email protected] ~]# docker exec -it ff sh -c "cat /tmp/1"
Hello World
00X09 容器资源限制
[[email protected] ~]# docker run --help|grep memory
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
[[email protected] ~]# docker run --help|grep cpu
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
选项 | 描述 |
---|---|
-m,–memory | 容器可以使用的最大内存量 |
–memory-swap | 允许交换到磁盘的内存量 |
–memory-swappiness=<0-100> | 容器使用SWAP分区交换的百分比(0-100,默认为-1) |
–oom-kill-disable | 禁用OOM Killer |
–cpus | 可以使用的CPU数量 |
–cpuset-cpus | 限制容器使用特定的CPU核心,如(0-3, 0,1) |
–cpu-shares | CPU共享(相对权重) |
00X091 内存限额
允许容器最多使用500M内存和100M的Swap,并禁用OOM Killer:
[[email protected] ~]# docker run -d --name nginx --memory="500m" --memory-swap=“600m" --oom-kill-disable nginx
[[email protected] ~]# docker stats --no-stream nginx #查看设置的内存
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
634bcb0a63ae nginx 0.00% 2.379MiB / 500MiB 0.13% 37.5MB / 34.8MB 0B / 0B 2
00X092 CPU限额
允许容器最多使用一个半的CPU:
docker run -d --name nginx --cpus="1.5" nginx
允许容器最多使用50%的CPU:
docker run -d --name nginx --cpus=".5" nginx