Deploying a Containerized Flask Application with AWS ECS and Docker
Jun 08, 2023 • 10 Minute Read
- Software Development
Containers are popular these days, with good reason. Let’s take fifteen minutes and find out why by deploying our own application using Docker, AWS, and Flask (a Python microframework used for building web applications). Note: Some of this post will assume you’re using a Linux/Mac system.Step 1 - PrerequisitesLet’s make sure we have everything we need to make this work:
- An AWS Account.
- The AWS CLI Installed and Configured (with enough permissions to take all the actions we need to take).
- Docker (Docker commands may require sudo, or you can add your current user to the docker group).
- The code for this project. You can run
git clone https://github.com/linuxacademy/cda-2018-flask-app.git && cd cda-2018-flask-app && git checkout ecs-master
- (Make sure you change to the other branch as this contains code specific to ECS)
sudo docker image lsto review the current Docker setup. If this is a new Docker setup, this command probably won’t show much. If Docker has been installed for a while though, there may be some images hanging around already. Next, let’s look at the Dockerfile in the repository with
cat Dockerfile. Our Dockerfile is pretty straightforward. Let’s go over it line by line:
FROM python:3.5-slim- We’re starting with a Python 3.5 image as the base of our Docker image. This means we’re getting Python3.5 and it’s package manager, pip, for free before we make any customizations.
MAINTAINERfernando@linuxacademy.com - This line declares who the author of the image is. You can change it to your email. This format is no longer used, so you may also want change it to the non-deprecated LABEL instruction instead, like this:
USER root- This is telling us which user should be running the image and any commands, like what are in the CMD or RUN sections, later in the Dockerfile.
WORKDIR /app- This sets the working directory for some other commands, like what are in the CMD or RUN sections, that run later on in the Dockerfile.
ADD . /app- This copies all the files and directories from the current directory (specified with the .) to the
RUN pip install --trusted-host pypi.python.org -r requirements.txt- This installs the requirements for our application using pip and the requirements.txt file provided.
EXPOSE 80- This tells Docker which ports to listen on at runtime.
ENV NAME World- This sets an environment variable called NAME to the value of World.
CMD ["python", "app.py"]- This sets the command to be executed when running the image
python app.py(the command required to run our Flask application)
sudo docker build -t cda-flask-app .Then let’s test our image. First, run
curl localhost:80just to make sure you’re not serving up anything locally. You didn’t get anything? Cool, that’s what we wanted.Next, run this command to run your Docker image locally
sudo docker run -p 80:80 cda-flask-app. Now either run this command again:
curl localhost:80or open up a web browser and enter
localhostinto your web browser’s URL bar.You should now see something like this:Yay! We have our site running locally in our new container image. Step 3 - Send our image up to AWSNow we’re going to send our newly created and tested image up to AWS.Start by creating a repository in the Elastic Container Registry:
aws ecr create-repository --repository-name cda-penguin-app1123Then run this to get a command you can use for logging in to the ECR repository you’ve just created:
aws ecr get-login --region us-east-1 --no-include-emailThen copy and paste the output of that command back into the terminal and run it. This will actually authenticate you with the Elastic Container Repository so you can push your Docker image into it. Now that we’re logged into ECR, we can put our image into ECR. Let’s do this by:
- Making sure we have our image locally by running
docker image ls
- Tagging the image by running
docker tag cda-flask-app:latest ACCT_ID.dkr.ecr.us-east-1.amazonaws.com/(where ACCT_ID is your own AWS account ID)
- Pushing the image up to AWS ECR with
docker push ACCT_ID.dkr.ecr.us-east-1.amazonaws.com/hello-world(again, using your own AWS account ID in place of ACCT_ID)
- Customize the Flask application so it does things other than displaying an adorable penguin website
- Configure many of the other settings available when working with ECS, depending on the needs of your own applications
- Deploy lots of cool containers with different requirements, other programming languages and much more