Home >Backend Development >Python Tutorial >Dockerfile for a Python application
Let’s create a simple Dockerfile for a Python application. This example assumes you have a Python script named app.py and a requirements.txt file containing the dependencies for your application.
# Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed dependencies specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 8080 available to the world outside this container EXPOSE 8080 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
In this Dockerfile:
To build an image using this Dockerfile, navigate to the directory containing the Dockerfile and run:
docker build -t my-python-app .
Replace my-python-app with the desired name for your Docker image.
After building the image, you can run a container from it using:
docker run -p 8080:8080 my-python-app
This command runs a container based on your Docker image, forwarding port 8080 from the container to port 8080 on your host machine. Adjust the port mapping as needed based on your application’s requirements.
The above is the detailed content of Dockerfile for a Python application. For more information, please follow other related articles on the PHP Chinese website!