Create a custom image and push to Amazon ECR
This page provides instructions on how to create a local Dockerfile, build the container image, and add it to Amazon Elastic Container Registry (Amazon ECR).
Note
In the following examples, the tags are not specified, and the tag latest
is applied by default. If you would like to specify a tag, you will need to append
:
to end of the image names. For more
information, see docker
image tagtag
Create a local Dockerfile and build the container image
Use the following instructions to create a Dockerfile with your desired software and dependencies.
To create your Dockerfile
-
First set your variables for the Amazon CLI commands that follow.
LOCAL_IMAGE_NAME=
local-image-name
is the name of the container image on your local device, that you define here.local-image-name
-
Create a text-based document, named
Dockerfile
, that meet the specifications in Custom image specifications.Dockerfile
examples for supported applications can be found in Dockerfile samples.Note
If you are bringing your own image to SageMaker Unified Studio, you will need to follow the Dockerfile specifications in the Amazon SageMaker Unified Studio User Guide.
Dockerfile
examples for SageMaker Unified Studio can be found in Dockerfile example in the Amazon SageMaker Unified Studio User Guide. -
In the directory containing your
Dockerfile
, build the Docker image using the following command. The period (.
) specifies that theDockerfile
should be in the context of the build command.docker build -t ${LOCAL_IMAGE_NAME} .
After the build completes, you can list your container image information with the following command.
docker images
-
(Optional) You can test your image by using the following command.
docker run -it ${LOCAL_IMAGE_NAME}
In the output you will find that your server is running at a URL, like
http://127.0.0.1:8888/...
. You can test the image by copying the URL into the browser.If this does not work, you may need to include
-p
in the docker run command. This option maps the exposed port on the container to a port on the host system. For more information about docker run, see the Running containersport
:port
in the Docker documentation. Once you have verified that the server is working, you can stop the server and shut down all kernels before continuing. The instructions are viewable the output.
Add a Docker image to Amazon ECR
To add a container image to Amazon ECR, you will need to do the following.
-
Create an Amazon ECR repository.
-
Log in to your default registry.
-
Push the image to the Amazon ECR repository.
Note
The Amazon ECR repository must be in the same Amazon Web Services Region as the domain you are attaching the image to.
To build and push the container image to Amazon ECR
-
First set your variables for the Amazon CLI commands that follow.
ACCOUNT_ID=
account-id
REGION=aws-region
ECR_REPO_NAME=ecr-repository-name
-
is your account ID. You can find this at the top right of any Amazon console page. For example, the SageMaker AI consoleaccount-id
. -
is the Amazon Web Services Region of your Amazon SageMaker AI domain. You can find this at the top right of any Amazon console page.aws-region
-
is the name of your Amazon Elastic Container Registry repository, that you define here. To view your Amazon ECR repositories, see the Amazon ECR consoleecr-repository-name
.
-
-
Log in to Amazon ECR and sign in to Docker.
aws ecr get-login-password \ --region ${REGION} | \ docker login \ --username AWS \ --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
On a successful authentication, you will receive a succeeded log in message.
Important
If you receive an error, you may need to install or upgrade to the latest version of the Amazon CLI. For more information, see Installing the Amazon Command Line Interface in the Amazon Command Line Interface User Guide.
-
Tag the image in a format compatible with Amazon ECR, to push to your repository.
docker tag \ ${LOCAL_IMAGE_NAME} \ ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${ECR_REPO_NAME}
-
Create an Amazon ECR repository using the Amazon CLI. To create the repository using the Amazon ECR console, see Creating an Amazon ECR private repository to store images.
aws ecr create-repository \ --region ${REGION} \ --repository-name ${ECR_REPO_NAME}
-
Push the image to your Amazon ECR repository. You can also tag the Docker image.
docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${ECR_REPO_NAME}
Once the image has been successfully added to your Amazon ECR repository, you can view it in
the Amazon ECR console