When the official images provided by EAS do not meet your business or environment requirements, you can package a complete runtime environment in a custom image for flexible and reliable model service deployment.
Quick start: build and deploy a custom image
Build a web service image based on Gunicorn and Flask on an Alibaba Cloud ECS instance, push it to an ACR repository over a private network, and deploy it as an EAS online service.
Step 1: Prepare the environment
Make sure the following resources are available in the same region (such as China (Hangzhou)) before you begin:
1.1 VPC
Prepare a VPC such as VPC_HZ. To create one, see VPCs and vSwitches.
Both the ECS instance and the EAS service use this VPC to ensure stable and secure image push and pull.
1.2 Container Registry (ACR)
Create an ACR Enterprise Edition instance to store and manage custom images. For more information about image repositories, see Choose an image registry.
Key steps are as follows. For details, see Push and pull images:
Log on to the Container Registry console.
Create an Enterprise Edition instance. This topic uses the China (Hangzhou) region and the Basic edition. Keep the default settings for other parameters.
Create a namespace such as
pai-demo.Create an image repository such as
flask-app. Select Local Repository as the code source.Configure VPC access control.
In the left-side navigation pane of the Enterprise Edition instance management page, choose .
On the VPC tab, click Add VPC . Select
VPC_HZas the VPC.
1.3 Development environment (ECS)
Create an ECS instance with the following specifications to build the image.
Instance type:
ecs.u1-c1m2.largeImage: Alibaba Cloud Linux 3.2104 LTS 64-bit
Extension: Docker Community Edition
Network: Select
VPC_HZ(the selected VPC must be added to the access control list of the ACR Enterprise Edition instance; otherwise, images cannot be pushed to the ACR repository).
You can also use a local or other development environment.
Local development environment: Install and run Docker to build images locally.
DSW development environment: In the DSW instance list, click Create Image in the Actions column. The system builds a Docker image and saves it to ACR. When you deploy the service, select Custom Image to choose from a drop-down list. For details, see Create a DSW instance image.
Step 2: Prepare the project files
Log in to the ECS instance, create a project folder such as my-app, and add the following three files:
requirements.txt (application dependencies)
flask gunicornapp.py (web application code)
from flask import Flask app = Flask(__name__) @app.route('/hello/model') def hello_world(): # Integrate your model inference or other business logic here return 'Hello World from Gunicorn!' # Note: app.run() is not needed. Gunicorn starts the application.Dockerfile (image build instructions)
# 1. Use the lightweight official Python image as the base FROM python:3.9-slim # 2. Set the working directory WORKDIR /app # 3. Copy the dependencies file and install. This layer is cached to speed up subsequent builds. COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ # 4. Copy the application code COPY app.py . # 5. Declare the service port EXPOSE 8000 # 6. Define the default container startup command (can be overridden by the Run Command in the EAS console) CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:app"]
Step 3: Build and push the image
Open a terminal and go to the project folder (
my-app).Log in to your ACR instance. Replace the placeholders in the command with your actual values.
NoteOn the details page of the ACR repository, you can find login, pull, and push command examples. The examples use public endpoints. Replace them with private endpoints.
# Example: docker login --username=your_user my-registry-vpc.cn-hangzhou.aliyuncs.com docker login --username=<username> <ACR private endpoint>Build the Docker image and tag it with the full repository path.
# Example: docker build -t my-registry-vpc.cn-hangzhou.aliyuncs.com/pai-demo/flask-app:v1 . # The full image address is <ACR private endpoint>/<namespace>/<repository name>:<tag> docker build -t <ACR private endpoint>/<namespace>/<repository name>:<tag> .(Recommended) Verify the image locally before pushing. Run the following command to make sure the image starts correctly before you push it to the repository.
# Replace <port> with the EXPOSE port in your Dockerfile (8000 in this example) docker run -p <port>:<port> <ACR private endpoint>/<namespace>/<repository name>:<tag>If the application responds as expected, the image is ready to push.
Push the image to ACR.
# Example: docker push my-registry-vpc.cn-hangzhou.aliyuncs.com/pai-demo/flask-app:v1 docker push <ACR endpoint>/<namespace>/<repository name>:<tag>
Step 4: Deploy the service
-
Log on to the PAI console. Select a region on the top of the page. Then, select the desired workspace and click Elastic Algorithm Service (EAS).
Click Deploy Service and choose .
Configure the following key parameters:
Deployment Method: Select Image-based Deployment.
Image Configuration: Select Image Address and enter the full image address from the previous step:
my-registry-vpc.cn-hangzhou.aliyuncs.com/pai-demo/flask-app:v1.Because this example uses ACR under the same Alibaba Cloud account, EAS can pull the image without credentials. No username or password is required.
Command to Run:
gunicorn -w 4 -b 0.0.0.0:8000 app:appThe
Run Commandin the EAS console overrides theCMDinstruction in the Dockerfile. Enter the startup command here for easier debugging and modification later.Port Number: 8000.
Deployment: This example uses CPU resources. Select Public Resources and choose
ecs.c6.large.VPC: Select a VPC (
VPC_HZ), a vSwitch, and a security group.Make sure the VPC you select here has been added to the access control list of the ACR instance. Otherwise, image pull fails due to network connectivity issues. A common error is
ImagePullBackOff.
Click Deploy. The deployment succeeds when the service status changes to Running.
Update the image address or service configuration: The JSON configuration displayed on the service details page is read-only and cannot be edited directly. To modify it, use the Update entry in the service list.
Step 5: Test the service
After the service is deployed, obtain the endpoint and token and run the following command to test the Flask service:
# Replace <endpoint> and <token> with the actual service endpoint and token
curl <endpoint>/hello/model -H "Authorization: <token>"If Hello World from Gunicorn! is returned, the service is deployed and accessible.
For more information about service invocation, see Service invocation methods.
Key concepts and limitations
Network: EAS services access internal resources through VPC endpoints and typically require VPC configuration. To access resources on the Internet (for example, using
pipto install dependencies from public sources, calling external APIs, or pulling images over the Internet), you must also configure a NAT gateway for the VPC. For details, see EAS access to public and private resources.Cost note: NAT gateway is a paid service and incurs additional charges.
Port restrictions:
The EAS service engine uses ports
8080and9090. Your application must not listen on these ports. Otherwise, the service fails to start due to port conflicts.When a service has multiple containers, each container must listen on a different port. For example, if the main container uses port
8000, the sidecar container cannot also use port8000. Otherwise, the service fails to start due to port conflicts.
CUDA version: The CUDA version for an online service is determined by the selected instance type and base image, and cannot be changed at runtime. To switch CUDA versions, redeploy the service with an image or instance type that supports the target CUDA version.
Sidecar injection: EAS injects a proxy container into each service instance as a sidecar for authentication, authorization, and monitoring. The proxy is transparent but uses some resources and forwards external requests to your configured service port.
API protocol support: Image deployment supports HTTP, WebSocket, and gRPC (HTTP/2) protocols for API services within the image.
Choose an image registry
Image registry types
EAS pulls images from an image registry when deploying services. We recommend Alibaba Cloud Container Registry (ACR) for image storage. Use VPC endpoints to pull images for better performance and security.
Pulling images over the Internet is insecure and slow. Do not use public endpoints in production.
Registry type | Description |
ACR Personal Edition |
|
ACR Enterprise Edition |
|
Self-managed registry | If you use a self-managed registry such as Harbor within an Alibaba Cloud VPC, the registry address is only accessible within the VPC. Similar to ACR Enterprise Edition, configure a VPC for the EAS service so that the registry and EAS service are in the same VPC. |
Image registry authentication
For registries that require authentication, provide credentials when you deploy the service.
Specify the credentials in the JSON configuration file through the dockerAuth parameter. The value is the Base64 encoding of username:password.
For example, to encode abcd:abcde12345, run echo -n "abcd:abcde12345" | base64 to get YWJjZDphYmNkZTEy****. Use this as the dockerAuth value.
{"dockerAuth": "YWJjZDphYmNkZTEy****"}When you use an ACR repository in the same region under the same Alibaba Cloud account, EAS can pull images without credentials. No username or password is required.
Production recommendations
Separate images from models: Always keep code and model files separate. Package code in the image and store models in OSS or NAS. Use the Storage mounts feature to mount models into the container during EAS deployment. This reduces image pull time during service updates and scale-outs.
Pull images over the private network: For security and performance, configure a VPC for the service so it can pull images from ACR through a VPC endpoint.
Preinstall dependencies to optimize startup time: If the service downloads Python packages or large model files on every restart, cold start time increases significantly. Preinstall the Python environment, dependencies (such as PyTorch and Transformers), and model files in the image so the container can serve requests immediately after startup.
Register the image as an AI asset: If the custom image applies to multiple scenarios, use the image feature of PAI AI Assets to manage it as a reusable AI asset.
Configure health checks: Set up health checks for the service. EAS automatically restarts unhealthy instances to maintain service stability. For details, see Health checks.
Enable auto scaling: When your workload has significant traffic fluctuations, enable Horizontal auto scaling to handle traffic changes in an elastic and cost-effective way.
FAQ
Q: Image pull fails (ImagePullBackOff)
Check the following items:
Image address: Verify that the image address, namespace, and tag are correct, and that the image has been pushed to the repository.
System disk space: If you see the error
no space left on device, expand the system disk.Network connectivity:
Using a public endpoint: Make sure EAS is configured with a VPC that has Internet access, and enable public access for ACR. Cross-region pull over the Internet is slow and prone to failure.
Using a private endpoint: For cross-region scenarios, only ACR Enterprise Edition is supported. You must connect the VPCs. For details, see Access an ACR Enterprise Edition instance across regions or from an on-premises data center.
ACR access control: Based on the image address type you use, check whether ACR Configure internet access control or Configure VPC access control is configured.
Registry authentication: If you use a private registry that requires authentication (not ACR under your account), verify that the credentials in Image Configuration are correct.
Q: How do I use ACR images across regions?
EAS can pull images from ACR repositories in the same region through VPC private endpoints by default. If the ACR instance and EAS service are in different regions, direct private network access is not available. Use one of the following methods:
Method 1 (recommended): Create an ACR instance in the same region as the EAS service, push the image to the local repository, and then deploy. This eliminates cross-region issues entirely.
Method 2: Use CEN (Cloud Enterprise Network) to connect VPCs across regions for private network image pull. For details, see Access an Enterprise Edition instance across regions or from a data center.
Method 3 (testing only): Pull images over the Internet. Enable public access for ACR and configure a NAT gateway with Internet access for the EAS service. This method is slow and has security risks. Use it only for testing.
Q: Custom image deployment fails with "no match for platform in manifest"
This error means the image architecture (such as arm64) does not match the EAS instance runtime environment (typically amd64/x86_64). EAS cannot run this image on the selected instance type. This commonly happens when you build an image on an M-series Mac (arm64) and deploy it to an EAS (amd64) instance. To fix this:
Confirm the CPU architecture of the target EAS instance (most EAS instances use
amd64).Rebuild the image locally to match the EAS instance architecture:
# Build an amd64 image (compatible with most EAS instances) docker build --platform linux/amd64 -t <ACR endpoint>/<namespace>/<repository name>:<tag> .Push the new image to ACR and redeploy the service.
Q: Can ACK connect to the EAS dedicated image registry (PAI official images)?
The EAS dedicated image registry does not support direct ACK connections. ACK cannot pull images from the EAS dedicated image registry.
For more questions, see EAS FAQ.