AWS EC2 上安装 Docker

在 AWS EC2 安装 Docker

参见:Amazon ECS 的 Docker 基本知识

我们在主机 18.222.176.21452.14.52.46 都安装 Docker。

安装最新的 Docker Community Edition 程序包:
sudo yum install -y docker

启动 Docker 服务:
sudo service docker start

将 ec2-user 添加到 docker 组,以便您能够执行 Docker 命令,而无需使用 sudo:
sudo usermod -a -G docker ec2-user

退出,再重新登录以接受新的 Docker 组权限。您的新 SSH 会话将具有相应的 Docker 组权限,而无需再使用 sudo

我们可以使用 docker --version 来查看 Docker 的版本:

我们可以使用 docker info 来查看 Docker 的运行信息,可以看出目前既没有镜像 Image,也没有容器 Container:

创建简单 Web 应用程序的 Docker 镜像

目标:我们在主机 52.14.52.46 上创建镜像,然后在主机 18.222.176.214 上获取镜像。

首先登录到主机 52.14.52.46ssh -i "XiangSecret.pem" [email protected]

创建名为 Dockerfile 的文件。Dockerfile 是一个清单文件,描述了用于 Docker 镜像的基本镜像以及要安装的项目以及在此项目上运行的内容。
touch Dockerfile

编辑 Dockerfile 并添加以下内容:
此 Dockerfile 使用 Ubuntu 12.04 镜像。RUN 指令将更新包缓存,安装一些适用于 Web 服务器的软件包,然后将 Hello World! 内容写入到 Web 服务器的文档根目录。EXPOSE 指令在容器上公开端口 80,CMD 指令启动 Web 服务器。

FROM ubuntu:12.04

# Install dependencies
RUN apt-get update -y
RUN apt-get install -y apache2

# Install apache and write hello world message
RUN echo "Hello World!" > /var/www/index.html

# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80

CMD ["/usr/sbin/apache2", "-D",  "FOREGROUND"]

从 Dockerfile 生成 Docker 镜像:
docker build -t hello-world . 注意该命令最后有一个 . 表示 Dockerfile 在当前目录。

通过 docker images 以验证是否已正确创建镜像:

验证是否已正确创建镜像

运行新构建的镜像。 -p 80:80 选项将容器上公开的端口 80 映射到主机系统上的端口 80。
docker run -p 80:80 hello-world

启动后,通过如下两种方式来访问页面:

通过 docker ps 来查看正在运行的 Docker 容器:

通过 Ctrl + C 来停止 Docker 容器。

将镜像推送至 Amazon Elastic Container Registry

首先需要通过 aws configure 配置好 AWS 账号

如何可以找到 AWS Access Key ID 和 AWS Secret Access Key 呢?通过下图的方式:

 

创建用于存储 hello-world 镜像的 Amazon ECR 存储库。

aws ecr create-repository --repository-name hello-world

在输出中记下 "repositoryUri": "679435956173.dkr.ecr.us-east-2.amazonaws.com/hello-world"
PS:其中 679435956173 是 AWS Account Id。

使用上一步中的 repositoryUri 值标记 hello-world 镜像:
docker tag hello-world 679435956173.dkr.ecr.us-east-2.amazonaws.com/hello-world

运行 aws ecr get-login --no-include-email 命令以获取您的注册表的 docker login 身份验证命令字符串。
运行上一步中返回的 docker login 命令。此命令提供一个在 12 小时内有效的授权令牌。

 

最后,将镜像推送至 Amazon ECR:
docker push 679435956173.dkr.ecr.us-east-2.amazonaws.com/hello-world

在另外一台主机上获取镜像

参见:拉取镜像

通过上面的步骤,我们已经在主机 52.14.52.46 上创建了镜像,现在我们在主机 18.222.176.214 上获取镜像。

首先登录到主机 18.222.176.214ssh -i "XiangSecret.pem" [email protected]

通过 aws ecr describe-repositories 列出一个注册表中的存储库。我们可以看到 hello-world 这个镜像的存储库

通过 aws ecr describe-images 命令描述存储库中的镜像。
aws ecr describe-images --repository-name hello-world

运行 aws ecr get-login --no-include-email 命令以获取您的注册表的 docker login 身份验证命令字符串。
运行上一步中返回的 docker login 命令。此命令提供一个在 12 小时内有效的授权令牌。

通过 docker pull 命令拉取镜像。
镜像名称格式应为 registry/repository[:tag] 以便按标签拉取,或为 registry/repository[@digest] 以便按摘要拉取。
docker pull 679435956173.dkr.ecr.us-east-2.amazonaws.com/hello-world:latest

 

通过 docker images 以验证是否已正确获得镜像:

 

运行新构建的镜像。 -p 80:80 选项将容器上公开的端口 80 映射到主机系统上的端口 80。
docker run -p 80:80 679435956173.dkr.ecr.us-east-2.amazonaws.com/hello-world

启动后,通过如下两种方式来访问页面:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章