创建名为
Dockerfile
的文件。Dockerfile 是一个清单文件,描述了用于 Docker 镜像的基本镜像以及要安装的项目以及在此项目上运行的内容。有关 Dockerfile 的更多信息,请转到
Dockerfile 参考
。
touch Dockerfile
编辑您刚刚创建的
Dockerfile
并添加以下内容。
FROM ubuntu:18.04
# Install dependencies
RUN apt-get update && \
apt-get -y install apache2
# Install apache and write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html
# Configure apache
RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \
echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \
echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \
echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \
chmod 755 /root/run_apache.sh
EXPOSE 80
CMD /root/run_apache.sh
此 Dockerfile 使用 Ubuntu 18.04 镜像。
RUN
指令将更新包缓存,安装一些适用于 Web 服务器的程序包,然后将“Hello World!” 内容写入到 Web 服务器的文档根目录。
EXPOSE
指令在容器上公开端口 80,
CMD
指令启动 Web 服务器。
从您的 Dockerfile 生成 Docker 镜像。
Docker 的某些版本可能需要在以下命令中使用 Dockerfile 完整路径,而不是所示的相对路径。
docker build -t hello-world .
运行
docker images
以验证是否已正确创建镜像。
docker images --filter reference=hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest e9ffedc8c286 4 minutes ago 241MB
运行新构建的镜像。
-p 80:80
选项将容器上公开的端口 80 映射到主机系统上的端口 80。有关
docker run
的更多信息,请转到
Docker 运行参考
。
docker run -t -i -p 80:80 hello-world
来自 Apache Web 服务器的输出将显示在终端窗口中。您可以忽略“
Could not reliably determine the server's fully
qualified domain name
”消息。
打开浏览器并指向正在运行 Docker 并托管您的容器的服务器。
如果您使用的是 EC2 实例,这将是服务器的
Public DNS
值,此值与您用于通过 SSH 连接到实例的地址相同。确保实例的安全组允许端口 80 上的入站流量。
如果您正在本地运行 Docker,可将您的浏览器指向
http://localhost/
。
如果你正在使用
docker-machine
在 Windows 或 Mac 计算机上,找到的 IP 地址VirtualBox使用 Docker 托管 Docker 的虚拟机
docker-machine ip
命令,替换
机器名
使用你正在使用的 docker 机器的名称。
docker-machine ip machine-name
您应看到一个显示“Hello World!”语句的 网页。
通过键入
Ctrl + c
来停止 Docker 容器。
将映像推送到 Amazon Elastic Container Registry
Amazon ECR 是一项托管 AWS Docker 注册表服务。您可以使用 Docker CLI 在 Amazon ECR 存储库中推送、拉取和托管映像。有关 Amazon ECR 产品详细信息、特色客户案例研究和常见问题解答,请参阅
Amazon Elastic Container Registry 产品详细信息页面
。
标记映像并将其推送至 Amazon ECR
-
创建用于存储您的
hello-world
映像的 Amazon ECR 存储库。在输出中记下
repositoryUri
。
将
region
替换为您的 AWS 区域,例如
us-east-1
。
aws ecr create-repository --repository-name hello-repository --region region
"repository":
{
"registryId": "
aws_account_id
",
"repositoryName": "hello-repository",
"repositoryArn": "arn:aws:ecr:
region
:
aws_account_id
:repository/hello-repository",
"createdAt": 1505337806.0,
"repositoryUri": "
aws_account_id
.dkr.ecr.
region
.amazonaws.com/hello-repository"
使用上一步中的
repositoryUri
值标记
hello-world
映像。
docker tag hello-world aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
运行
aws ecr get-login-password
命令。指定要对其进行身份验证的注册表 URI。有关更多信息,请参阅
Amazon Elastic Container Registry 用户指南
中的
注册表身份验证
。
docker login -u AWS -p $(aws ecr get-login-password --region REGION) aws_account_id.dkr.ecr.REGION.amazonaws.com
Login Succeeded
使用上一步中的
repositoryUri
值将映像推送至 Amazon ECR。
docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
要继续创建 Amazon ECS 任务定义并使用容器镜像启动任务,请跳至
后续步骤
。完成试验 Amazon ECR 映像后,您可以删除存储库,从而无需为映像存储付费。
aws ecr delete-repository --repository-name hello-repository --region region --force
您的任务定义需要任务执行角色。有关更多信息,请参阅
Amazon ECS 任务执行 IAM 角色
:
在创建容器镜像并将其推送到 Amazon ECR 后,您应考虑以下后续步骤。