Containerize Python app using Docker
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:
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.
First thing we will do is ssh into our server by running:
ssh -i /path/to/private-key root@<ec2-public-dns-address>Firstly let us run
sudo apt update -yIf 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.ioAfter this process is complete it will be good to confirm that docker is now running. This can be accomplished by running:
sudo systemctl status dockerNow let us try to use Docker
docker run hello-worldThis 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 ubuntuAfter running this sign out of the server and sign back in. Then let’s get this show on the road.
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
Next cd into the directory where the code is stored and create a file called Dockerfile
vim DockerfieThis 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.
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'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.0Clean up!
SIDENOTE IlikEBreadandch33s3 has not been leaked before xD








