Deploy Docker Image with AWS ECS (Part 1)
One of the things I have been working on is to help our developers to containerize their applications and deploy them to AWS ECS.
In this post, I will walk through the steps to upload a Docker image to AWS ECR (Elastic Container Repository).
As the first step, we need to provision the ECR with CloudFormation template.
Below is a simple CFN template written in YAML.
AWSTemplateFormatVersion: "2010-09-09"
Description: \>
Play stack
Parameters:
RepoName:
Default: tomrepo
Description: ECR Repoistory Name
Type: String \# required
ConstraintDescription: must be a name
Resources:
myrepo:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !Ref RepoName
mycluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: tomecscluster
Outputs:
AWSTemplateFormatVersion:
Description: Tempalte version
Value: "1.0"
Deploy the CFN template with AWS CLI command below.
aws cloudformation create-stack --stack-name createecs --template-body file://mytemplate.yaml --parameters ParameterKey=RepoName,ParameterValue=webfront
After the template is successfully deployed, go to ECS in AWS Console and confirm the repository and cluster is created.
Next we need to build the Docker image locally to be used for this deployment. In this example I use my local Docker engine installed on my Win 10 workstation. You can learn more about Docker for Windows here.
Once Docker is installed and running, you should see a little docker icon in the task tray. Make sure it is switched to Linux Containers.
Open CMD and create a folder to host the docker image.
Create a file named as “Dockfile” in the folder. Yes, it does not have an extension in the file name.
Copy the code below to the file. The Dockerfile basically uses a Ubuntu image and installed Apache web service on it along with a simple webpage named index.html.
FROM ubuntu:12.04
# Install dependencies
RUN apt-get update -y
RUN apt-get install -y apache2
# Install apache and write hello world message
RUN echo "Hello World!" > /var/www/index.html
# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE\_RUN\_USER www-data
ENV APACHE\_RUN\_GROUP www-data
ENV APACHE\_LOG\_DIR /var/log/apache2
EXPOSE 80
CMD \["/usr/sbin/apache2", "-D", "FOREGROUND"\]
Save the file and we can now build the image.
docker build -t webserver .
Before we upload the image to ECR, we need to test it out locally first.
Before you run the image (create the container), make sure port 80 on your local machine is not used.
docker run -p 80:80 webserver
Now, test access to http://localhost, you should see the “Hello World” page.
The image is now ready to be pushed to AWS ECR.
First, we need to retrieve the login information to authenticate our local Docker client with ECR. Run the command below and COPY the output. Change the region parameter if your ECR is in a different region.
aws ecr get-login --no-include-email --region ap-southeast-1
The output of the command should look like something below.
Copy the whole output and paste it into command line. Press enter to execute it.
You should get output like below, indicating your Docker client is now authenticated with AWS ECR.
Tag the image you got and make it ready for the push. If you run docker images, you should see a new image with aws tag
You can now push the image.
docker push 1234567891011.dkr.ecr.ap-southeast-1.amazonaws.com/webfront:latest
It may take sometime for the image to be uploaded. But eventually you should see something like below.
In AW Console, under the ECR “webfront” repository, you should now see the image tagged as latest.