1 介绍
容器就是在隔离的环境运行的一个进程,如果进程停止,容器就会销毁。隔离的环境拥有自己的系统文件、IP地址、主机名等。
容器共用宿主机内核,不需要指令级模拟。
1.1 docker工作流程
1.2 docker核心技术
1.2.1 Namespace
实现Container的进程、网络、消息、文件系统和主机名隔离。目前linux内核实现的namespace主要有:
- Mount namespace(CLONE_NEWNS):系统挂载点
- UTS namespace (CLONE_NEWUTS):Hostname等信息
- IPC namespace(CLONE_NEWIPC):进程间通讯
- PID namespace(CLONE_NEWPID):进程号
- Network namespace(CLONE_NEWNET):网络相关资源
- User namespace(CLONE_NEWUSER):用户ID
1.2.2 Cgroup
实现对资源(cpu个数、内存大小等)的配额和度量
1.3 docker特性
- 文件系统隔离:每个容器运行在完全独立的根文件系统里
- 资源隔离:系统资源,像CPU和内存可以分配到不同的容器中,使用cgroup
- 网络隔离:每个进程容器运行在自己的网络空间,虚拟接口和IP地址
- 日志记录:Docker收集和记录每个进程容器的标准流(stdout/stdin/stderr),用于实时检索或批量检索
- 变更管理:容器文件系统的变更可以提交到新的镜像中,并可重复使用以创建更多的container,无需使用模版或手动配置。
- 交互式shell:Docker可以分配一个虚拟终端并关联到任何容器的标准输入上,例如运行一个一次性交互shell
1.4 container运行条件
docker容器内的第一个进程(初始命令)必须一直处于前台运行的状态,否则这个容器就会处于退出状态。
业务在容器中运行:夯住,启动服务
1.5 container运行时覆盖3个文件
容器运行时,会覆盖/etc/hostname
、/etc/hosts
、/etc/resolve.conf
这3个文件。使容器有自己的主机名、hosts解析、dns服务器
2 安装
2.1 安装环境依赖
yum -y install yum-utils device-mapper-persistent-data lvm2
2.2 安装yum源
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.3 yum安装
yum install -y docker-ce docker-ce-cli containerd.io
2.4 启动docker
systemctl start docker
systemctl enable docker
2.5 镜像加速
镜像可以添加多个,中间用,
分隔
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://阿里云页面查看.mirror.aliyuncs.com","http://hub-mirror.c.163.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.6 默认root目录
/var/lib/docker
2.7 开启路由转发功能
yum安装后默认会开启,确认方式
cat /proc/sys/net/ipv4/ip_forward
1
手动开启方法
vim /etc/sysctl.conf
...
net.ipv4.ip_forward = 1
sysctl -p
2.9 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2.10 重启docker
重启docker,调用内核模块netfilter增加规则
systemctl restart docker
2.11(可选)重启docker容器不会停止
修改配置文件/etc/docker/daemon.json
,一般不建议配置
{
"registry-mirrors": ["..."],
"live-restore": true
}
sudo systemctl daemon-reload
sudo systemctl restart docker
3 基本命令
3.1 docker info
docker info
查看基本信息:
- 默认工作目录:/var/lib/docker
- 默认请求的镜像仓库:https://index.docker.io/v1/
- 自己配置的镜像仓库:Registry Mirrors部分
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.7
Storage Driver: overlay2
Backing Filesystem: xfs
Docker Root Dir: /var/lib/docker #默认工作目录
Debug Mode: false
Registry: https://index.docker.io/v1/ #默认请求的镜像仓库
Registry Mirrors: #镜像仓库
https://bigsky000.mirror.aliyuncs.com/
3.2 docker search
docker search [OPTIONS] image
搜索镜像
[root@centos7-wht ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6638 [OK]
ansible/centos7-ansible Ansible on Centos7 134 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 129 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 118 [OK]
centos/systemd systemd enabled base container. 100 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 89
#NAME:镜像名字,/前是项目名,/后是镜像名
#OFFICIAL:是否 docker 官方发布
#STARS:表示点赞、喜欢的意思
#AUTOMATED:自动构建
3.3 docker pull
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
拉取镜像
docker pull centos #从默认仓库中下载镜像
docker pull hub.c.163.com/library/tomcat:latest #直接下载其他站点的镜像
3.4 docker load
dokcer load -i image位置
导入已经下载好的docker镜像
3.5 docker images/docker image
3.5.1 docker images
查看现有镜像
[root@centos7-wht ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 69593048aa3a 7 weeks ago 1.24MB
127.0.0.1:5000/busybox v1 69593048aa3a 7 weeks ago 1.24MB
registry latest 1fd8e1b0bb7e 3 months ago 26.2MB
centos centos7 8652b9f0cb4c 8 months ago 204MB
127.0.0.1:5000/busybox,这类的镜像名表示镜像来自127.0.0.1:5000仓库,而非docker-hub官方库
3.5.2 docker image
镜像相关操作
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
例子:
将registry
镜像打包成registry.tar
文件
docker image save -o registry.tar registry
载入tar格式的registry.tar
镜像
docker image load -i registry.tar
3.6 docker run
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
运行一个container
OPTIONS 常用参数:
通常-i和-t配合使用
-i 以交互模式运行
-t 为容器分配一个伪输入终端
-d 后台运行容器,并返回容器ID
-c 接在bash COMMAND后,为container中bash运行的命令
-c --cpu-shares 后面接数字,限制容器的cpu权重,默认权重是1024
-m --memory 单位b, k, m, or g,限制容器的内存使用量
-p 将host端口映射给container端口,默认是tcp端口,要映射多个端口写多个-p
eg:ip:[hostPort]:containerPort | hostPort:containerPort | hostPort:containerPort:udp
-P 随机将一个host端口映射给container
--name 后面接container名字
-h 后面接container的主机名
--restart always:在容器退出时总是重启容器,容器会伴随docker启动
no:默认策略,容器退出时不重启
on-failure:在容器非正常退出时(退出状态非0),才会重启
on-failure:3:在容器非正常退出时(退出状态非0),最多重启3次
unless-stopped:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
-v HOST-DIR:CONTAINER-DIR[:权限] 将容器内部的目录mount到host的目录下,权限是rw、ro等以逗号分隔,要mount多个目录写多个-v参数
volume:CONTAINER-DIR,容器在/var/lib/docker/volumes目录下创建数据卷,删除容器后数据卷不消失
--mount 默认:source=volume名字,destination=CONTAINER-DIR。默认情况下用来挂载volume,但也可以用来创建bind mount和tmpfs。
bind mount:type=bind,source=HOST-DIR,destination=CONTAINER-DIR,source必须是绝对路径,且路径必须已经存在。
--volumes-from 挂载已有的数据卷
-e 设置container内部的环境变量,eg: -e "SSH_PWD=123",SSH_PWD为容器内部定义的变量
--privileged Docker将启用对宿主机上所有设备的访问,允许容器对宿主机的访问几乎与在宿主机上容器外部运行的进程相同,可以直接挂载宿主机的磁盘
--rm 容器命令运行结束后,自动删除容器,自动释放资源
--device onhost:incontainer[:mode],将host的设备映射给container,/dev/sdc:/dev/xvdc:rwm
--device-write-bps 单位kb、mb、gb,限制容器写IO速度
--device-read-bps 单位kb、mb、gb,限制容器读IO速度
--ip 设置容器的IPv4地址
--network 设置容器连接的网络,默认是docker0
例子:
以交互模式运行centos,并运行container中的bash
docker run -it centos:latest bash
后台运行container,每隔10s不断输出hello world
docker run -d centos:latest bash -c "while true;do echo hello world;sleep 10;done"
3.7 docker ps
docker ps
列出正在运行的容器状态
docker ps -a
列出所有容器的状态,包括沉睡/退出状态的容器docker ps -q
只显示容器的CONTAINER ID
docker ps -l
只显示最后创建的容器信息docker ps --no-trunc
不缩略,显示容器的全部信息
3.8 docker logs
docker logs 容器ID/名字
从容器中取日志,检查docker的标准输出。
docker logs 4336a96cc275
hello world
hello world
hello world
3.9 docker kill
docker kill 容器ID/名字
杀死一个container实例
3.10 docker rename
docker rename 旧容器名 新容器名
更改容器名
3.11 docker update
docker update 参数 容器ID/名字
更新容器参数,只能更新cpu、内存、io、pid策略、重启策略
docker update --restart always centos
3.12 docker exec
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
进入创建一个伪终端,类似于ssh,不会因为输入exit而终止容器
OPTIONS 常用参数:
通常-i和-t配合使用
-i 以交互模式执行
-t 为容器分配一个伪输入终端
-d 后台执行
例子:
以交互模式在container中运行bash
docker exec -it centos bash
3.13 docker attach
会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止进程。会进入第1个伪终端。
3.14 临时回到host主机
ctrl+p,ctrl+q
临时回到host主机
docker attach ...
再次回到docker伪终端
3.15 docker rm
docker rm [OPTIONS] CONTAINER [CONTAINER...]
删除已经建立的容器
删除建立的所有容器
docker rm -f `docker ps -a -q`
3.16 docker inspect
docker inspect [OPTIONS] 容器NAME|容器ID [NAME|ID...]
查看容器的祥细信息
docker inspect ssh_nginx
[
{
"Id": "973c9487f8dbf2c6dfa5c443dd3b2697eced2ac78d896dd9cd50b00cf87f6816",
"Created": "2021-09-01T08:32:08.645237423Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 23325,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-09-01T08:32:08.951092626Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
...
3.17 docker build
以dockerfile创建镜像,PATH为dockerfile所在目录
docker build -t name:tag PATH