Let's deploy a simple voting application using the docker RUN method.
Introduction and Architecture
A front-end web app in Python which lets you vote between two options.
A Redis messaging system which helps you to cast new votes.
A .NET worker which consumes votes and stores them in…
A Postgres database backed by a Docker volume.
A Node.js web app which shows the results of the voting in real-time.
You can clone the repository from here to your local system https://github.com/shashankxrm/example-voting-app.git
From the above 5 components, we don't have to worry about Redis and PostgreSQL. They have prebuilt docker images in DockerHub. The other 3 are the applications we develop.
You don't have to be an expert. You can get the application source code from the GitHub Repository.
Vote App
Click on the vote directory and you will be able to see the source code of the application in the app.py file.
It's a simple flask-based application. It has a POST and GET request.
The GET request sends an Index.html, which shows the actual web page where the user can cast a vote.
The POST section establishes connectivity to Redis and tries to store the vote information in Redis.
Alongside the code, we also have the DockerFile.
The base image is a python:3.9-slim image.
Sets the working directory to the app folder and copies the requirements from the requirements file to install them.
Then copies the source code of the application from the current folder to the app folder inside the container.
And finally the entry point for the application.
Worker Application
You can see the source code of the worker application in the program.cs file in the worker file.
It is a .NET based application. So at the top, it tries to connect to the Redis and PostgreSQL database, and there's a while loop where it waits for the votes from Redis.
When the vote gets cast, it's available in Redis and takes that vote and updates the table in the PostgreSQL database. If you are interested go and take a look at the remainder of the code.
The DockerFile is based on Microsoft dotnet sdk and that is where it adds the source code of the application.
Resulting Application
It is a Node.js based application. Under the result folder open the server.js file to take a look at the source code.
This is an express-based server and all it does is establish connectivity to the PostgreSQL database that it hosts.
Then it reads the values from the database and displays that on the UI.
If we look at the DockerImage, it starts from Node.js slim image and then adds the JSON package which has dependencies to install and copy the source code over and finally specifies the node server.js command.
Deploying the Final Voting Application
Firstly we need to build the docker images for the above three applications as the other two- Redis, and PostgreSQL have prebuilt images on DockerHub.
- Cloning the repository to our local machine
git clone https://github.com/shashankxrm/example-voting-app.git
Let's build the DockerImage for the vote app
Change directory >> /example-voting-app/vote
cat Dockerfile
# reads the dockerfile
docker build . -t voting-app
# builds dockerimage as *voting-app*
Running the vote-app image in a container
sudo docker run -p 5000:80 voting-app # runs voting-app image in container and maps our host port(5000) to # 80 port
If we try to cast our vote, it gives us an error because we didn't run a Redis instance yet. Let's do it now.
Running Redis Container as redis.
sudo docker run -d --name=redis redis
Stop the previous container before running the Redis instance.
Now run the voting-app container by linking it to Redis.
sudo docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
Now we can cast our vote without any error.
We deployed the vote app and Redis containers. Now to deploy the worker container we need the PostgreSQL database.
Refer to the final application structure at the beginning for reference.
Running the PostgreSQL Container as db in the background
sudo docker run -d --name=db postgres:15-alpine
Running Worker Container
To run worker container, first, we need to build its image from its docker file.
Change directory example-voting-app/worker
cd example-voting-app/worker
# changes directory
cat Dockerfile
#opens dockerfile of worker
sudo docker build . -t worker-app
# builds docker image for worker as worker-app
Now run the worker-app container by linking both Redis and postgreSQL
sudo docker run --link redis:redis --link db:db worker-app
Running result app container as result-app
To build the Docker Image
cd /example-voting-app/result # changes directory cat Dockerfile # shows dockerfile
sudo docker build . -t result-app #builds docker image
sudo docker run -p 5001:80 --link db:db result-app # runs the result-app container by linking to postgreSQL database
Now we can see the results of our casted vote on port 5001.
Your Simple-Voting Application Is Ready!
Don't forgot to drop a like and do leave feedback if you learnt something new from this Blog.
Subscribe to my newsletter and Follow for more DevOps Content in the coming days.
Thanks for Reading and Have a Great Day!