Docker
NameSpace
实现了资源隔离
UTS:每一个 NameSpace 都拥有独立的主机或域名,可以把每个 NameSpace 认为一个独立主机。
IPC:每个容器依旧使用 Linux 内核中进程交互的方法,实现进程间通信
Mount:每个容器的文件系统是独立的
Net:每个容器的网络是隔离
User: 每个容器的用户和组 ID 是隔离,每个容器都拥有 root 用户
PID:每个容器都拥有独立的进程树,由容器是物理机中的一个进程,所以容器中的进程是物理机一个进程的线程
容器使用的命名空间有哪些?
1应用程序运行环境隔离的空间,就是一个容器,每一个容器都将拥有UTS,IPC,Mount,Net,User,PID。
CGroup
实现了资源限制、限制、审计等…
LXC
Linux Container,可以提供轻量级的虚拟化。Docker 底层就是使用 LXC 来实现的。以 LXC 为基础实现了更强的功能。
Docker 安装
1yum install -y yum-utils device-mapper-persistent-data lvm2
2
3yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4
5yum makecache fast
6
7yum -y install docker-ce
8
9systemctl enable docker
10
11systemctl restart docker
12
13sudo yum list docker-ce.x86_64 --showduplicates | sort -r
14
15docker -v
16
17docker version
镜像加速器
1mkdir -p /etc/docker
2tee /etc/docker/daemon.json <<-'EOF'
3{
4 "registry-mirrors": ["https://demo.888bit3po.mirror.aliyuncs.com"]
5}
6EOF
7systemctl daemon-reload
8systemctl restart docker
镜像
打包好的环境与应用
1# 查看本地所有镜像
2[root@localhost ~]# docker images
3
4# 查找镜像
5[root@localhost ~]# docker search centos
6
7# 拉取镜像
8[root@localhost ~]# docker push centos
9
10# 删除镜像
11[root@localhost ~]# docker rmi de974760ddb2
12
13# 导出镜像
14[root@localhost ~]# docker save centos:latest -o /root/centos_latest
15[root@localhost ~]# ll -h /root/centos_latest
16-rw-------. 1 root root 207M Apr 26 10:29 /root/centos_latest
17
18# 导入镜像
19[root@localhost ~]# docker load < /root/centos_latest
20
21
22
23
24
25
26
27
28
镜像仓库
1、官方自建镜像仓库(网络太慢)
2、阿里云自建镜像仓库
1# 登录阿里云Docker Registry
2docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
3
4# 从Registry中拉取镜像
5docker pull registry.cn-shanghai.aliyuncs.com/leon_ns/test:[镜像版本号]
6docker pull registry.cn-shanghai.aliyuncs.com/leon_ns/test:v1
7
8# 将镜像推送到Registry
9docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/leon_ns/test:[镜像版本号]
10docker tag 300e315adb2f registry.cn-shanghai.aliyuncs.com/leon_ns/test:v1
11
12docker push registry.cn-shanghai.aliyuncs.com/leon_ns/test:[镜像版本号]
13docker push registry.cn-shanghai.aliyuncs.com/leon_ns/test:v1
14
15# 退出阿里云
16docker logout registry.cn-shanghai.aliyuncs.com
17
3、通过 Harbor 搭建自己的企业级镜像仓库
Harbor
Harbor 是由 VMware 公司开源出来的企业级 Registry 项目,可以帮助用户快速搭建一个企业级的 Docker Registry 服务。
Harbor 由 python 语言开发,需要使用 docker-compose 工具进行启动。
需要的工具:
- 使用 epel 源安装 pip
- 使用 pip 安装 docker-compose
安装 docker 和 python2-pip、安装 docker-compose
1yum install -y yum-utils device-mapper-persistent-data lvm2
2
3yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4
5yum install epel-release -y
6
7yum clean all
8
9yum makecache fast
10
11yum install -y docker-ce python2-pip
12
13systemctl start docker
14
15systemctl enable docker
16
17pip install --upgrade pip
18
19
20# 如果以上不成功安装docker-compose
21curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
22
23chmod +x /usr/local/bin/docker-compose
24
25
26pip install docker-compose
27
安装 Harbor
1[root@localhost software]# tar xf harbor-offline-installer-v1.8.2.tgz -C /usr/local/
2[root@localhost software]# cd /usr/local/harbor/
3[root@localhost harbor]# ls
4harbor.v1.8.2.tar.gz harbor.yml install.sh LICENSE prepare
5[root@localhost harbor]# vim harbor.yml
65 hostname: 192.168.31.211
7harbor_admin_password: 123
8[root@localhost harbor]# ./install.sh
9
10# 访问 http://192.168.31.211/
11admin
12123
客户端测试
1# 修改为非安装方式连接harbor
2[root@localhost ~]# cat /etc/docker/daemon.json
3{
4 "registry-mirrors": ["https://sds.888bit3po.mirror.aliyuncs.com"],
5 "insecure-registries": ["192.168.31.211"]
6}
7[root@localhost ~]# systemctl daemon-reload
8[root@localhost ~]# systemctl restart docker
9
10# 登录
11docker login 192.168.31.211
12admin
13123
14
15# 上传
16docker tag SOURCE_IMAGE[:TAG] 192.168.31.211/library/IMAGE[:TAG]
17docker tag hello-world 192.168.31.211/library/hello-world:v1
18
19docker push 192.168.31.211/library/IMAGE[:TAG]
20docker push 192.168.31.211/library/hello-world:v1
21
22# 下载
23docker pull 192.168.31.211/library/hello-world:v1
24
容器
运行镜像的实例
1# 用hello-world 镜像来启动一个容器
2docker run hello-world
3
4# -d表示后台运行(不输出结果到屏幕)
5docker run -d centos /bin/bash -c "while true; do echo haha;sleep 3;done"
6
7# 查看后台日志结果
8docker logs 50d2dd324a1e
9
10# 查看容器的状态信息
11docker inspect de2e6a1763eb
12
13# 交互模式
14docker run -it --name=c1 centos:latest /bin/bash
15
16# 进入正在运行的容器
17docker attach 20cf1bda4f4f
18
19# 把修改过的容器保存成一个新的镜像(进入容器touch 2222文件)
20docker commit 20cf1bda4f4f test:v1
21docker run -it --name=c1 test:v1 /bin/bash # 启动修改好的镜像,发现新容器中有2222文件
22[root@cf6ac75a432d /]# ls
232222 bin
24
25
仓库
存放多个镜像的一个仓库