这篇文章上次修改于 1460 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

封面

这里以 Debian 9 为例展示如何安装 Docker 运行环境

安装 Docker-ce

方法一

这里使用TUNA镜像源进行安装,适用于国内

首先卸载之前的 docker 并安装依赖

sudo apt remove docker docker-engine docker.io
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

添加 Docker 官方 GPG 公钥

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

添加软件仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
   $(lsb_release -cs) \
   stable"

安装 Docker-ce

sudo apt update 
sudo apt install docker-ce

方法二

运行Docker官方提供的安装脚本

wget -qO- get.docker.com | bash
# 或者 
curl -Lso- get.docker.com | bash

修改 Docker Hub 源

sudo vim /etc/docker/daemon.json

在文件里面添加

{
      "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}

也可以使用 Docker 官方提供的镜像

https://registry.docker-cn.com

重启 Docker

sudo systemctl restart docker

看一下是否成功启用

$ sudo docker info 

......

 Registry Mirrors:
  https://docker.mirrors.ustc.edu.cn/

......

安装 Docker-Compose

使用 apt 安装

sudo apt install docker-compose

或者直接下载二进制程序安装

curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 获取 docker-compose 运行文件 (v1.22.0)
chmod +x /usr/local/bin/docker-compose
# 添加执行权限
sudo docker-compose --version
# 看一下

测试 Docker 是否安装正确

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

参考

文章头图 12の月のイヴ
USTC Mirror Help
Get Docker CE for Debian
Debian 安装 Docker CE
TUNA Docker Community Edition 镜像使用帮助