Deploy model services with custom images

更新时间:
复制 MD 格式

When the official EAS images don't meet your requirements, use a custom image to package a complete runtime environment.

Image registry planning

EAS pulls images from an image registry when deploying services. Store your images in Container Registry ACR and use the VPC address for better performance and security.

Important

Pulling images over the public internet poses security risks and is significantly slower. Don't use public internet access for image pulls.

ACR Personal Edition

  • Free to use, but only available in a single region. Cross-region image pulls require a public internet address.

  • Use ACR Personal Edition for testing only, and make sure it's in the same region as your EAS service.

ACR Enterprise Edition

  • Provides stronger security, performance, and global synchronization. EAS services can pull images securely at high speed over the private network, within the same region or across regions.

  • Use ACR Enterprise Edition in production environments.

Self-hosted image registry

  • If you use a self-hosted registry such as Harbor in an Alibaba Cloud VPC, the registry address is only accessible within that VPC. Similar to ACR Enterprise Edition, configure the VPC for your EAS service so that the registry and EAS service are in the same VPC.

Image registry authentication

For private registries that require authentication, provide credentials when deploying the service.

In the JSON configuration file, use the dockerAuth parameter to specify authentication credentials. The value is the Base64-encoded form of username:password.

For example, for abcd:abcde12345, run echo -n "abcd:abcde12345" | base64 to get YWJjZDphYmNkZTEy****, which is the value to use for dockerAuth.

{"dockerAuth": "YWJjZDphYmNkZTEy****"}
Note

When using an ACR image registry under the same Alibaba Cloud account and in the same region, EAS you can pull images without credentials — no username or password needed.

Quick start: build and deploy a custom image

This section shows how to build a Gunicorn and Flask web service image on an Alibaba Cloud ECS instance, push it to ACR, and deploy it as an EAS online service.

Step 1: Set up your environment

Before you begin, make sure the following resources are ready:

  • Virtual Private Cloud (VPC): EAS services, ECS instances, and ACR communicate over a VPC for stable, secure image push and pull operations.

  • Container Registry (ACR): Stores and manages your custom images. See Push and pull images to set up an ACR Enterprise Edition instance with the required namespaces and repositories.

  • Development environment: Create an ECS instance with the following configuration to build your image:

    • Instance type: ecs.u1-c1m2.large

    • Image: Alibaba Cloud Linux 3.2104 LTS 64-bit

    • Extension: Docker Community Edition

    • Network: Add the VPC to the ACR Enterprise Edition instance's access control settings; otherwise, image pushes to ACR will fail. For details, see Configure VPC access control for ACR.

    Note

    You can also use a local or other development environment.

    • Local development environment: Install and run Docker to build images.

    • DSW development environment: In the DSW instance's Actions column, click Create Image. The system builds a Docker image and saves it to ACR. When deploying, select Custom Image to choose the image from a drop-down list. For details, see Create a DSW instance image.

Step 2: Prepare your application files

Create a project folder (for example, my-app) with the following files:

  1. requirements.txt (application dependencies)

    flask
    gunicorn
  2. app.py (web application code)

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/hello/model')
    def hello_world():
        # Add your model inference or business logic here
        return 'Hello World from Gunicorn!'
    
    # Note: No app.run() needed — Gunicorn starts the app
  1. Dockerfile (image build instructions)

    # 1. Use a lightweight official Python image as the base
    FROM python:3.9-slim
    
    # 2. Set the working directory
    WORKDIR /app
    
    # 3. Copy and install dependencies first to leverage Docker's layer cache
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
    
    # 4. Copy application code
    COPY app.py .
    
    # 5. Declare the service port
    EXPOSE 8000
    
    # 6. Define the default container startup command (can be overridden by the EAS "Run command" setting)
    CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:app"]

Step 3: Build and push the image

  1. Open a terminal and navigate to the project folder (my-app).

  2. Log in to the ACR instance. Replace the placeholders in the command with actual values.

    # Example: docker login --username=your_user my-registry.cn-hangzhou.aliyuncs.com
    docker login --username=<username> <ACR registry domain>
  3. Build the image with the full tag.

    # Example: docker build -t my-registry.cn-hangzhou.aliyuncs.com/my-namespace/flask-app:v1 .
    docker build -t <ACR registry domain>/<namespace>/<repository name>:<tag> .
  4. (Recommended) Verify the image locally before pushing. Run the following command to confirm the image starts correctly before pushing to ACR, avoiding broken images in your registry.

    # Replace <port> with the port declared in your Dockerfile's EXPOSE instruction (8000 in this example)
    docker run -p <port>:<port> <ACR registry domain>/<namespace>/<repository name>:<tag>

    If the application responds normally, the image is ready to push.

  5. Push the image to ACR.

    # Example: docker push registry.cn-hangzhou.aliyuncs.com/my-namespace/flask-app:v1
    docker push <ACR registry domain>/<namespace>/<repository name>:<tag>

