# 從 Docker 容器部署 Elastic Beanstalk 應用程序 [TOC] ###### tags: `aws` `eb` `docker` --- ## 先決條件 安裝… - AWS CLI - EB CLI - Docker ## 容器化 Elastic Beanstalk 應用程序 ``` ├─ .elasticbeanstalk/ │ └─ … ├─ .platform/ │ └─ … ├─ Dockerfile ├─ Dockerrun.aws.json ├─ pom.xml └─ src/ └─ … ``` The most current platform branches are based on the **Amazon Linux 2** operating system and offer long-term support: - **Docker** Running on 64bit Amazon Linux 2 - **ECS** Running on 64bit Amazon Linux 2 Supported platform branches for each Docker platform: | Platform Version | Solution Stack Name | AMI | Docker | Docker Compose | Proxy Server | | ----------------- | ------------------------------------------- | ------------ | ---------- | -------------- | ------------ | | Docker AL2 3.4.17 | 64bit Amazon Linux 2 v3.4.17 running Docker | 2.0.20220606 | 20.10.13-2 | 1.29.2 | nginx 1.20.0 | | ECS AL2 3.1.3 | 64bit Amazon Linux 2 v3.1.3 running ECS | 2.0.20220606 | | | | # Comparison | | Docker running on AL2 | ECS running on AL2 | | -------------------- | --------------------- | ------------------ | | single-container | ✅ | | | multi-container | ✅ | ✅ | | `Dockerrun.aws.json` | | ✅ | | | | | | | | | | | | | | | | | | | | | # Docker running on Amazon Linux 2 ``` ├─ Dockerfile └─ src/ └─ ... ``` ## `Dockerfile` ``` FROM python:3.6 COPY . /app WORKDIR /app RUN pip install Flask==1.0.2 EXPOSE 5000 CMD ["python", "application.py"] ``` :::info If you're deploying a remote **Docker** image, you don't need to include a **`Dockerfile`**. ::: ### To build a Docker image ```shell % docker build -t {docker-id}/{beanstalk-flask}:latest . ``` ### To push it to ECR ```shell % docker push {docker-id}/{beanstalk-flask}:latest ``` ## If using **Docker Compose**, utilize a **`docker-compose.yml`**. ```yaml version: '3.8' services: beanstalk-flask: image: "username/beanstalk-flask" ports: - "80:5000" ``` ## If ***not*** using **Docker Compose**, utilize a **`Dockerrun.aws.json`**. ```json { "AWSEBDockerrunVersion": "1", "Image": { "Name": "username/beanstalk-flask", "Update": "true" }, "Ports": [ { "ContainerPort": "5000" } ] } ``` ## ECS running on 64bit Amazon Linux 2