Deploy Python Web application using Docker and Amazon EC2


Many of us usually think about the whole life cycle of an application (Web or Standalone), the deployment and testing phase comes with many tough thoughts as a developer. For example, a machine learning model is created, tested locally, and passes all test cases. Now the main part comes for deployment and testing to be done by the testing team. Many times, at least in my early days of Development/Technical Consulting, the tester used to be my biggest enemy though being one of the best friends otherwise. The first test case fails to say “It’s not working on my laptop, not sure why?”. Then after certain investigations, logistics comparisons, and many other iterations, finally the testing of proper application starts. One of the toughest times for all developers usually given by the testing team.

Docker is one of the most adopted technologies to overcome the issues of deployment and testing phase due to reasons of the compatible operating system or other dependencies.

This story assumes that the reader has prior knowledge of docker and Amazon EC2. A web application is created in Python and tested locally. Further is to discuss the steps to deploy the web application using docker and host it on Amazon EC2 Linux instance as follows:

1. Spin up the EC2 Linux instance: In AWS free eligible account, follow the below process to create new instances:

a. Click Launch instance from EC2 dashboard.

b. Select the Linux Image from the image list which is available for the free tier.

c. Choose the t2.micro from the instance type list.

d. Go with default configurations in instance creation wizards.

e. Create a new Security Group to open SSH/Port 22, HTTP/Port 5000, or for testing purposes open all “All Traffic” options to avoid an issue for accessing this EC2 Instance.

f. Wait for the EC2 instance to be in running status. Once up and running use SSH client or Putty to login to this EC2 Instance with the below command.

ssh -i “LBEC2KeyPair.pem” ec2-user@ec2–3–23–60–

2. Install Docker on the fresh instance: Once logged in EC2 instance, install docker using the below commands:

a. yum update -y (to update packages if required.)

b. yum install docker (to install docker on the machine)

3. Basic Docker Commands: here is the list of few basic commands which can be used for docker operations:

a. To start docker engine: service start docker/systemctl start docker

b. To check the status of the Docker engine: service start docker/systemctl start docker

c. To stop docker engine: service stop docker/systemctl stop docker

d. To check all containers: docker ps / docker ps -a (It will list all active and inactive containers)

e. To get the list of images (if there are any in the machine, else blank list): docker images

4. Docker file creation: To create an image from source code, the user need to create Dockerfile (keep this name) in the same location where is located. The file will have some basic content as below:

5. Setup folder in EC2: Since the code is developed and deployed in the local machine of the developer, its important to move the code to the EC2 server as below:

a. Use winscp to login using .ppk file which is to be downloaded while spinning up the instance.

b. Upload your code to any folder in EC2.

c. Please check the permission (either root or ec2-user). Use chmod wherever required to set the right permissions.

6. Build and Run images: Now the code is loaded in the folder. Let’s build the image using the below command:

a. Build image: docker build -t <image-name for e.g. actnow_image>

b. Run the container: docker run -p 5000:5000 <image-name for e.g. actnow_image>

7. Sharing the docker image: Since the container is deployed, running, and tested. Now it’s time to share/release the application for testing or production. There are two ways where you can share these images:

a. Save the image on the local machine: Users can save the image in the server as a file (ideally .tar format) using the below command:

docker save — output <tar file name for e.g.actnow_image.tar> <image to be saved for e.g. actnow_image>

It will create a .tar file in the server. This file can be downloaded from EC2 using WinSCP and share with another team.

Another team can load this image in their docker using the below command:

docker load — input <image file name for e.g. actnow_image.tar>

c. Once the other team loads the image, using the docker images command the new image can be found in the list. User can run this image using docker run -p 5000:5000 <image-name for e.g. actnow_image>

b. Using docker hub: The developer can share the images using docker hub also. Create a login if the user doesn’t have one. Provide relevant/identifiable text value for docker id, user email and password to get registered. Push the docker image from the developer machine in the docker hub using the below commands:

  1. Create a tag in source docker using docker tag ec13a8a91b9f <docker-id/repo-name:tag> . “Tag” helps to maintain the release and versions. Once the tag is created a new image will be shown in the docker images command as below:

2) push the image to docker using docker push snitesh140752/actnow_image. This will list the docker image in docker hub website as below:

a. Once the docker hub has this image, another team can use docker pull snitesh140752/actnow_image:rel12–03–2021 to get the image in their docker engine.

b. Run the container using docker run -p 5000:5000 snitesh140752/actnow_image:rel12–03–2021



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store