Docker
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 是一个工具,可以帮助解决如安装、拆卸、升级、分发、信任和管理软件等常用问题。Docker包含一个命令行程序、一个后台守护进程和一组远程服务。
1、Docker是什么
Section titled “1、Docker是什么”Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 是一个工具,可以帮助解决如安装、拆卸、升级、分发、信任和管理软件等常用问题。Docker包含一个命令行程序、一个后台守护进程和一组远程服务。
不同于虚拟机,Docker容器不使用硬件虚拟化,运行在Docker容器中的程序接口直接和Linux内核打交道,容器中的程序和操作系统之间没有额外的中间层。
命令行工具在用户名称空间的内存中运行, 同操作系统上运行的其他程序一样。
Docker构建的容器隔离包括以下8个方面:
- PID名称空间---进程表示符和能力
- UST名称空间---主机名和域名
- MNT名称空间---文件系统访问和结构
- NET名称空间---网路访问和结构
- USR名称空间---用户名和标识
- chroot()---控制文件系统根目录的位置
- cgroups---资源保护
2、构建和环境无关的系统
Section titled “2、构建和环境无关的系统”Docker有三个特定功能,来帮助建立与环境无关的系统
- 只读文件系统
- 环境变量注入
- 存储卷
2.1、只读文件系统
Section titled “2.1、只读文件系统”只读文件系统,容器不能更改它所包含的任何文件,同时容器中的攻击者无法破坏文件。
创建容器时需使用—read-only标志,如:
docker run -d --read-only wordpress:42.2、环境变量注入
Section titled “2.2、环境变量注入”环境变量是通过执行上下文提供给程序的键值对。可以让用户在更改程序配置时,无须修改任何文件或者更改启动程序的命令。
—env标志或者-e缩写,可用于注入任何环境变量, 如:
$ docker run --env MY_ENV="hello world" busybox:latest env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=90b9adbe051cMY_ENV=hello worldHOME=/root2.3、修改容器启动参数
Section titled “2.3、修改容器启动参数”创建容器时,如果忘记添加参数—restart=always,那么容器并不会自动重启。容器已经创建,此时修改容器配置可以通过命令:docker container update,
± docker container update --help
Usage: docker container update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options: --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --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 the CPU real-time period in microseconds --cpu-rt-runtime int Limit the 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) --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 --pids-limit int Tune container pids limit (set -1 for unlimited) --restart string Restart policy to apply when a container exits如,修改restart配置, 可以使用:
docker container update --restart=always 容器ID