Docker 与 虚拟机 区别

  • 虚拟机:在基础设施上,运用虚拟化平台,创建了多台虚拟机,单个的虚拟机是一个用户的操作系统,占用了一部分系统资源。在资源内部运行了一个app
  • docker:多个 app 共用了一个操作系统以及里面的基础设施,由 Docker 进程来管理这些应用。
  • docker 运行一个 app 时速度非常快,而虚拟机则会把基础设施的资源完全剥离出来,在虚拟的内部运行操纵系统,在操作系统之上运行一个app

image.png

Docker主要特性

  • 文件、资源、网络隔离
  • 变更管理、日志记录
  • 写时复制

安装 Docker

  1. 先删除旧的版本(没有可以跳过)

    1. $ sudo yum remove docker \
    2. docker-client \
    3. docker-client-latest \
    4. docker-common \
    5. docker-latest \
    6. docker-latest-logrotate \
    7. docker-logrotate \
    8. docker-engine
  2. 安装必须的依赖:

    1. $ sudo yum install -y yum-utils \
    2. device-mapper-persistent-data \
    3. lvm2

    添加stable的Docker-ce的源:

    1. $ sudo yum-config-manager \
    2. --add-repo \
    3. https://download.docker.com/linux/centos/docker-ce.repo
  3. 安装docker-ce:

    1. $ sudo yum install docker-ce docker-ce-cli containerd.io

    centos8 安装 Docker 出现 Podman 冲突的话执行执行 $ yum erase podman buildah

  4. 选择指定的安装版本(可选) ```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

  1. 我们来举个例子,比如我们要安装`3:18.09.1-3.el7`这个版本,使用如下命令结构:
  2. ```bash
  3. $ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

命令说明:
第一部分是docker-ce,第二部分是版本号18.09.1,看明白了吗?就是这样子:

  1. $ sudo yum install -y docker-ce-18.09.1 docker-ce-cli-18.09.1
  1. 启动服务并测试一下: ```bash

    启动服务

    $ sudo systemctl start docker

来一个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:

  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/

  1. 如果看到上面的提示,说明Docker已经成功安装并运行了了
  2. 6. Docker 镜像加速
  3. 国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:
  4. - 科大镜像:**[https://docker.mirrors.ustc.edu.cn/](https://docker.mirrors.ustc.edu.cn/)**
  5. - 网易:**[https://hub-mirror.c.163.com/](https://hub-mirror.c.163.com/)**
  6. - 阿里云:**[https://<你的ID>.mirror.aliyuncs.com](https://%3C%E4%BD%A0%E7%9A%84ID%3E.mirror.aliyuncs.com)**
  7. - 七牛云加速器:**[https://reg-mirror.qiniu.com](https://reg-mirror.qiniu.com)**
  8. 第一步:挑选一个加速源<br />第二步:在 **/etc/docker/daemon.json** 中写入如下内容(如果文件不存在请新建该文件):
  9. ```json
  10. {
  11. "registry-mirrors": ["加速源地址"]
  12. }

第三步:重新启动服务:

  1. $ sudo systemctl daemon-reload
  2. $ sudo systemctl restart docker
  1. 关于升级&删除:

升级:

  1. # 更新所有
  2. $ yum -y update
  3. # 更新指定
  4. $ yum -y update docker-ce docker-ce-cli containerd.io

删除:

  1. $ sudo yum remove docker-ce
  2. # 删除文件系统
  3. $ sudo rm -rf /var/lib/docker

Docker 常用命令

  1. # 删除容器 | 停止容器 | 运行容器 | 查看容器日志
  2. $ docker rm(| stop | start | log) 容器名称

demo:运行一个 mysql 容器

打开 docker-hub 官网,搜索 mysql,根据官网来进行操作(如下图):
image.png

  1. # 安装 mysql容器
  2. $ docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
  3. # --name 用户名
  4. # MYSQL_ROOT_PASSWORD 密码
  5. # -d 后台运行的意思
  6. # mysql后面不加东西,就会默认去拉取最新的镜像
  1. # 添加端口映射
  2. $ docker run --name imooc-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 28001:3306 -d mysql
  3. # -p 服务器对外的端口:镜像的内的端口
  4. # 在这里可以看到 docker 的写时复制,因为已然安装过mysql了所以一秒钟就又一次拉去下来了
  5. # 防火墙打开 28001 端口

使用 Navicat 来测试一下宿主机的连接
image.png

Docker采用的是资源共享型的管理方案,所有的Docker使用的硬件资源由docker daemon进行管理与分配。