Skip to content

Commit

Permalink
Optimize the Docker files
Browse files Browse the repository at this point in the history
  • Loading branch information
spyker77 committed Nov 3, 2023
1 parent 0fc6dc7 commit 5a655f3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 66 deletions.
32 changes: 10 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ WORKDIR /code
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

# Install system dependencies
RUN apt-get update && \
pip install --upgrade pip pdm && \
rm -rf /var/lib/apt/lists/*
# Install PDM
RUN pip install pdm==2.10.0

# Copy PDM files and install python dependencies
# Copy PDM files and install project dependencies
COPY pyproject.toml pdm.lock ./
RUN mkdir __pypackages__ && pdm sync --dev
RUN pdm sync -p . --global --dev

###################
# 2 STAGE - FINAL #
Expand All @@ -32,30 +30,20 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 AS final
WORKDIR /home/app

# Set environment variables
ENV ENVIRONMENT=dev \
TESTING=0 \
PYTHONPATH=/home/pkgs \
DEBIAN_FRONTEND=noninteractive \
LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH}
ENV DEBIAN_FRONTEND=noninteractive \
ENVIRONMENT=dev \
TESTING=0

# Create the app user and install system dependencies
RUN addgroup --system app && \
adduser --system --group app && \
echo "deb http://archive.ubuntu.com/ubuntu/ mantic main multiverse" >> /etc/apt/sources.list && \
apt-get update && \
apt-get install -y --no-install-recommends software-properties-common && \
# Add the deadsnakes PPA for Python 3.11
add-apt-repository ppa:deadsnakes/ppa && \
# Add repos where to find the libcupti12 for CUDA
add-apt-repository "deb http://archive.ubuntu.com/ubuntu/ mantic main multiverse" && \
apt-get update && \
apt-get install -y python3.11 libcupti12=12.0.* && \
# Create a symlink for Python
ln -s /usr/bin/python3.11 /usr/local/bin/python && \
apt-get install -y --no-install-recommends libcupti12=12.0.* && \
rm -rf /var/lib/apt/lists/*

# Copy packages and executables from the builder stage
COPY --from=builder /code/__pypackages__/3.11/lib /home/pkgs
COPY --from=builder /code/__pypackages__/3.11/bin/* /usr/local/bin/
COPY --from=builder /usr/local /usr/local

# Add app and chown all the files to the app user
COPY --chown=app:app . .
Expand Down
33 changes: 11 additions & 22 deletions Dockerfile.web
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ WORKDIR /code
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

# Install system dependencies
RUN apt-get update && \
pip install --upgrade pip pdm && \
rm -rf /var/lib/apt/lists/*
# Install PDM
RUN pip install pdm==2.10.0

# Copy PDM files and install python dependencies
# Copy PDM files and install project dependencies
COPY pyproject.toml pdm.lock ./
RUN mkdir __pypackages__ && pdm sync --dev
RUN pdm sync -p . --global --prod

###################
# 2 STAGE - FINAL #
Expand All @@ -32,36 +30,27 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 AS final
WORKDIR /home/app

# Set environment variables
ENV ENVIRONMENT=dev \
TESTING=0 \
PYTHONPATH=/home/pkgs \
DEBIAN_FRONTEND=noninteractive \
LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH}
ENV DEBIAN_FRONTEND=noninteractive \
ENVIRONMENT=prod \
TESTING=0

# Create the app user and install system dependencies
RUN addgroup --system app && \
adduser --system --group app && \
echo "deb http://archive.ubuntu.com/ubuntu/ mantic main multiverse" >> /etc/apt/sources.list && \
apt-get update && \
apt-get install -y --no-install-recommends software-properties-common && \
# Add the deadsnakes PPA for Python 3.11
add-apt-repository ppa:deadsnakes/ppa && \
# Add repos where to find the libcupti12 for CUDA
add-apt-repository "deb http://archive.ubuntu.com/ubuntu/ mantic main multiverse" && \
apt-get update && \
apt-get install -y python3.11 libcupti12=12.0.* && \
# Create a symlink for Python
ln -s /usr/bin/python3.11 /usr/local/bin/python && \
apt-get install -y --no-install-recommends libcupti12=12.0.* && \
rm -rf /var/lib/apt/lists/*

# Copy packages and executables from the builder stage
COPY --from=builder /code/__pypackages__/3.11/lib /home/pkgs
COPY --from=builder /code/__pypackages__/3.11/bin/* /usr/local/bin/
COPY --from=builder /usr/local /usr/local

# Add app and chown all the files to the app user
COPY --chown=app:app . .

# Change to the app user
USER app


# Run server
CMD gunicorn -b 0.0.0.0:$PORT -k uvicorn.workers.UvicornWorker app.main:app
32 changes: 10 additions & 22 deletions Dockerfile.worker
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ WORKDIR /code
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

# Install system dependencies
RUN apt-get update && \
pip install --upgrade pip pdm && \
rm -rf /var/lib/apt/lists/*
# Install PDM
RUN pip install pdm==2.10.0

# Copy PDM files and install python dependencies
# Copy PDM files and install project dependencies
COPY pyproject.toml pdm.lock ./
RUN mkdir __pypackages__ && pdm sync --dev
RUN pdm sync -p . --global --prod

###################
# 2 STAGE - FINAL #
Expand All @@ -32,30 +30,20 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 AS final
WORKDIR /home/app

# Set environment variables
ENV ENVIRONMENT=dev \
TESTING=0 \
PYTHONPATH=/home/pkgs \
DEBIAN_FRONTEND=noninteractive \
LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH}
ENV DEBIAN_FRONTEND=noninteractive \
ENVIRONMENT=prod \
TESTING=0

# Create the app user and install system dependencies
RUN addgroup --system app && \
adduser --system --group app && \
echo "deb http://archive.ubuntu.com/ubuntu/ mantic main multiverse" >> /etc/apt/sources.list && \
apt-get update && \
apt-get install -y --no-install-recommends software-properties-common && \
# Add the deadsnakes PPA for Python 3.11
add-apt-repository ppa:deadsnakes/ppa && \
# Add repos where to find the libcupti12 for CUDA
add-apt-repository "deb http://archive.ubuntu.com/ubuntu/ mantic main multiverse" && \
apt-get update && \
apt-get install -y python3.11 libcupti12=12.0.* && \
# Create a symlink for Python
ln -s /usr/bin/python3.11 /usr/local/bin/python && \
apt-get install -y --no-install-recommends libcupti12=12.0.* && \
rm -rf /var/lib/apt/lists/*

# Copy packages and executables from the builder stage
COPY --from=builder /code/__pypackages__/3.11/lib /home/pkgs
COPY --from=builder /code/__pypackages__/3.11/bin/* /usr/local/bin/
COPY --from=builder /usr/local /usr/local

# Add app and chown all the files to the app user
COPY --chown=app:app . .
Expand Down

0 comments on commit 5a655f3

Please sign in to comment.