Docker_1 安装Docker-CE

安装

Docker-CE
安装过程参考官网,Ubuntu中如下:

## 1. 从仓库安装
$ sudo apt-get update

# 使apt使用https的仓库
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

# 添加docker的官方GPG Key
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 验证
$ sudo apt-key fingerprint 0EBFCD88

pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22

# 添加稳定版仓库
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# 再次update
$ sudo apt-get update

# 安装docker-ce,这一步比较慢,清华的源也是在官网下载安装的
$ sudo apt-get install docker-ce

# hello world
$ sudo docker run hello-world


## 1.1 安装其他可用版本
# 显示可用版本
$ apt-cache madison docker-ce
# 指定版本
$ sudo apt-get install docker-ce=<VERSION>

## 2. 从deb包安装
# 从https://download.docker.com/linux/ubuntu/dists/选择对应的发行版本,进入pool/stable/amd64,下载一个合适的版本
$ sudo dpkg -i /path/to/package.deb

nvidia-docker
nvidia-docker用于运行基于GPU的程序,参考github.

# 删除nvidia-docker1.0的容器,卸载nvidia-docker1.0
docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo apt-get purge -y nvidia-docker

# 添加仓库
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
  sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-docker.list | \
  sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update

# 安装nvidia-docker2,重新加载docker daemon的配置
sudo apt-get install -y nvidia-docker2
sudo pkill -SIGHUP dockerd

# 测试nvidia-smi命令
docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
  • 安装nvidia-docker2之后的/etc/docker/daemon.json
{
    "runtimes": {
        "nvidia": {
            "path": "/usr/bin/nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}

免sudo运行docker命令

  • 创建docker组
    $ sudo groupadd docker
  • 添加当前用户到该docker组
    $ sudo usermod -aG docker $USER
  • 注销后重新登录
  • 运行hello-world程序进行测试。如果提示以下错误:
WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied

原因是在当前用户加入docker组之前以sudo权限配置了docker,配置目录~/.docker/的权限是sudo的。解决方法有两个:

  • 删除~/.docker/,重新配置
  • 用下面的命令更改权限
$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "/home/$USER/.docker" -R

ustc mirrors

在/etc/docker/daemon.json中添加新的项,使用中科大的docker镜像:

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

然后重启docker服务
sudo service docker restart

service failed

Fixing the Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details. problem, for me this worked:

  1. sudo rm -rf /var/lib/docker
  2. create daemon.json in /etc/docker/
  3. put this in it:
    {
    "exec-root": "/path/to/docker/run",
    "storage-driver": "overlay",
    "graph": "/path/to/docker/lib"
    }
  4. systemctl start docker
  5. docker run --rm hello-world
posted @ 2018-02-06 11:21  actonton  阅读(470)  评论(0编辑  收藏  举报