Sitemap
Akil Blanchard

Akil Blanchard

Containerize Python app using Docker

5 min readJan 27, 2024
Press enter or click to view image in full size

We will be utilizing Docker to create a Docker image of Python program that is executable in the terminal and we will be pushing this docker image to Docker Hub where it can be shared with everyone.

Prequisites:

Docker Container
Some knowledge of Terraform

The program that will be containerized:

Press enter or click to view image in full size
Pyton program

This program accepts an input from the user that would be a password that the user want to know whether it has been leaked or not and if so how many times. The program interacts with an api that it sends a part of the password to in the form of a hash then it takes the response from the api processes it and provided the final output.

The build begins:

First we will create the infrastructure needed for this project by utilizing a Terraform Module on my GitHub. This will allow for the quick deployment of the environment needed to get the work done.

Press enter or click to view image in full size
Running Terraform Init
Infrastructure created after running Terraform Apply

First thing we will do is ssh into our server by running:

ssh -i /path/to/private-key root@<ec2-public-dns-address>
Welcome message after running ssh

Firstly let us run

sudo apt update -y

If the instance was previously in use this should not take much time, if this is a new instance it may take just slightly longer.

After this proceed to install Docker on the server:

sudo apt install docker.io

After this process is complete it will be good to confirm that docker is now running. This can be accomplished by running:

sudo systemctl status docker
Docker running on the instance

Now let us try to use Docker

docker run hello-world
Press enter or click to view image in full size
I guess not? :(

This happens because we used sudo to install Docker.
Docker by default needs to be installed by using the root user only.

Get Akil Blanchard’s stories in your inbox

Join Medium for free to get updates from this writer.

To fix this the Ubuntu user will be added to the Docker group.
The behavior of the ubuntu user is being modified by the “usermod” and adds it to the docker

sudo usermod -aG ubuntu

After running this sign out of the server and sign back in. Then let’s get this show on the road.

IT’S ALIVE

Now the code to be containerized has to be be sourced.
If you would like to use the same application I am using you can get it from here Github

Cloned Github Repo

Next cd into the directory where the code is stored and create a file called Dockerfile

vim Dockerfie

This file contains all the commands a user could call on the command line to assemble an image.

Firstly in the Dockerfile we will provide the container with a base image. Although containers do not have an entire OS like a VM they will still have a base image that provides container with the bare necessities to operate. The image will be pulled from the Docker registry.

Next set the working directory that will be where the Docker container works out of

The next line tells the Docker where to copy the content it needs from.
Since the Dockerfile will be located in the same place as the code it needs we will just copy it.

The next line is where the necessary packages will be installed.
First we will have the image execute apt-get update to ensure everything is up to date, next Python will be installed as the application is a Python app,
then add in the necessary dependencies for the application.

Next we will create a virtual environemnt that the docker container will run within upon execution. This will prevent conflicting behavior with the system package manager.

Finally the commands to execute the code will be passed into the Dockerfile.

Press enter or click to view image in full size
Dockerfile

Next the image can finally be built by running

docker build -t 'YourDockerAccountUsername/'NameOfYourApplication':1.0 .

The ‘-t’ is used to tag the container as it will later be pushed to the Docker repo, this will aid in finding the container on the repo.

The 1.0 is used to represent the version of the container.

Now we can run the container by executing the docker build command

docker run 'YourDockerAccountUsername/'NameOfYourApplication'
Press enter or click to view image in full size
Executing the container

Build begins: Time to share this beauty with the world

Next sign in to your Docker account by running the below command add in your username and account password:

docker login 

Next we will push the sparkling new container to Docker Hub

docker push YourDockerAccountUsername/'NameOfYourApplication':1.0
Press enter or click to view image in full size
Pushing the container
Press enter or click to view image in full size
WE LIVE

Clean up!

SIDENOTE IlikEBreadandch33s3 has not been leaked before xD

Press enter or click to view image in full size

--

--