DEV Community

Janak Shrestha
Janak Shrestha

Posted on Edited on

Docker Python App

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:

  1. Create a Dockerfile under /python_app directory:

    • Use any python image as the base image.
    • Install the dependencies using requirements.txt file.
    • Expose the port 8088.
    • Run the server.py script using CMD.
  2. Build an image named nautilus/python-app using this Dockerfile.

  3. Once image is built, create a container named pythonapp_nautilus:

    • Map port 8088 of the container to the host port 8099.
  4. Once deployed, you can test the app using curl command on App Server 2.

curl http://localhost:8099/
Enter fullscreen mode Exit fullscreen mode

Connect to App Server 2

ssh steve@stapp02
sudo -i
Enter fullscreen mode Exit fullscreen mode

Password for steve: Am3ric@


Inspect the Application Files

cd /python_app
ls -la
ls -la /python_app/src
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Review the dependency list:

cat /python_app/src/requirements.txt
Enter fullscreen mode Exit fullscreen mode

Output:

flask
Enter fullscreen mode Exit fullscreen mode

Review the application entry point:

cat /python_app/src/server.py
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

Verify the file:

cat /python_app/Dockerfile
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

Build the Image

cd /python_app
docker build -t nautilus/python-app .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

nautilus/python-app   latest    cc89e28e8ede   19 seconds ago   141MB
Enter fullscreen mode Exit fullscreen mode

Run the Container

docker run -d --name pythonapp_nautilus -p 8099:8088 nautilus/python-app
Enter fullscreen mode Exit fullscreen mode

Output:

2839b3db14a5cf97cc16e2154845f511823e2696e86a84c24bcfa557aabb62fe
Enter fullscreen mode Exit fullscreen mode

Verify the Container and Port Mapping

docker ps | grep pythonapp_nautilus
docker port pythonapp_nautilus
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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+\['
Enter fullscreen mode Exit fullscreen mode

Output:

                        "HostPort": "8099"
                        "HostPort": "8099"
                        "HostPort": "8099"
                "8088/tcp": [
                "8088/tcp": [
Enter fullscreen mode Exit fullscreen mode

Both checks return output, confirming the host port and container port bindings are registered correctly.


Test the Application

curl http://localhost:8099/
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to xFusionCorp Industries!
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Top comments (0)