镜像(images)
Docker镜像就好比是一个模板,可以通过这个模板来创建容器服务,tomcat镜像===>run tomcat01容器(提供服务) ,通过这个镜像可以创建多个容器 ,【最终服务的运行或者是项目的运行就是在这个容器中的】这里可以暂时理解为JAVA 中 类与对象的关系
容器(container)
Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建。
启动,停止,删除基本命令
仓库(repository)
仓库就是存放镜像的地方!
仓库分为公有仓库和私有仓库
Dcoker Hub(默认是国外的),国内也有很多公有仓库,例如 阿里云…(可以配置镜像加速)
环境准备
环境查看
# 系统的内核是3.10+
[root@iZhp3do4qhzu84445osa3sZ ~]# uname -r
3.10.0-1062.18.1.el7.x86_64
# 系统版本
[root@iZhp3do4qhzu84445osa3sZ ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
Docker 帮助文档
# 1. 卸载旧的版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2. 需要的安装包
yum install -y yum-utils
# 3. 设置镜像的仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo # 默认是国外的 不推荐
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云镜像地址 推荐
# 4. 更新yum !!! 不然后面可能会报错
yum makecache fase
# 5. 安装Docker相关的内容(这里是最新版本,如果想安装其他版本版本参考官方文档) ce-社区版 ee-企业版
yum install docker-ce docker-ce-cli containerd.io
# 6. 启动docker
systemctl start docker
# 7. 使用docker version 查看docker是否启动成功
[root@iZhp3do4qhzu84445osa3sZ ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
Go version: go1.10.3
Git commit: cccb291/1.13.1
Built: Tue Mar 3 17:21:24 2020
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
Go version: go1.10.3
Git commit: cccb291/1.13.1
Built: Tue Mar 3 17:21:24 2020
OS/Arch: linux/amd64
Experimental: false
# 8. 测试 hello-world
[root@iZhp3do4qhzu84445osa3sZ ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 没有寻找到hello-world镜像
Trying to pull repository docker.io/library/hello-world ... #尝试从远程仓库中寻找hello-world镜像
latest: Pulling from docker.io/library/hello-world #在远程仓库寻找到了该镜像,并且正在pull
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for docker.io/hello-world:latest
Hello from Docker! #运行成功显示 相当于Docker安装成功了
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
# 9. 通过docker images 查看存在的镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/rabbitmq latest 7e50c60f7e3e 3 days ago 156 MB
docker.io/redis latest 235592615444 10 days ago 104 MB
docker.io/nginx latest 2622e6cca7eb 10 days ago 132 MB
docker.io/sonatype/nexus3 latest 57a6261043b9 2 months ago 644 MB
docker.io/hello-world latest bf756fb1ae65 5 months ago 13.3 kB
docker.io/mysql 5.7.25 98455b9624a9 15 months ago 372 MB
# 卸载Docker
# 1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2. 删除资源文件
rm -rf /var/lib/docker
# 3. Docker的默认工作路径
/var/lib/docker
# 1.在docker目录中新建文件夹
sudo mkdir -p /etc/docker
# 2.配置了一个阿里云的地址 (只有当前的服务器可以用,内网地址)
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://14ukbbwv.mirror.aliyuncs.com"]
}
EOF
# 3.重启镜像
sudo systemctl daemon-reload
# 4.重启docker
sudo systemctl restart docker
# 5. 配置完毕
[root@iZhp3do4qhzu84445osa3sZ ~]# sudo mkdir -p /etc/docker
[root@iZhp3do4qhzu84445osa3sZ ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://14ukbbwv.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://14ukbbwv.mirror.aliyuncs.com"]
}
[root@iZhp3do4qhzu84445osa3sZ ~]# sudo systemctl daemon-reload
[root@iZhp3do4qhzu84445osa3sZ ~]# sudo systemctl restart docker
[root@iZhp3do4qhzu84445osa3sZ ~]#
执行 docker run hello-world 后的执行流程分析
[root@iZhp3do4qhzu84445osa3sZ ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 没有寻找到hello-world镜像
Trying to pull repository docker.io/library/hello-world ... #尝试从远程仓库中寻找hello-world镜像
latest: Pulling from docker.io/library/hello-world #在远程仓库寻找到了该镜像,并且正在pull
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for docker.io/hello-world:latest
Hello from Docker! #运行成功显示 相当于Docker安装成功了
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Docker 在执行 docker run 这个命令的流程图
Docker 未找到镜像示例
[root@iZhp3do4qhzu84445osa3sZ ~]# docker run hzbs66666666666666
Unable to find image 'hzbs66666666666666:latest' locally
Trying to pull repository docker.io/library/hzbs66666666666666 ...
/usr/bin/docker-current: repository docker.io/hzbs66666666666666 not found: does not exist or no pull access.
See '/usr/bin/docker-current run --help'.
[root@iZhp3do4qhzu84445osa3sZ ~]#
Dcoker是如何工作的
Docker是C/S Client-Server 结构的系统 ,Docker的守护进程一直在宿主机上运行,通过Socket 从客户端访问!
DcoekrServer在接收到Docker-Client 的指令,就会执行这个命令!
每一个容器都是独立的环境 可以同时开启8080端口 互不影响!
Docker为什么比VM块
docker | LXC | VM | |
---|---|---|---|
虚拟化类型 | OS虚拟化 | OS虚拟化 | 硬件虚拟化 |
性能 | =物理机性能 | =物理机性能 | 5%-20%的性能损耗 |
隔离性 | NS隔离 | NS隔离 | 强 |
Qos | Ggroup弱 | Ggroup弱 | 强 |
安全性 | 中 | 差 | 强 |
GuestOS | 只支持Linux | 只支持Linux | 全部 |
可迁移 | 强 | 弱 | 强 |