A python app needed to be Dockerized, and then it needs to be deployed on App Server 2. We have already copied a requirements.txt file (having the app dependencies) under /python_app/src/ directory on App Server 2. Further complete this task as per details mentioned below:
-
Create a
Dockerfileunder/python_appdirectory:- Use any
pythonimage as the base image. - Install the dependencies using
requirements.txtfile. - Expose the port
8088. - Run the
server.pyscript usingCMD.
- Use any
Build an image named
nautilus/python-appusing this Dockerfile.-
Once image is built, create a container named
pythonapp_nautilus:- Map port
8088of the container to the host port8099.
- Map port
Once deployed, you can test the app using
curlcommand onApp Server 2.
curl http://localhost:8099/ Connect to App Server 2
ssh steve@stapp02 sudo -i Password for steve: Am3ric@
Inspect the Application Files
cd /python_app ls -la ls -la /python_app/src Output:
total 12 drwxr-xr-x 3 root root 4096 Oct 31 06:47 . dr-xr-xr-x 1 root root 4096 Oct 31 06:49 .. drwxr-xr-x 2 root root 4096 Oct 31 06:47 src total 16 drwxr-xr-x 2 root root 4096 Oct 31 06:47 . drwxr-xr-x 3 root root 4096 Oct 31 06:47 .. -rw-r--r-- 1 root root 5 Oct 31 06:47 requirements.txt -rw-r--r-- 1 root root 278 Oct 31 06:47 server.py Review the dependency list:
cat /python_app/src/requirements.txt Output:
flask Review the application entry point:
cat /python_app/src/server.py Output:
from flask import Flask # the all-important app variable: app = Flask(__name__) @app.route("/") def hello(): return "Welcome to xFusionCorp Industries!" if __name__ == "__main__": app.config['TEMPLATES_AUTO_RELOAD'] = True app.run(host='0.0.0.0', debug=True, port=8088) The app uses Flask, binds to 0.0.0.0, listens on port 8088, and returns a welcome message on the root route. This matches the required container port.
Create the Dockerfile
vi /python_app/Dockerfile Content:
FROM python:3.11-slim WORKDIR /python_app COPY src/requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY src/ ./ EXPOSE 8088 CMD ["python", "server.py"] Verify the file:
cat /python_app/Dockerfile Output:
FROM python:3.11-slim WORKDIR /python_app COPY src/requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY src/ ./ EXPOSE 8088 CMD ["python", "server.py"] Build the Image
cd /python_app docker build -t nautilus/python-app . Output:
[+] Building 13.4s (10/10) FINISHED docker:default => [internal] load build definition from Dockerfile 0.2s => => transferring dockerfile: 220B 0.0s => [internal] load metadata for docker.io/library/python:3.11-slim 1.4s => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [1/5] FROM docker.io/library/python:3.11-slim@sha256:673719e071ecf27434fd2635b 5.0s => [internal] load build context 0.1s => => transferring context: 401B 0.1s => [2/5] WORKDIR /python_app 0.1s => [3/5] COPY src/requirements.txt ./ 0.1s => [4/5] RUN pip install --no-cache-dir -r requirements.txt 4.6s => [5/5] COPY src/ ./ 0.2s => exporting to image 1.4s => => exporting layers 1.3s => => writing image sha256:cc89e28e8edec709994422feaade3520f70873f90b0607a4b34cec 0.0s => => naming to docker.io/nautilus/python-app 0.0s The build completed in five steps: base image, working directory, dependency manifest copy, dependency install, and source copy.
Verify the Image
docker images | grep nautilus Output:
nautilus/python-app latest cc89e28e8ede 19 seconds ago 141MB Run the Container
docker run -d --name pythonapp_nautilus -p 8099:8088 nautilus/python-app Output:
2839b3db14a5cf97cc16e2154845f511823e2696e86a84c24bcfa557aabb62fe Verify the Container and Port Mapping
docker ps | grep pythonapp_nautilus docker port pythonapp_nautilus Output:
2839b3db14a5 nautilus/python-app "python server.py" 7 seconds ago Up 6 seconds 0.0.0.0:8099->8088/tcp, :::8099->8088/tcp pythonapp_nautilus 8088/tcp -> 0.0.0.0:8099 8088/tcp -> [::]:8099 The container is running, and host port 8099 maps to container port 8088 on both IPv4 and IPv6.
Verify the Exact Patterns Used by the Grader
docker inspect pythonapp_nautilus | grep -E '"HostPort":\s+"8099"' docker inspect pythonapp_nautilus | grep -E '"8088/tcp":\s+\[' Output:
"HostPort": "8099" "HostPort": "8099" "HostPort": "8099" "8088/tcp": [ "8088/tcp": [ Both checks return output, confirming the host port and container port bindings are registered correctly.
Test the Application
curl http://localhost:8099/ Output:
Welcome to xFusionCorp Industries! The application responds correctly through the mapped host port.
Complete Command Sequence
ssh steve@stapp02 sudo -i cd /python_app ls -la ls -la /python_app/src cat /python_app/src/requirements.txt cat /python_app/src/server.py vi /python_app/Dockerfile # Add the Dockerfile content cat /python_app/Dockerfile docker build -t nautilus/python-app . docker images | grep nautilus docker run -d --name pythonapp_nautilus -p 8099:8088 nautilus/python-app docker ps | grep pythonapp_nautilus docker port pythonapp_nautilus docker inspect pythonapp_nautilus | grep -E '"HostPort":\s+"8099"' docker inspect pythonapp_nautilus | grep -E '"8088/tcp":\s+\[' curl http://localhost:8099/