Dockerizing and Deploying a Python App
1. Create the Dockerfile
The first step is to create the Dockerfile in the /python_app
directory. This file contains the instructions for building the Docker image.
[tony@stapp01 python_app]$ sudo vi Dockerfile
The content of the file I used was:
# Use a Python 3.9 slim image as the base
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the requirements.txt file from the src/ directory
COPY src/requirements.txt .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application source code from the src/ directory
COPY src/ .
# Expose port 3003
EXPOSE 3003
# Run the server.py script when the container starts
CMD ["python", "server.py"]
This is a standard procedure for setting up a Python application inside a Docker container.
2. Build the Docker Image
Next, build the Docker image using the docker build
command. The command uses the Dockerfile
in the current directory (.
) to create an image named nautilus/python-app
.
[tony@stapp01 python_app]$ docker build -t nautilus/python-app .
The output confirms the successful build process, showing each step from the Dockerfile
being executed:
[+] Building 196.3s (10/10) FINISHED
=> [internal] load build definition from Dockerfile
=> [internal] load metadata for docker.io/library/python:3.9-slim
=> [internal] load .dockerignore
=> [1/5] FROM docker.io/library/python:3.9-slim
=> [internal] load build context
=> [2/5] WORKDIR /app
=> [3/5] COPY src/requirements.txt .
=> [4/5] RUN pip install --no-cache-dir -r requirements.txt
=> [5/5] COPY src/ .
=> exporting to image
=> => exporting layers
=> => writing image sha256:99f9bcece2819a222881e87b93f5779d9fa4cde460c25cb9a06f1d6c91c8f746
=> => naming to docker.io/nautilus/python-app
3. Run the Docker Container
With the image built, I can now run a container from it. The docker run
command is used to create and start a container. The -d
flag runs it in the background, --name
gives it a specific name, and -p
maps the container port to a host port.
[tony@stapp01 python_app]$ docker run -d --name pythonapp_nautilus -p 8092:3003 nautilus/python-app
The output is the container's ID, confirming that it has started successfully:
e3c4697f371922f35dbb84d9077d73f64283918b481ee92bff184dff7c0683d7
4. Test the Application
Finally, test the application's accessibility. First, test locally on the server where the container is running.
[tony@stapp01 /]$ curl http://localhost:8092/
Welcome to xFusionCorp Industries!
The response "Welcome to xFusionCorp Industries!" confirms the application is running and accessible on stapp01
.
Next, test from a different machine (jumphost
) to confirm network accessibility. You must use the hostname or IP address of the server (stapp01
) instead of localhost
.
thor@jumphost ~$ curl http://stapp01:8092/
Welcome to xFusionCorp Industries!
This final output confirms that the application is successfully deployed and accessible from an external host.
Top comments (0)