Search code examples
amazon-web-servicesdockercontainersamazon-sagemaker

Automate Docker Run command on Sagemaker's Notebook Instance


I have a Docker image in AWS ECR and I open my Sagemaker Notebook instance--->go to terminal-->docker run.... This is how I start my Docker container.

Now, I want to automate this process(running my docker image on Sagemaker Notebook Instance) instead of typing the docker run commands.

Can I create a cron job on Sagemaker? or Is there any other approach?

Thanks


Solution

  • For this you can create an inline Bash shell in your SageMaker notebook as follows. This will take your Docker container, create the image, ECR repo if it does not exist and push the image.

    %%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}
    

    I am contributing this on behalf of my employer, AWS. My contribution is licensed under the MIT license. See here for a more detailed explanation https://aws-preview.aka.amazon.com/tools/stackoverflow-samples-license/