Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created June 30, 2024 12:15
Show Gist options
  • Save decagondev/ec2a5ad68624d9b7ab74be2a1c7822bd to your computer and use it in GitHub Desktop.
Save decagondev/ec2a5ad68624d9b7ab74be2a1c7822bd to your computer and use it in GitHub Desktop.

Recommended Workflow for Python Project with Docker

1. Set Up Project Directory

mkdir my_project
cd my_project

2. Create Virtual Environment (Optional for Local Development)

python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`

3. Install Dependencies Locally (Optional for Local Development)

pip install <package1> <package2> ...

4. Create Requirements File

pip freeze > requirements.txt

5. Create Dockerfile

# 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 packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app.py"]

6. Build Docker Image

docker build -t my_project_image .

7. Run Docker Container

docker run -p 4000:80 my_project_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment