Skip to content

Instantly share code, notes, and snippets.

@tuliocasagrande
Last active May 31, 2022 18:46
Show Gist options
  • Save tuliocasagrande/2d2e87a362b8eba2998658634da6e327 to your computer and use it in GitHub Desktop.
Save tuliocasagrande/2d2e87a362b8eba2998658634da6e327 to your computer and use it in GitHub Desktop.

Running a SageMaker Docker Image Locally

Download image locally

  1. (Optional) Grab the image URI using the SageMaker Python SDK:

     In [1]: import sagemaker
     In [2]: sagemaker.image_uris.retrieve("pytorch", "us-east-1", version="1.10", image_scope="inference", instance_type="ml.p3.2xlarge")
     Out[2]: '763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:1.10-gpu-py38'
    
  2. Docker login (docker login is made with the registry URI, portion before slash):

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 763104351884.dkr.ecr.us-east-1.amazonaws.com
    
  3. Docker pull:

    docker pull 763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:1.10-gpu-py38
    

Docker run in interactive mode

docker run -it --rm 763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:1.10-gpu-py38 bash

Docker run in detached mode

Here, we're also mounting a local src/ directory on /opt/ml inside the container. Any changes on src/ are automatically reflected to the container.

docker run -d -it \
  --name my_container_name \
  --rm \
  --volume $(pwd)/src:/opt/ml \
  --workdir /opt/ml \
  763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:1.10-gpu-py38

Run a command on a running container

docker exec -it my_container_name python3 run.py

Clean up

Stop all containers

docker stop $(docker ps -qa)

Remove all containers

docker rm $(docker ps -qa)

Remove all images

docker rmi $(docker images -qa)

Remove all unused data (containers, images, volumes)

docker system prune --volumes --all --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment