石头记

Docker、Kubernetes、CI/CD 等技术分享

Hello Docker

环境选择

容器需要管理工具、运行时和操作系统,我们的选择如下:

  • 管理工具 - Docker Engine
    因为 Docker 最流行使用最广泛

  • 运行时 - runc
    Docker 的默认 runtime

  • 操作系统 - Ubuntu
    选择大家熟悉的操作系统

安装 Docker

我们将在 ubuntu 16.04 虚拟机中安装 Docker。因为安装过程需要访问 internet, 所以虚拟机必须能够上网。

Docker 支持几乎所有的 Linux 发行版,也支持 Mac 和 Windows。各操作系统的安装方法可以访问:https://docs.docker.com/install/

Docker 分为开源免费的 CE(Community Edition)版本和收费的 EE(Enterprise Edition)版本。下面我们将按照文档,通过以下步骤在 Ubuntu 16.04 上安装 Docker CE 版本。

配置 Docker 的 apt 源

  1. 安装包,允许 apt 命令 HTTPS 访问 Docker 源。
1
2
3
4
5
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
  1. 添加 Docker 官方的 GPG
1
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  1. 将 Docker 的源添加到 /etc/apt/sources.list
1
2
3
4
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

安装 Docker

1
2
$ sudo apt-get update
$ sudo apt-get install docker-ce

运行第一个容器

环境就绪,马上运行第一个容器,执行命令:

1
$ sudo docker run -d -p 80:80 httpd

其过程可以简单的描述为:

  • 从 Docker Hub 下载 httpd 镜像。镜像中已经安装好了 Apache HTTP Server
  • 启动 httpd 容器,并将容器的 80 端口映射到 host 的 80 端口

下面我们可以通过浏览器验证容器是否正常工作。在浏览器中输入 http://[your ubuntu host IP]

成功访问

可以访问容器的 http 服务了,第一个容器运行成功!我们轻轻松松就拥有了一个 WEB 服务器。随着学习的深入,会看到容器技术带给我们更多的价值。

镜像下载加速

由于 Docker Hub 的服务器在国外,下载镜像会比较慢。我们可以去阿里巴巴或 DaoCloud 申请一个国内镜像地址。

阿里镜像下载加速

配置镜像下载加速并重启docker服务:

1
2
3
4
5
6
7
8
$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxl8pb5.mirror.aliyuncs.com"]
}
EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

Proudly powered by Hexo and Theme by Hacker
© 2019 ist0ne