Docker 与 虚拟机 区别
- 虚拟机:在基础设施上,运用虚拟化平台,创建了多台虚拟机,单个的虚拟机是一个用户的操作系统,占用了一部分系统资源。在资源内部运行了一个app
- docker:多个 app 共用了一个操作系统以及里面的基础设施,由 Docker 进程来管理这些应用。
- docker 运行一个 app 时速度非常快,而虚拟机则会把基础设施的资源完全剥离出来,在虚拟的内部运行操纵系统,在操作系统之上运行一个app
Docker主要特性
- 文件、资源、网络隔离
- 变更管理、日志记录
- 写时复制
安装 Docker
先删除旧的版本(没有可以跳过)
$ sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine
安装必须的依赖:
$ sudo yum install -y yum-utils \device-mapper-persistent-data \lvm2
添加
stable的Docker-ce的源:$ sudo yum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.repo
安装
docker-ce:$ sudo yum install docker-ce docker-ce-cli containerd.io
centos8 安装 Docker 出现 Podman 冲突的话执行执行
$ yum erase podman buildah选择指定的安装版本(可选) ```bash $ yum list docker-ce —showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
我们来举个例子,比如我们要安装`3:18.09.1-3.el7`这个版本,使用如下命令结构:```bash$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
命令说明:
第一部分是docker-ce,第二部分是版本号18.09.1,看明白了吗?就是这样子:
$ sudo yum install -y docker-ce-18.09.1 docker-ce-cli-18.09.1
来一个Hello World吧
$ sudo docker run hello-world Unable to find image ‘hello-world:latest’ locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Status: Downloaded newer image for hello-world:latest
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
- The Docker client contacted the Docker daemon.
- The Docker daemon pulled the “hello-world” image from the Docker Hub. (amd64)
- The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
- 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已经成功安装并运行了了6. Docker 镜像加速国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:- 科大镜像:**[https://docker.mirrors.ustc.edu.cn/](https://docker.mirrors.ustc.edu.cn/)**- 网易:**[https://hub-mirror.c.163.com/](https://hub-mirror.c.163.com/)**- 阿里云:**[https://<你的ID>.mirror.aliyuncs.com](https://%3C%E4%BD%A0%E7%9A%84ID%3E.mirror.aliyuncs.com)**- 七牛云加速器:**[https://reg-mirror.qiniu.com](https://reg-mirror.qiniu.com)**第一步:挑选一个加速源<br />第二步:在 **/etc/docker/daemon.json** 中写入如下内容(如果文件不存在请新建该文件):```json{"registry-mirrors": ["加速源地址"]}
第三步:重新启动服务:
$ sudo systemctl daemon-reload$ sudo systemctl restart docker
- 关于升级&删除:
升级:
# 更新所有$ yum -y update# 更新指定$ yum -y update docker-ce docker-ce-cli containerd.io
删除:
$ sudo yum remove docker-ce# 删除文件系统$ sudo rm -rf /var/lib/docker
Docker 常用命令
# 删除容器 | 停止容器 | 运行容器 | 查看容器日志$ docker rm(| stop | start | log) 容器名称
demo:运行一个 mysql 容器
打开 docker-hub 官网,搜索 mysql,根据官网来进行操作(如下图):
# 安装 mysql容器$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql# --name 用户名# MYSQL_ROOT_PASSWORD 密码# -d 后台运行的意思# mysql后面不加东西,就会默认去拉取最新的镜像
# 添加端口映射$ docker run --name imooc-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 28001:3306 -d mysql# -p 服务器对外的端口:镜像的内的端口# 在这里可以看到 docker 的写时复制,因为已然安装过mysql了所以一秒钟就又一次拉去下来了# 防火墙打开 28001 端口
使用 Navicat 来测试一下宿主机的连接
Docker采用的是资源共享型的管理方案,所有的Docker使用的硬件资源由docker daemon进行管理与分配。
