【问题标题】:Automate Docker Run command on Sagemaker's Notebook Instance在 Sagemaker 的笔记本实例上自动执行 Docker Run 命令
【发布时间】:2021-06-26 15:48:40
【问题描述】:
我在 AWS ECR 中有一个 Docker 映像,我打开了我的 Sagemaker Notebook 实例--->转到终端-->docker run....
这就是我启动 Docker 容器的方式。
现在,我想自动化这个过程(在 Sagemaker Notebook 实例上运行我的 docker 映像),而不是输入 docker run 命令。
我可以在 Sagemaker 上创建一个 cron 作业吗?或者还有其他方法吗?
谢谢
【问题讨论】:
标签:
amazon-web-services
docker
containers
amazon-sagemaker
【解决方案1】:
为此,您可以在 SageMaker 笔记本中创建一个内联 Bash shell,如下所示。这将获取您的 Docker 容器、创建映像、ECR 存储库(如果不存在)并推送映像。
%%sh
# Name of algo -> ECR
algorithm_name=your-algo-name
cd container #your directory with dockerfile and other sm components
chmod +x randomForest-Petrol/train #train file for container
chmod +x randomForest-Petrol/serve #serve file for container
account=$(aws sts get-caller-identity --query Account --output text)
# Region, defaults to us-west-2
region=$(aws configure get region)
region=${region:-us-west-2}
fullname="${account}.dkr.ecr.${region}.amazonaws.com/${algorithm_name}:latest"
# If the repository doesn't exist in ECR, create it.
aws ecr describe-repositories --repository-names "${algorithm_name}" > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ecr create-repository --repository-name "${algorithm_name}" > /dev/null
fi
# Get the login command from ECR and execute it directly
aws ecr get-login-password --region ${region}|docker login --username AWS --password-stdin ${fullname}
# Build the docker image locally with the image name and then push it to ECR
# with the full name.
docker build -t ${algorithm_name} .
docker tag ${algorithm_name} ${fullname}
docker push ${fullname}
我代表我的雇主 AWS 做出贡献。我的贡献是在 MIT 许可下获得许可的。有关更详细的说明,请参见此处
https://aws-preview.aka.amazon.com/tools/stackoverflow-samples-license/
【解决方案2】:
SageMaker Notebook 实例lifecycle configuration script 可用于在您创建笔记本时或启动时运行脚本。在此脚本中,您可以在创建时或启动时从笔记本访问其他 AWS 资源,例如访问您的 ECR 映像并使用 shell 脚本自动启动 docker 容器。这个script 展示了如何使用 cron 来安排某些操作的示例,可以根据您的用例进行修改
参考this github页面中的更多生命周期配置示例