Why Automating Docker Setup with Dockerfile is Better Than Typing Commands
Overview
This project involves deploying a simple Flask web application inside a Docker container running the RedHat Universal Base Image 8 (UBI8). The process was first done manually and then automated using a Dockerfile. The goal is to demonstrate how to automate the setup of a Flask app by installing dependencies, copying the application code, and running the app inside the container.
Manual Steps (Initial Setup)
Start with RedHat UBI8 Container
First, we use the RedHat UBI8 container image:docker pull redhat/ubi8
Install Python3
Once inside the container, we install Python 3 using the following command:yum install python3 -y
Install Flask
We then install Flask, the Python web framework, usingpip
:pip3 install flask
Copy Application Code
Next, we copy the application code (app.py
) into the container. You can use thedocker cp
command or manually copy the file inside:docker cp app.py <container_id>:/app.py
Run the Application
Finally, the Flask app is run using the following command inside the container:python3 /app.py
At this point, the Flask app should be running inside the container and accessible on port 5000
.
Automated Deployment Using Dockerfile
After performing the setup manually, the next step is to automate the process using a Dockerfile. This approach simplifies the deployment and ensures consistency when building and running the container.
Dockerfile
The Dockerfile automates the entire process, from installing dependencies to running the Flask app:
FROM redhat/ubi8
# Install Python3
RUN yum install python3 -y
# Install Flask
RUN pip3 install flask
# Set working directory inside the container
WORKDIR /app
# Copy the Flask app code from the host machine to the container
COPY app.py vimal.py
# Run the Flask application
CMD ["python3", "vimal.py"]
Explanation of Dockerfile
FROM redhat/ubi8:
This specifies the use of the RedHat Universal Base Image 8 (UBI8) as the container image.RUN yum install python3 -y:
Installs Python 3 inside the container.RUN pip3 install flask:
Installs the Flask web framework usingpip3
.WORKDIR /app:
Sets the working directory inside the container to/app
.COPY app.py vimal.py:
Copies theapp.py
file from the host machine into the container and renames it tovimal.py
.CMD ["python3", "vimal.py"]:
Specifies the command to run the Flask app, which ispython3
vimal.py
.
Flask Application Code (vimal.py)
The Flask app (vimal.py
) contains the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/info")
def lw():
return "welcome to lw"
app.run(host='0.0.0.0')
Key Features:
The app defines a single route,
/info
, which returns a simple "welcome to lw" message.app.run
(host='0.0.0.0')
ensures the app is accessible from outside the container.
Building and Running the Docker Container
1. Build the Docker Image
To build the Docker image, run the following command in the directory containing your Dockerfile
:
docker build -t flask-app .
2. Run the Docker Container
Once the image is built, you can run the container using the following command:
docker run -p 5000:5000 flask-app
This command binds port 5000
of the container to port 5000
of the host system, making the Flask app accessible at http://localhost:5000/info
.
3. Access the Application
Once the container is running, you can access the Flask app by navigating to:
http://localhost:5000/info
You should see the message: "welcome to lw".
Conclusion
This project demonstrates how to deploy a simple Flask application in a Docker container based on the RedHat Universal Base Image 8 (UBI8). By first performing manual setup and then automating the process with a Dockerfile, we ensure a repeatable and consistent deployment process. The containerized Flask app is accessible through port 5000
and responds with a simple message on the /info
route.