47/100 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:
Create a Dockerfile under /python_app directory:
- Use any
pythonimage as the base image. - Install the dependencies using
requirements.txtfile. - Expose the port
5004. - Run the
server.pyscript usingCMD.
Build an image named nautilus/python-app using this Dockerfile. Once image is built, create a container named pythonapp_nautilus. Map port 5004 of the container to the host port 8096. Once deployed, you can test the app using curl command on App Server 2.
ssh as normal
cd /python_app
ls
src
cd src
[steve@stapp02 src]$ ls
requirements.txt server.py
cd ../ <- so we are back in the python_app directory
sudo vi Dockerfile
# Use any Python base image
FROM python:3.10-slim
# Set working directory inside container
WORKDIR /app
# Copy requirements.txt first (for caching)
COPY src/requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the app files
COPY src/ .
# Expose the application port
EXPOSE 5004
# Command to run the Python server
CMD ["python", "server.py"]
esc
wq!
# Build the docker image
docker build -t nautilus/python-app .
[+] Building 192.4s (10/10) FINISHED docker:default
# Run the container
docker run -d --name pythonapp_nautilus -p 8096:5004 nautilus/python-app
792114a7169c49779c19dbde997dadf87ac3e12adcc80822427e09936e25b17b
# Verify that its running
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
792114a7169c nautilus/python-app "python server.py" 53 seconds ago Up 50 seconds 0.0.0.0:8096->5004/tcp pythonapp_nautilus
curl stapp02:8096
Welcome to xFusionCorp Industries!