Docker与虚拟化
在没有 Docker 的时代,我们会使用硬件虚拟化(虚拟机)以提供隔离。这里,虚拟机通过在操作系统上建立了一个中间虚拟软件层 Hypervisor ,并利用物理机器的资源虚拟出多个虚拟硬件环境来共享宿主机的资源,其中的应用运行在虚拟机内核上。但是,虚拟机对硬件的利用率存在瓶颈,因为虚拟机很难根据当前业务量动态调整其占用的硬件资源,因此容器化技术得以流行。其中,Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上。
Docker 容器不使用硬件虚拟化,它的守护进程是宿主机上的一个进程,换句话说,应用直接运行在宿主机内核上。因为容器中运行的程序和计算机的操作系统之间没有额外的中间层,没有资源被冗余软件的运行或虚拟硬件的模拟而浪费掉。
Docker 由镜像(Image)、容器(Container)、仓库(Repository) 三部分组成。
一、压缩包安装docker(纯净模式下)
1、上传docker软件包
[root@feng ~]# ll
total 4425304
-rw-------. 1 root root 1390 Nov 6 16:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 4329570304 Jun 7 2019 CentOS-7-x86_64-DVD-1511.iso
-rw-r--r--. 1 root root 201935496 Nov 6 17:53 Docker.tar.gz
2、解压到/opt/下
[root@feng ~]# tar -zxvf docker.tar.gz -c /opt
3、配置yum源
[root@feng ~]# mv /etc/yum.repos.d/C* /media/
[root@feng ~]# mkdir /opt/centos
[root@feng ~]# mount -o loop CentOS-7-x86_64-DVD-1511.iso /opt/centos
[root@feng ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[docker]
name=docker
baseurl=file:///opt/Docker
gpgcheck=0
enabled=1
[root@feng ~]# yum clean all
[root@feng ~]# yum repolist
Loaded plugins: fastestmirror
centos | 3.6 kB 00:00
docker | 2.9 kB 00:00
(1/3): centos/group_gz | 155 kB 00:00
(2/3): centos/primary_db | 2.8 MB 00:00
(3/3): docker/primary_db | 851 kB 00:00
Determining fastest mirrors
repo id repo name status
centos centos 3,723
docker docker 463
repolist: 4,186
4、安装docker
[root@feng ~]# yum install docker-ce -y
5、开启,并设置开机自启
[root@feng ~]# systemctl start docker
[root@feng ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
二、Docker镜像管理
1、镜像获取
(1)、拉取镜像
首先,配置一下仓库文件,配置加速器
[root@feng ~]# docker pull centos (下载centos镜像慢,可以不操作)
[root@feng ~]# vi /etc/docker/daemon.json 加速镜像
{
“registry-mirrors”:[“https://dhq9bx4f.mirror.aliyuncs.com”]
}
[root@feng ~]# systemctl restart docker 重启docker服务
拉取镜像
[root@feng ~]# docker pull centos 拉取镜像
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
[root@feng ~]# docker images 镜像列表
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
(2)、将tar包解压成镜像(需要本地有镜像tar包)
[root@feng images ]# ll
total 1239652
-rwxrwxrwx. 1 root root 1446400 Oct 31 2019 busybox_latest.tar
-rwxrwxrwx. 1 root root 227455488 Oct 31 2019 centos_latest.tar
-rwxrwxrwx. 1 root root 170482176 Oct 31 2019 httpd_latest.tar
-rwxrwxrwx. 1 root root 130066944 Oct 31 2019 nginx_latest.tar
-rwxrwxrwx. 1 root root 128326656 Oct 31 2019 nginx.tar
docker load < 或 -i 都可以tar 导为镜像
[root@feng images]# docker load < nginx_latest.tar
b67d19e65ef6: Loading layer 72.5MB/72.5MB
6eaad811af02: Loading layer 57.54MB/57.54MB
a89b8f05da3a: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@feng images]# docker load -i registry_latest.tar
d9ff549177a9: Loading layer 4.671MB/4.671MB
f641ef7a37ad: Loading layer 1.587MB/1.587MB
d5974ddb5a45: Loading layer 20.08MB/20.08MB
5bbc5831d696: Loading layer 3.584kB/3.584kB
73d61bf022fd: Loading layer 2.048kB/2.048kB
Loaded image: registry:latest
cat tar 包 | docker import - 镜像名字
[root@feng images]# cat busybox_latest.tar | docker import - busybox
sha256:6e0417bef529384eb8ca0bffb122d29a0aaa88882255d0d66973b997308ecd83
最后看一下镜像列表
[root@feng images]# docker images 镜像列表
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
busybox latest 6e0417bef529 7 seconds ago 1.44MB
[root@feng images]# docker rmi 300e315adb2f / centos:latest 删除镜像(ID/aaa:v1)
[root@feng images]# docker rmi busybox 删除镜像
[root@feng images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
(3)本地仓库上传镜像后拉取
首先要创建一个本地仓库
[root@feng ~]# docker pull registry 下载registry 镜像
Using default tag: latest
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete
47112e65547d: Pull complete
46bcb632e506: Pull complete
c1cc712bcecd: Pull complete
3db6272dcbfa: Pull complete
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
[root@feng ~]# docker run -d -p 5000:5000 registry 以registry镜像启动容器
1808bb3e7b96f05eed02980865a54738718e4494e652514283da7a9e6adb3121
[root@feng ~]# curl http://192.168.200.10:5000/v2/_catalog 可以查看到推送上来的镜像
{“repositories”:[]}
[root@feng ~]# docker tag centos:latest 192.168.200.10:5000/centos
[root@feng ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.200.10:5000/centos latest 300e315adb2f Less than a second ago 209MB
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
[root@feng ~]# vi /etc/docker/daemon.json
{
"insecure-registries":["192.168.200.10:5000"]
}
[root@feng ~]# systemctl restart docker
[root@feng ~]#
上传镜像并删除那个镜像
[root@feng ~]# docker push 192.168.200.10:5000/centos
The push refers to repository [192.168.200.10:5000/centos]
2653d992f4ef: Pushed
latest: digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 size: 529
[root@feng ~]# docker rmi 192.168.200.10:5000/centos
Untagged: 192.168.200.10:5000/centos:latest
Untagged: 192.168.200.10:5000/centos@sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875
[root@feng ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
然后从本地仓库中拉取
## 先看一下本地仓库
[root@feng ~]# curl http://192.168.200.10:5000/v2/_catalog
{"repositories":["centos"]}
### 拉取
[root@feng ~]# docker pull 192.168.200.10:5000/centos
Using default tag: latest
latest: Pulling from centos
Digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875
Status: Downloaded newer image for 192.168.200.10:5000/centos:latest
## 查看
[root@feng ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.200.10:5000/centos latest 300e315adb2f Less than a second ago 209MB
centos latest 300e315adb2f Less than a second ago 209MB
registry latest f32a97de94e1 20 months ago 25.8MB
2、管理镜像
(1)删除镜像
[root@feng ~]# docker rmi 192.168.200.10:5000/centos
(2)查看镜像
[root@feng ~]# docker images
(3)给镜像打标签
[root@feng ~]# docker tag centos:latest 192.168.200.10:5000/centos
(4)运行镜像
[root@localhost ~]# docker run -itd centos