O SAE executa aplicações como imagens de contêiner. Caso você não possua uma imagem existente, siga as etapas abaixo para criar uma e enviá-la a um repositório de imagens.
Pré-requisitos
Antes de começar, certifique-se de que você tem:
O Docker instalado e em execução. Execute
docker --versionpara verificar.Um diretório de projeto com um artefato compilado (JAR, arquivos PHP, aplicação Python ou ativos estáticos).
Criar e enviar uma imagem de contêiner
Etapa 1: Criar um Dockerfile
Acesse o diretório do seu projeto e crie um arquivo chamado Dockerfile. Os exemplos abaixo abrangem Java, PHP, Python e Node.js.
Java
Estrutura de diretórios:
.
├── 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"]
Dica: Para aplicações Java em produção, considere usar builds multiestágio para manter a imagem final enxuta: compile em um estágio JDK e copie apenas o JAR para um estágio de runtime com JRE.
PHP
Estrutura de diretórios:
.
├── 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 inicialização (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
Estrutura de diretórios:
.
├── 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 inicialização (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 exemplo aborda um projeto frontend cujos ativos estáticos são servidos pelo Nginx.
Estrutura de diretórios:
.
├── 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/
Execute a etapa de build (por exemplo,npm run build) antes de executar odocker build. O diretóriodistdeve existir na raiz do seu projeto.
Etapa 2: Criar a imagem
docker build -t <image-name>:<image-tag> .
Exemplo:
docker build -t my-app:1.0 .
Não codifique segredos (chaves de API, senhas ou credenciais de acesso) diretamente no Dockerfile usando ENV ou ARG. Valores passados por argumentos de build podem ficar expostos no histórico da imagem.
Etapa 3: Enviar a imagem para um repositório
Envie a imagem para um repositório acessível pelo SAE. Se você ainda não tiver um, utilize o Alibaba Cloud Container Registry (ACR):
Etapa 4: Implantar no SAE
Escolha o método de implantação conforme o local onde sua imagem está armazenada:
|
Cenário |
Método |
|
Imagem no ACR sob a mesma conta Alibaba Cloud |
|
|
Imagem no ACR sob uma conta Alibaba Cloud diferente |
|
|
Imagem em um repositório fora do ACR |