Wait a lab manual?
As it stands, there is an upcoming exam on devops for some very good friends of mine. And with the lack of material provided by college combined with incompetent faculties, this subject quickly became a nightmare for them.
Fear not, since I was summoned to help out this crisis. Time to put some respect on the name of devops and in the process, try and make the life of some of my friends easier.
Note : This handout will contain solutions and explanation for all 6 of the following questions:
,
.
Step 2: Build
If you know, Python is an interpreted language. There is no build step so to speak, since the code gets executed line by line. So here, build refers to the preparation of the environment and making sure the app is ready to run.
We will write a simple shell script (build.sh) to install dependencies and ensure the app runs:
CODE#!/bin/bash
echo "Setting up environment..."
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
echo "Environment setup complete. Run the app with: python app.py"
Run the script:
CODEbash build.sh
Step 3: Test
Now since the program demands we write some test, we will write a very simple test that checks if the app is running or not. Btw shameless plugin but if you want to write tests with 0 code, checkout my .
Unlock Jenkins:
Use the admin password found in: (cant memorise , the password is unique always, you'll find it here)
cat /var/lib/jenkins/secrets/initialAdminPassword
Install Recommended Plugins:
Follow the on-screen setup to install necessary plugins (e.g., Git, NodeJS, Pipeline).
Jenkins Pipeline for a Web Application
- Create a New Job:
- Go to Jenkins Dashboard and click *"New Item"*.
- Choose *"Pipeline"* and give it a name.
- Configure Git Repository:
- In the Pipeline configuration, specify the repository URL:
- *Source Code Management* > Select *Git* > Enter your repository URL.
- Add credentials if needed.
Define the Pipeline Script:
pipeline {
agent any
environment {
VIRTUAL_ENV = 'venv' // Virtual environment directory
FLASK_PORT = '5000' // Port to run the Flask app
}
stages {
stage('Clone Repository') {
steps {
echo 'Cloning the repository...'
git branch: 'main', url: 'https://github.com/ahanasrinath/myflaskapp.git'
}
}
stage('Set Up Environment') {
steps {
echo 'Setting up virtual environment and installing dependencies...'
sh '''
python3 -m venv $VIRTUAL_ENV
source $VIRTUAL_ENV/bin/activate
pip install -r requirements.txt
'''
}
}
stage('Run Tests') {
steps {
echo 'Running unit tests...'
sh '''
source $VIRTUAL_ENV/bin/activate
python -m unittest discover -s . -p "test_*.py"
'''
}
}
stage('Build and Package') {
steps {
echo 'Packaging the application...'
sh '''
tar -czf flask-app.tar.gz app.py requirements.txt .env
'''
}
}
stage('Deploy') {
steps {
echo 'Deploying the application...'
sh '''
# Copy the application to the deployment server
scp flask-app.tar.gz user@your-server:/path/to/deployment
# SSH into the server and deploy
ssh user@your-server << EOF
cd /path/to/deployment
tar -xzf flask-app.tar.gz
source $VIRTUAL_ENV/bin/activate
nohup python app.py > app.log 2>&1 &
EOF
'''
}
}
}
post {
success {
echo 'Pipeline completed successfully!'
}
failure {
echo 'Pipeline failed. Check the logs for more details.'
}
}
}
NOW HERE IS THE THING.....Since we dont have a server to begin with, there is no place for jenkins to deploy our app so to speak. Which again makes me wonder why the fuck have they told y'all to deploy something without any context whatsoever as to where it should be deployed?
Will the college give yall a dedicated server to deploy to? Very unlikely. Do they even know that they need to do so? Also unlikely. So take the last deploy stage with a grain of salt.
and this one by
Now they have mentioned that you should run different container OS, which is brain dead easy:
1. Pull Operating System Images
Docker Hub provides a wide variety of operating system images.
Search for an OS image:
docker search <os-name>
Example for Ubuntu:
docker search ubuntu
Pull an OS image:
docker pull <os-name>:<tag>
Example for Ubuntu:
docker pull ubuntu:latest
2. Run Containers from the OS Image
Run an interactive container:
docker run -it <os-name>:<tag> /bin/bash
Example:
docker run -it ubuntu:latest /bin/bash
This will give you a shell prompt inside the container.
List running containers:
docker ps
List all containers (including stopped ones):
docker ps -a
Stop a running container:
docker stop <container-id>
Remove a container:
docker rm <container-id>
3. Building a Custom Application Container
Now we will dockerise your flask app! The above step 2 is not necessary for this step.
Create a Dockerfile in the root dir
CODEFROM python:3.8-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Build the Docker Image
docker build -t flask-devops-app .
Run the Container
docker run -d -p 5000:5000 flask-devops-app
Access the app at http://localhost:5000.
I will still write what each step does, feel free to skip if you can just memorise it:
FROM python:3.8-slim
- Purpose:
This specifies the base image for your container.
- python:3.8-slim is a lightweight version of the Python 3.8 image. It contains only the essential components required to run Python, making it smaller and faster to download.
WORKDIR /app
- Purpose:
This sets the working directory inside the container to /app.
- Any subsequent commands like COPY or RUN will execute relative to this directory.
- It ensures your application code and dependencies stay organized in a specific directory within the container.
COPY requirements.txt requirements.txt
- Purpose:
This copies the requirements.txt file from your local system (host) to the /app directory inside the container.
- The requirements.txt file contains all the Python libraries your app depends on.
RUN pip install -r requirements.txt
Purpose:
This installs all the Python dependencies specified in the requirements.txt file using pip.
For example, if your app uses Flask, the requirements.txt might contain:
flask==2.1.2
COPY . .
- Purpose:
This copies all files from your local working directory (host) to the /app directory inside the container.
- It ensures your app.py and any other necessary files (e.g., templates, static assets) are available in the container.
CMD ["python", "app.py"]
- Purpose:
This specifies the command the container should run when it starts.
- Here, it tells Docker to execute python app.py, which starts your Flask application.
- The CMD command runs the app in the foreground to keep the container alive while your app is running.
THATS IT! YOUR DOCKER APP IS NOW ALIVE!!!
4. Manage Docker Containers and Images
Some extra commands because they maybe useful yk
View all images:
docker images
Remove an image:
docker rmi <image-id>
Stop and remove all containers:
CODEdocker stop $(docker ps -aq)
docker rm $(docker ps -aq)
Prune unused data (stopped containers, networks, etc.):
docker system prune -a
Question 6: Deploying Apps with Docker
Congrats! If you have reached this far I am v. v. proud of you! Here is your reward: You dont have to study Question 6. Yes, question 5 and 6 are literally same to same steps! You can deploy the same flask app using the same step 3 here as well. For safety, here is the java implementation as well, you will need a simple hello world written in java btw, here it is:
Create Main.java in your project directory
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Dockerized Java App!");
}
}
Then add the Dockerfile in the same folder :
Dockerfile for a Java App
FROM openjdk:11
COPY . /app
WORKDIR /app
RUN javac Main.java
CMD ["java", "Main"]
Explanation for the steps here are the same as the Dockerfile for our Flask app, only that instead of python commands we use java commands:
FROM openjdk:11
- Purpose:
This specifies the base image for your container.
- openjdk:11 is a lightweight image containing the Java Development Kit (JDK) for Java 11.
- It provides everything needed to compile and run Java applications.
- Using a specific version (11) ensures consistency and avoids issues from version mismatches.
COPY . /app
- Purpose:
This copies all files from your current working directory on the host machine to the /app directory inside the container.
- For example, this will include your Java source files (Main.java) and any other files needed to run your application.
WORKDIR /app
- Purpose:
This sets the working directory inside the container to /app.
- All subsequent commands (like RUN or CMD) will execute relative to this directory.
- It ensures your application files are in the right location before compilation or execution.
RUN javac Main.java
- Purpose:
This compiles your Main.java file using the javac (Java Compiler) command inside the container.
- The javac command generates the bytecode .class file (e.g., Main.class), which is necessary to run the Java application.
- If your application has multiple Java files or dependencies, make sure they are all copied and accessible for compilation.
CMD ["java", "Main"]
- Purpose:
This specifies the command the container should run when it starts.
- java Main executes your compiled Java program (Main.class).
- CMD runs the application in the foreground, ensuring the container remains active while the application runs.
After building and running the container, your Java application will execute and output results to the terminal (or wherever your app is programmed to send them).
Build and Run
Build the image:
CODEdocker build -t my-java-app .
Run the container:
CODEdocker run my-java-app
Output:
You should see:
Hello, World!!
Conclusion
I hope this blog helps y'all out in passing this lab exam. This was genuinely fun to write and solve , not sure what your faculty did the entire semester if he/she didn't share this material. While all of this is available online and probably written and explained better, I tried my best in explaining it in my terms and how I would solve all of them.
Then again, your lab systems will have internet. Without internet, you can't run any of the above programs anyways? So yes , its easy to google up the steps or look up this handout during the exam.
Feeling grattitude? I accept payments in the form of cadbury fuse , mango lassi and ocassionally *ganne ka juice. Yes, this is part of my work at Vance and as a devops engineer. Yes, this is a very very smol drop in the ocean of devops and things get complicated really quickly with monitoring and kubernetes and yamls etc. Yes I need to touch some grass and yes, all of you reading are *profoundly welcome <3
Thanks for reading :))
SOCIAL SHARE CARD GENERATOR