SAE ejecuta las aplicaciones como imágenes de contenedor. Si no cuenta con una imagen existente, siga los pasos a continuación para compilar una y enviarla a un repositorio de imágenes.
Requisitos previos
Antes de empezar, asegúrese de contar con lo siguiente:
Docker instalado y en ejecución. Ejecute
docker --versionpara comprobarlo.Un directorio de proyecto que contenga un artefacto compilado (JAR, archivos PHP, aplicación de Python o recursos estáticos).
Compilar y enviar una imagen de contenedor
Paso 1: Crear un Dockerfile
Vaya al directorio de su proyecto y cree un archivo llamado Dockerfile. Los siguientes ejemplos abarcan Java, PHP, Python y Node.js.
Java
Estructura de directorios:
.
├── Dockerfile
├── target
│ └── my-app.jar # Compiled JAR — only this and the Dockerfile are needed to build the image
├── src
│ └── ...
└── pom.xml
Dockerfile:
# Use an Alpine-based OpenJDK 8 image — Alpine keeps the image size small
FROM openjdk:8-jdk-alpine
# Copy the compiled JAR into the image root
COPY ./target/my-app.jar /
# Start the application when the container launches
ENTRYPOINT ["java", "-jar", "/my-app.jar"]
Consejo: Para aplicaciones Java en producción, considere usar compilaciones por etapas para mantener la imagen final ligera: compile en una etapa con JDK y luego copie solo el JAR a una etapa de ejecución que use exclusivamente JRE.
PHP
Estructura de directorios:
.
├── Dockerfile
├── entrypoint.sh # Startup script that launches PHP-FPM and Nginx
├── php
│ ├── index.php
│ └── phpinfo.php
├── nginx
│ ├── nginx.conf
│ ├── conf.d
│ │ └── default.conf
│ ├── fastcgi_params
│ └── ...
Dockerfile:
# Use an Alpine-based PHP-FPM image — PHP-FPM handles PHP processing, Nginx handles HTTP
FROM php:8.2-fpm-alpine
# Install Nginx into the same container to serve as the HTTP frontend
RUN apk add --no-cache nginx
# Copy Nginx configuration files
COPY nginx/ /etc/nginx/
# Copy PHP application files to the web root
COPY php/ /var/www/html/
# Copy and enable the startup script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Script de inicio (entrypoint.sh):
#!/bin/sh
# Start PHP-FPM in the background so Nginx can forward PHP requests to it
php-fpm -D
# Verify PHP-FPM started successfully before launching Nginx
if ! pgrep "php-fpm" >/dev/null
then
echo "PHP-FPM failed to start!"
exit 1
fi
# Run Nginx in the foreground — the container stays alive as long as Nginx runs
nginx -g "daemon off;"
Python
Estructura de directorios:
.
├── Dockerfile
├── entrypoint.sh
├── my_app
│ ├── requirements.txt
│ ├── main.py
│ └── my_package
│ ├── __init__.py
│ └── ...
Dockerfile:
# Use the official Python 3.9 image
FROM python:3.9
# Copy the application code into the image
RUN mkdir -p /my_app
COPY my_app/ /my_app/
# Install Python dependencies declared in requirements.txt
RUN pip install -r /my_app/requirements.txt
# Copy and enable the startup script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Script de inicio (entrypoint.sh):
#!/bin/sh
# Start the Flask app using Gunicorn with 3 worker processes, listening on port 8080
# Flask and Gunicorn are declared as dependencies in requirements.txt
gunicorn -w 3 -b 0.0.0.0:8080 my_app.main:app
Node.js
Este ejemplo corresponde a un proyecto de frontend cuyos recursos estáticos se sirven mediante Nginx.
Estructura de directorios:
.
├── Dockerfile
├── nginx-conf
│ ├── nginx.conf
│ ├── conf.d
│ │ └── default.conf
│ └── ...
├── package.json
├── src
│ └── ...
└── dist # Static files generated by your build step (e.g., npm run build)
├── index.html
└── static
└── ...
Dockerfile:
# Use Nginx 1.22 as the base image — it provides the startup command, so no ENTRYPOINT is needed
FROM nginx:1.22
# Copy the built static files to Nginx's default serving directory
COPY ./dist /usr/share/nginx/html/
# Copy your Nginx configuration files
COPY ./nginx-conf /etc/nginx/
Ejecute el paso de compilación (por ejemplo,npm run build) antes de ejecutardocker build. El directoriodistdebe existir en la raíz de su proyecto.
Paso 2: Compilar la imagen
docker build -t <image-name>:<image-tag> .
Ejemplo:
docker build -t my-app:1.0 .
No incluya secretos (claves de API, contraseñas o credenciales de acceso) directamente en su Dockerfile mediante ENV o ARG. Los valores pasados como argumentos de compilación pueden quedar expuestos en el historial de la imagen.
Paso 3: Enviar la imagen a un repositorio
Envíe la imagen a un repositorio al que SAE pueda acceder. Si no dispone de uno, utilice Alibaba Cloud Container Registry (ACR):
Paso 4: Desplegar en SAE
Elija el método de despliegue según la ubicación donde se almacene su imagen:
|
Escenario |
Método |
|
La imagen se encuentra en ACR bajo la misma cuenta de Alibaba Cloud |
|
|
La imagen se encuentra en ACR bajo una cuenta distinta de Alibaba Cloud |
|
|
La imagen se encuentra en un repositorio externo a ACR |