Building development image with Nodejs and production without NodeJS (with only precompiled files)
I have a Django application, which is using TailwindCSS for styling (using the django-tailwind package). I am developing locally with docker compose and plan to deploy using the same.
So I have the following requirements
- For development: I need to run the
python manage.py tailwind start
ornpm run dev
command so that the postcss watcher rebuilds the static files when I am developing the application (this requires NodeJS) - For Production: I compile the CSS files at build time and do not need NodeJS overhead.
I can always create two Dockerfiles for development and production, but I do not want to do that unless absolutely necessary.
How can I do both of these in a single Dockerfile. This is the current Dockerfile I have
ARG BUILD_TYPE=production
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim AS base-builder
# Set environment variables to optimize Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set environment variables to optimize UV
ENV UV_COMPILE_BYTECODE=1
ENV UV_SYSTEM_PYTHON=1
WORKDIR /app
# Install the requirements
COPY uv.lock .
COPY pyproject.toml .
# Update the package list and install Node.js
RUN apt-get update && \
apt-get install -y nodejs npm && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
FROM base-builder AS production-builder
RUN echo "Running the Production build image!"
ENV UV_NO_DEV=1
FROM base-builder AS development-builder
RUN echo "Running the Development build image!"
ENV UV_NO_DEV=0
FROM ${BUILD_TYPE}-builder AS builder
# Install dependencies
RUN uv sync --locked
# Copy the codebase
COPY ./src .
# Build the theme
RUN cd theme/static_src && \
npm install && \
npm run build && \
rm -rf node_modules
FROM python:3.13-slim-bookworm
WORKDIR /app
RUN apt-get update && apt-get upgrade -y && apt-get clean
COPY --from=builder /app/.venv/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin/
COPY --from=builder /app/ /app/
COPY ./docker/entrypoint.sh .
RUN chmod +x entrypoint.sh
ENTRYPOINT [ "./entrypoint.sh" ]
The entrypoint script
#!/bin/sh
# Apply database migrations
echo "Applying database migrations..."
python manage.py migrate
# Collect static files
echo "Collecting static files..."
python manage.py collectstatic --noinput
# Create the superuser
echo "Creating superuser..."
python manage.py createsuperuser --noinput
# Start the application
echo "Starting application..."
exec "$@"