Step 4: Deploy the service

  1. 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).

  2. Click Deploy Service, select Custom Model Deployment > Custom Deployment.

  3. Configure the following key parameters:

    • Deployment Method: Select Image-based Deployment.

    • Image Configuration: Select Image Address, then enter the full image address from the previous step.

      Because this example uses ACR under the same Alibaba Cloud account, EAS can pull the image without credentials.
    • Command to Run: gunicorn -w 4 -b 0.0.0.0:8000 app:app

      How it works: The run command entered in the EAS console overrides the CMD instruction in the Dockerfile. Define the startup command here to make it easier to debug and update later.
    • Port Number: 8000.

    • Deployment: CPU resources are sufficient for this example. Under Public Resources, select ecs.c6.large.

    • VPC: Select a VPC, vSwitch, and security group.

      Make sure the VPC you select here has been added to the ACR instance's access control settings. If not, image pulls will fail due to network connectivity issues (a common error is ImagePullBackOff).
  4. Click Deploy. When the service status changes to Running, the deployment is complete.

Note

Updating the image address or service configuration: The JSON configuration on the service details page is read-only. To make changes, use the Update entry in the service list.

Step 5: Test the service

After the service is deployed, get the endpoint address and token, then 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 the response is Hello World from Gunicorn!, the service is deployed and accessible.

For more information about calling services, see Service invocation methods.

Key concepts and limitations

  • Network: EAS services access private network resources through VPC addresses, which typically requires VPC configuration. To access public internet resources — such as installing dependencies from public sources with pip, calling external APIs, or pulling images over the internet — 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.
  • Ports:

    • The EAS service engine reserves ports 8080 and 9090. Don't use these ports in your application, or the service will fail to start due to port conflicts.

    • When a service includes multiple containers, each container must listen on a different port. For example, if the main container uses port 8000, the sidecar container can't also use port 8000, or the service will fail to start due to port conflicts.

  • CUDA version: The CUDA version for an online service is determined by the instance type and base image you select, and can't be changed while the service is running. To switch CUDA versions, redeploy the service and select an image or instance type that supports the target CUDA version.

  • Sidecar injection: For authentication, authorization, and monitoring, EAS injects a proxy container into the service instance in sidecar mode. This proxy is transparent to your application, but consumes a small amount of resources. It routes external requests securely to the service port you configured.

  • API protocol support: Image-based deployments support HTTP, WebSocket, and gRPC (HTTP/2) for API services running inside the container.

Apply in production

  • Separate image from model: Keep code and model files separate. Package code in the image, store model files in Object Storage (OSS) or Network-Attached Storage (NAS), and mount them into the container at deploy time using the Storage mounts feature on the EAS deployment page. This reduces image pull time during service updates and scale-out operations.

  • Pull images over the private network: Configure a VPC for your service so it can pull images from ACR using the VPC address, ensuring both security and performance.

  • Pre-install dependencies to reduce cold start time: If your service needs to download Python packages or large model files on every restart, cold start times increase significantly. Build the Python environment, packages (such as PyTorch and Transformers), and model files directly into the image so the container can serve requests immediately on startup without downloading anything at runtime.

  • Register images as AI assets: If a custom image applies to multiple scenarios, use the PAI AI Assets image feature to register and manage it centrally.

  • Configure health checks: Set up health checks for your service so EAS automatically restarts unhealthy instances and maintains service stability. For details, see Configure health checks.

  • Enable auto scaling: When traffic load fluctuates significantly, enable Horizontal auto scaling to handle traffic changes elastically and cost-effectively.

FAQ

Q: Image pull fails with ImagePullBackOff

Troubleshoot in this order:

  1. Image address: Verify that the image address, namespace, and tag are correct, and that the image was successfully pushed to the registry.

  2. System disk space: If you see the error no space left on device, expand the system disk.

  3. Network connectivity:

  4. ACR access control: Based on the type of image address you're using, check whether Configure internet access control or Configure VPC access control is configured for ACR.

  5. Registry credentials: If you're using a private registry that requires authentication (not ACR under your own account), check that the credentials in the image configuration are correct.

Q: How do I use an ACR image across regions?

By default, EAS can pull images from ACR repositories in the same region over the VPC private network. If the ACR instance and EAS service are in different regions, private network access isn't available. Use one of the following approaches:

  • Option 1 (recommended): Create a new ACR instance in the same region as your EAS service and push the image to that regional repository before deploying. This avoids cross-region issues.

  • Option 2: Connect the VPCs across both regions using Cloud Enterprise Network (CEN) to enable cross-region private network pulls. For details, see Access an Enterprise Edition instance across regions or from a data center.

  • Option 3 (testing only): Pull images over a public internet address. Make sure public access is enabled for ACR and that the EAS service VPC has a NAT gateway for internet access. This option is slow and poses security risks — use it for testing only.

Q: What does "no match for platform in manifest" mean when deploying a custom image?

This error means the image architecture (for example, arm64) doesn't match the EAS instance's runtime environment (typically amd64/x86_64). This commonly occurs when building an image on an Apple Silicon Mac (arm64) and deploying it to an EAS instance (amd64). To resolve this:

  1. Confirm the CPU architecture of the target EAS instance (most EAS instances use amd64).

  2. 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 registry domain>/<namespace>/<repository name>:<tag> .
  3. Push the new image to ACR and redeploy the service.

Q: Can ACK connect to the EAS dedicated image registry (PAI official images)?

No. The EAS dedicated image registry doesn't support direct ACK connections.

For more issues, see EAS FAQ.