docker入门之安装教程

本篇介绍了在Mac OS, Windows, Linux下的Docker安装方法。

先来一个官方的介绍:

Docker Engine is available for Linux (CentOS, Debian, Fedora, Oracle Linux, RHEL, SUSE, and Ubuntu) or Windows Server operating systems and is based on containerd - the open source container runtime project that Docker donated to the Cloud Native Computing Foundation (CNCF) in 2017. It is available as both a free community-supported engine and as a commercially-supported enterprise engine (Docker Engine-Enterprise) that also forms the foundation for an enterprise container platform.

基本上全平台已经覆盖,英文好的同学,可以自取。

Mac OS上的Docker安装方法

方法一官方dmg

官方下载地址:

Docker Descktop for Mac

对系统的要求:

Requires Apple Mac OS Sierra 10.12 or above. Download Docker Toolbox for previous OS versions.

只要系统是 Mac OS Sierra 10.12以上即可。

下载完Docker.dmg安装包之后,双击即可以安装,可能需要系统管理员权限,输入密码即可。

运行过后,小图标:

Mac上Docker安装运行之后

安装完之后,在终端工具中,使用docker version来查看Docker版本。

docker版本

设置中国区加速地址:

docker加速

阿里云加速:Docker 镜像加速器

方法二brew cask:

brew update 

brew cask install docker

# 删除的方法, 还需要手动删除Docker.app
brew cask uninstall docker

上面的命令将会把Docker安装在Applications目录下。

Windows上Docker安装

官方下载地址:Docker Desktop for Windows

对系统的要求:

Requires Microsoft Windows 10 Professional or Enterprise 64-bit. For previous versions get Docker Toolbox.

需要Windows 10专业版及企业的64位版本,在Windows server 2016以上亲测是可用的。Windows 8/7/Vista/Xp之类的,就别想了,老实去装Windows 10或者虚拟机中去使用。

windows-docker安装示例

Linux上Docker安装

Centos中Docker安装方法

  1. 先删除旧的版本(如果没有可以跳过)
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
  1. 安装必须的依赖:
$ sudo yum install -y yum-utils \
 device-mapper-persistent-data \
 lvm2

添加stable的Docker-ce的源:

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
  1. 安装docker-ce:
$ sudo yum install docker-ce docker-ce-cli containerd.io
  1. 选择指定的安装版本(可选)
$ 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

我们来举个例子,比如我们要安装3:18.09.1-3.el7这个版本,使用如下命令结构:

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

命令说明:

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

$ sudo yum install -y docker-ce-18.09.1 docker-ce-cli-18.09.1
  1. 启动服务并测试一下:
# 启动服务
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/

如果看到上面的提示,说明Docker已经成功安装并运行了了。

  1. 关于升级&删除:

升级:

# 更新所有
yum -y update

# 更新指定
yum -y update docker-ce docker-ce-cli containerd.io

删除:

sudo yum remove docker-ce

# 删除文件系统
sudo rm -rf /var/lib/docker

Debian中Docker的安装方法

  1. 删除旧的版本(可跳过)
$ sudo apt-get remove docker docker-engine docker.io containerd runc
  1. 安装依赖:
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common
    
# 添加GPG key
$ curl -fsSL https://download.docker.com/linux/debian/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) 
sub   4096R/F273FCD8 2017-02-22
  1. 使用stable安装源:
    x86_64/amd64
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"

armhf

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

arm64

$ sudo add-apt-repository \
"deb [arch=arm64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"
  1. 安装docker-ce
$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 安装指定的版本:(可选)
$ apt-cache madison docker-ce

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

安装格式:

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

举例说明:比如要安装5:18.09.1~3-0~debian-stretch版本的docker的话:

$ sudo apt-get install docker-ce=18.09.1 docker-ce-cli=18.09.1 containerd.io
  1. 启动服务并测试:
$ sudo service docker start

# 查看Docker运行状态
$ sudo service docker status

$ sudo docker run hello-world

Ubuntu中Docker安装方法

  1. 删除旧的版本(可跳过)
$ sudo apt-get remove docker docker-engine docker.io containerd runc
  1. 安装依赖:
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
    
# 添加GPG key
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

$ 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) 
sub   rsa4096 2017-02-22 [S]
  1. 使用stable安装源:
  • x86_64/amd64
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  • armhf
$ sudo add-apt-repository \
   "deb [arch=armhf] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  • arm64
$ sudo add-apt-repository \
   "deb [arch=arm64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  • ppc64le
$ sudo add-apt-repository \
   "deb [arch=ppc64el] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  • s390x
$ sudo add-apt-repository \
   "deb [arch=s390x] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  1. 安装docker-ce
$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 安装指定的版本:(可选)
$ 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
  ...

安装格式:

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

举例说明:比如要安装5:18.09.1~3-0~ubuntu-xenial版本的docker的话:

$ sudo apt-get install docker-ce=18.09.1 docker-ce-cli=18.09.1 containerd.io
  1. 启动服务并测试:
 $ sudo service docker start

# 查看Docker运行状态
$ sudo service docker status

$ sudo docker run hello-world

Docker-compose集合命令

Compose工具是一个批量工具,用于运行与管理多个docker容器。

官方文档:Install Docker Compose

  1. 在Mac/Windows中,已经集成了docker-compose命令

  2. 在WindowsServer中
    先启动PowerShell

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

然后运行如下命令:

Invoke-WebRequest "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFiles\Docker\docker-compose.exe

然后测试一下:docker-compose --version

  1. Linux中:
# 下载docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 给予执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 测试命令
$ docker-compose --version
docker-compose version 1.23.2, build 1110ad01

你可能感兴趣的:(docker入门之安装教程)