docker轻松使用一 docker安装

安装

卸载旧版本
旧版本被称为docker,docker.io 或docker-engine。如果已安装,请卸载它们:

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

使用存储库安装
在新主机上首次安装Docker CE之前,需要设置Docker存储库。之后,您可以从存储库安装和更新Docker。

设置存储库
更新apt包索引:

$ sudo apt-get update

允许apt通过HTTPS使用存储库:

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

添加Docker的官方GPG密钥:

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

9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88通过搜索指纹的最后8个字符,验证您现在拥有带指纹的密钥 。

$ sudo apt-key fingerprint 0EBFCD88
    
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]



$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

lsb_release -cs子命令返回Ubuntu发行版的名称

安装DOCKER CE
更新apt包索引。

$ sudo apt-get update

安装最新版本

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

特定版本

$ apt-cache madison docker-ce

  docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 18.06.1~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 18.06.0~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
  ...

例如,使用第二列中的版本字符串安装特定版本5:18.09.1~3-0~ubuntu-xenial

$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

验证是否正确安装了Docker CE 。

$ sudo docker run hello-world

正确会返回如下

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/

安装后步骤

创建docker组。

$ sudo groupadd docker

将您的用户添加到该docker组。

$ sudo usermod -aG docker $USER

注销并重新登录,以便重新评估您的组成员身份。
如果在虚拟机上进行测试,则可能需要重新启动虚拟机才能使更改生效。
在桌面Linux环境(如X Windows)上,完全注销会话,然后重新登录。

验证您是否可以运行docker命令sudo。

$ docker run hello-world

配置Docker以在启动时启动

大多数当前的Linux发行版(RHEL,CentOS,Fedora,Ubuntu 16.04及更高版本)用于systemd管理系统启动时启动的服务。Ubuntu 14.10及以下使用upstart。

systemd

$ sudo systemctl enable docker

要禁用此行为,请disable改用。

$ sudo systemctl disable docker

如果需要添加HTTP代理,为Docker运行时文件设置不同的目录或分区,或进行其他自定义,请参阅 自定义systemd Docker守护程序选项。

upstart
Docker自动配置为在启动时启动 upstart。要禁用此行为,请使用以下命令:

$ echo manual | sudo tee /etc/init/docker.override

chkconfig

$ sudo chkconfig docker on

更多精彩内容