Version: Next

Docker安装

Docker的基本组成

  1. 客户端
  2. 服务器
  3. 远程仓库

image-20200520182233592

关于Docker的基本名词

  • 镜像(image):

    docker镜像就像是一个模板,可以通过模板创建容器服务,通过镜像可以创建多个容器,最宠服务运行在容器中

  • 容器(container):

    docker利用容器技术,独立运行一个或者一组应用,通过镜像创建 容器可以执行一些基本命令:

    • 启动
    • 停止
    • 删除

    可以将容器理解为简易的Linux

  • 仓库(repository):

    仓库就是存放镜像的地方,仓库分为私有仓库和共有仓库

    • 官方 Docker Hub
    • 国内 阿里云等都有容器服务器

安装Docker

环境准备

  1. 一定的Linux基础
  2. Linux发行版系统 (我用的Ubuntu 18.04)
  3. 连接远程服务器的终端,例如Xshell
  • SSH连接Linux服务器
ssh 用户名@ip地址
  • 查看内核版本
uname -r
  • 查看系统信息
cat /etc/os-release

卸载旧版本

sudo apt-get remove docker docker-engine docker.io containerd runc

设置Docker仓库

  1. 更新apt并且安装一些包以让apt可以通过HTTP协议访问仓库
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  1. 添加中科大Docker GPG Key
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  1. 设置仓库,注意更换为国内的仓库
sudo add-apt-repository "deb https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

安装Docker引擎

  1. 更新apt,安装
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 验证安装结果
docker version

终端结果:

root@ubuntu:/# docker version
Client: Docker Engine - Community
Version: 19.03.9
API version: 1.40
Go version: go1.13.10
Git commit: 9d988398e7
Built: Fri May 15 00:25:18 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.9
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 9d988398e7
Built: Fri May 15 00:23:50 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
  1. 运行hello-world程序
sudo docker run hello-world

终端回显:

root@ubuntu:/# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
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/

查看下载的hello-world镜像

docker images
root@ubuntu:/# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 4 months ago 13.3kB

卸载Docker

sudo apt-get purge docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker

配置阿里云镜像加速

  • 登录自己的阿里云

  • 找到容器镜像服务

  • 最下方找到镜像加速器

    登录自己的阿里云查看加速器地址

    https://XXXXxxx.mirror.aliyuncs.com

    根据操作文档提示完成设置

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
    "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker

分析Hello-world执行流程

image-20200520200232458


附录:Mac 安装

直接用迅雷下载 https://download.docker.com/mac/stable/Docker.dmg

  • 安装完毕后,打开 Docker 设置,更换国内源
    • 这里用中科大的,图上是 Docker 中国的,还是很慢
配置国内源
{
"debug": true,
"experimental": false,
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn"
]
}

image-20210114152216423