Pushing docker image into AWS ECR

Faraz Amjad
7 min readSep 18, 2022

--

In this blog we will cover the following major concepts:

  1. What is Docker?
  2. Configuring Docker File and building Image.
  3. Running your Container.
  4. What is Amazon ECR?
  5. Creating Repo in ECR.
  6. Pushing your code to ECR.

What is Docker?

In simple words we can say, docker is a platform which is used for creating and managing containers. But wait what does containers mean? So just like real life containers where you store different packages, a docker container contains the package of code and all dependences required to run that code. For example if you have NodeJS application with a container build with docker, you could have your application source code, NodeJS runtime and any other tools that are required to run the code in that container. If your application was build in windows it won’t have any issue running on Linux or any other OS. To get a better idea why docker was created refer to the most popular docker meme.

image source: imgflip

Note: You can clone the code I am using from GitHub link or feel free to use your own.

https://github.com/farazamjad/Flask_HelloWorld

Configuring Docker File

This is what my docker file looks like, you can also copy paste this to your editor and make changes according to the technology you have used.

FROM python:3.9.5-busterWORKDIR /codeCopy ..RUN pip install flaskEXPOSE 3001CMD python main.py

Now lets go through the code and understand each instruction:

FROM: This instruction is followed by the operating system image which is publicly available on docker hub and the version we want.

If you want to know more about docker hub click

WORKDIR: The WORKDIR instruction sets working directory for all the other instruction that follow it in docker file.

Copy: The COPY instruction copies all the files/directories into docker image. In our case the first dot tells docker to copy every file, folder and sub folder of the project into image. The second dot is the path inside of the image where the files should be stored.

RUN: The RUN will execute the given instruction. In our case it installs flask using pip.

EXOPOSE: The EXPOSE instruction tells the specified port number where the container can listen app at runtime.

CMD: The CMD instruction tells container how to start the application.

Building docker Image

We execute the docker file code to build an image. The command to build image is:

docker build [options] [imagename]:[tag][path]

The build command takes couple of arguments. The first one is option and there are different parameters you can use but the one I am going to use is -t which is used to give name tag to image for easy access. The second argument tells the location of docker file. In our case the command to build image is:

docker build -t flaskapp .

DOT tells that our docker file is in the same directory as our code. You can follow the link to know more about available argument in docker build: https://docs.docker.com/engine/reference/commandline/build/

When you run the above command, you will see that steps are being executed in the same order as they are written in the docker file.

Output after running build command

To confirm that you have successfully built the image use the following command:

docker images
Output after running docker images command

RUNNING YOUR CONTAINER:

Before running the docker RUN command you can check if there any any container running using the command:

docker container ls -a

As we have successfully build our image we will run our container with the following command:

docker run -it -p 3001:3001 flaskapp

NOTE: When running docker container you must provide an IP if not you might face error.

After running the command our container is active and running.

Docker Run output

Let’s check out if our browser is showing the “hello world” output on the link http://127.0.0.1:3001 .

Application Running

Great ! So far, we know what is docker and why we use it, we have created docker image and build it. We also tested the image and confirmed that the application is running on container. The next important step to to push this image into container registry so others can use it. We will be using amazon elastic container service but other services such as docker hub can also be used.

What is AWS ECR? Benefits of AWS ECR?

AWS ECR is one of the amazon web services which the users can use to push their docker containers into AWS managed container registry. AWS ECR provides increased security. The developers can use AWS IAM to create policies that control permission and manage access to docker images. Moreover all images in ECR are transferred over HTTPS and images are also automatically encrypted for better security. AWS ECR is also fully managed which means no extra software are required to push images or manage infrastructure. And AWS ECR provides high availability meaning the container images are easily accessible, available and deployable for all users. Want to know more about AWS ECR click this link.

Creating repository in ECR

Follow the following steps to create repository on ECR:

  • Open https://aws.amazon.com/ .
  • Login to Amazon EC2 Console.
  • Search for ECR services.
  • Click on Create Repository.
  • Enter the name of repository of your choice.
  • Keep rest of the setting as it is and click on Create Repository button.
Flaskapp Repository

Now we have the repository the next step is to push the docker image into the repository. To push the code we need to use AWS CLI. If you don’t have it just run the following command:

msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

You can access the CLI through cmd. Before signing into your account through CLI you first need to give access to the registry policy. To do so follow the steps:

  • Search for IAM.
  • On the side bar select users.
  • Click the user you want to give access.
  • On permission tab select add permissions.
  • From three options select attach existing policy directly.
  • Search for AmazonEC2ContainerRegistryFullAccess select it.

Now on CMD run the command aws configure. You need to provide Access key and Access Secret. You can find both keys from IAM, then going into users ,select the user and on top select the security credentials tab.

Policies for selected User

Now for the last step go to your repository on AWS console and on top right corner you have to click view push commands. A window will appear where there are total of four commands. You need to run these one by one on CMD for pushing the image into repository.

View Push commands

If you followed the steps correctly and there is no error you will the following output on terminal:

Docker Push

Finally you can see the uploaded image in the AWS console. Go to ECR, click on repositories, and open ECR repo we uploaded.

Hurray! We have made it 🎉

Summary

In simple words we discussed what is dockers and why we use them. Also how to create docker file and then build that file into an image. We also got a basic idea what AWS ECR is and we saw how to push your docker image into AWS repo. You also might have got an idea how important these two technologies are to build large scale apps and having some idea about using them will come handy in future. I hope you have learned something new from this blog and follow me for more content.

Resources

--

--