Skip to content

Instantly share code, notes, and snippets.

@NimJay
Last active March 22, 2024 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NimJay/53d95c601dc43721362ab983607f3b7d to your computer and use it in GitHub Desktop.
Save NimJay/53d95c601dc43721362ab983607f3b7d to your computer and use it in GitHub Desktop.
Generate requirements.txt for a given requirements.in file using a Docker container.
# Set the version of Python you want to use here.
FROM python:3.12.2-slim@sha256:36d57d7f9948fefe7b6092cfe8567da368033e71ba281b11bb9eeffce3d45bc6 as base
# Ignore this. This is needed to build Online Boutique's recommendationservice.
# RUN apt-get -qq update \
# && apt-get install -y --no-install-recommends \
# wget g++ \
# && rm -rf /var/lib/apt/lists/*
# This Dockerfile assumes that the requirements.in file is in the same folder as this Dockerfile.
COPY requirements.in .
# Install pip-tools which will contain pip-compile.
RUN pip install -U pip pip-tools
# Generate requirements.txt.
RUN pip-compile --output-file=requirements.txt requirements.in
# Let's test that the requirements.txt actually installs dependencies properly.
RUN pip install -r requirements.txt
# Finally, print out the contents of requirements.txt
# when you build this Docker container image
RUN cat requirements.txt
# and when you run this Docker container image
ENTRYPOINT ["cat", "requirements.txt"]
@NimJay
Copy link
Author

NimJay commented Nov 28, 2022

Instructions

  1. Copy this Gist into a file called Dockerfile.
  2. Place the Dockerfile file and your requirements.in file in the same folder.
  3. From that folder, build the Docker container:
    docker build . -t my-docker-container
    
  4. You'll see the contents of the requirements.txt in the previous step, but you can also run the container for cleaner output:
    docker run my-docker-container
    

Without Docker

If you aren't able to use docker, you can use gcloud to build a Dockerfile and push it to an Artifact Registry.

  1. Set environment variables:
    export PROJECT_ID=...
    export IMAGE_TAG=...
    
  2. Create the Artifact Registry repository:
    gcloud artifacts repositories create my-docker-registry \
        --project=${PROJECT_ID} \
        --repository-format=docker \
        --location=us-central1 --description="My Docker registry"
    
  3. Build the repository:
    gcloud builds submit \
        --project=${PROJECT_ID} \
        --tag us-central1-docker.pkg.dev/${PROJECT_ID}/my-docker-registry/${IMAGE_TAG} .
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment