轻松 运行 AWS 上的单个 Dockerfile

Easily run a single Dockerfile on AWS

假设我有一个简单的独立 Dockerfile 及其 entrypoint.sh。容器已构建并存储在 AWS ECR 上。

在本地,我运行是这样的:

docker run —rm my_container my_parameters

我不需要负载均衡,也不需要在执行后让容器保持活动状态,只是为了按需执行(通过 AWS API)。

在 AWS 上执行此操作的最简单方法是什么?

这是 AWS.

上 运行 最简单容器的顶层 3 层
  1. 创建集群
  2. 创建任务定义
  3. 创建服务

但是在上面的序列中,你的命令docker run —rm my_container my_parameters落在第3层。因为layer 1在整个生命周期中只需要创建一次,而layer 2需要更新(如果有的话)需要更改端口更新或任务定义中的任何 ENV,但在某些情况下这并不常见。

最好使用 API 或 AWS CLI 管理 layer3。正如您按需提到的那样,您也可以 运行 使用 schedule based 容器,该容器将 运行 并在特定时间停止。

bash脚本将创建任务定义(模板),启动服务(运行容器) 在另一个(停止容器)中停止服务

#!/bin/bash
AWS_PROFILE="test"
COMMAND=""
ECS_CLUSTER="test"
if [ "${COMMAND}" == "setup_task" ]; then
aws ecs register-task-definition --cli-input-json file://taskdefinition.json --profile $AWS_PROFILE
elif [ "${COMMAND}" == "run_container" ]; then
aws ecs create-service  --cluster $ECS_CLUSTER  --service-name ecs-simple-service    --task-definition nginx    --desired-count 1 --profile $AWS_PROFILE
elif [ "${COMMAND}" == "stop_container" ];then
aws ecs delete-service --cluster $ECS_CLUSTER --service ecs-simple-service --force --profile $AWS_PROFILE
else
>&2 echo "Wrong Argument passed! Valid option is setup_task, run_container and stop_container"
fi

taskdefinition

taskdefinition.json


{
  "containerDefinitions": [
  {
    "cpu": 0,
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "/ecs/stage-background-worker",
        "awslogs-region": "us-west-2",
        "awslogs-stream-prefix": "ecs"
  }
},
    "environment": [
      {
        "name": "NODE_ENV",
        "value": "staging"
      }
    ],


    "memoryReservation": 400,
    "image": "youarn.dkr.ecr.us-west-2.amazonaws.com:latest",
    "dockerLabels": {
      "Name": "test"
    },
    "privileged": true,
    "name": "test"
  }
],

"family": "test"
}

这基于 AWS-CLI,您可以探索任何编程语言的相同内容here