Skip to content

Commit

Permalink
better docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Jul 17, 2024
1 parent efad325 commit 0cd53fe
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.10.0
20.15.1
71 changes: 47 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
# Use an official Python runtime as a parent image
FROM python:3.11-slim
# Build stage
FROM python:3.12.4 as builder

WORKDIR /app

COPY . /app

# Install system dependencies
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
build-essential \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements /app/requirements
RUN pip install --no-cache-dir -r requirements/dev.txt
RUN pip install django

RUN python manage.py migrate
RUN python manage.py shell <<ORM
# Install NVM and Node.js
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 20.15.1

COPY package.json package-lock.json /app/

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash \
&& . "$NVM_DIR/nvm.sh" \
&& nvm install ${NODE_VERSION} \
&& nvm use v${NODE_VERSION} \
&& nvm alias default v${NODE_VERSION} \
&& npm install


# Runtime stage
FROM python:3.12.4

WORKDIR /app

# Copy Python environment from builder
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy Node.js environment from builder
COPY --from=builder /root/.nvm /root/.nvm
ENV NVM_DIR /root/.nvm
ENV PATH $NVM_DIR/versions/node/v20.11.1/bin:$PATH
COPY --from=builder /app/node_modules /app/node_modules

COPY . /app

# Run migrations and create initial data
RUN python manage.py migrate && \
python manage.py shell <<ORM
from django.contrib.auth.models import User
u = User.objects.filter(username='admin').first()
if not u:
Expand All @@ -30,26 +63,16 @@ if queries == 0:
q.save()
ORM

# Set environment variables for NVM
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 20

# Install NVM, Node.js, and npm dependencies
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash \
&& . "$NVM_DIR/nvm.sh" \
&& nvm install ${NODE_VERSION} \
&& nvm use v${NODE_VERSION} \
&& nvm alias default v${NODE_VERSION} \
&& node --version \
&& npm --version \
&& npm install

# Copy the entrypoint script
COPY entrypoint.sh /entrypoint.sh
# Copy and set permissions for the entrypoint script
COPY --chown=myuser:myuser entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Expose the ports the app runs on
EXPOSE 8000 5173

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/ || exit 1

# Set the entrypoint
ENTRYPOINT ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ Development

Included is a test_project that you can use to kick the tires. You can run this via ``docker compose up``

You can now navigate to 127.0.0.1:8000/explorer/ and begin exploring!
You can now navigate to 127.0.0.1:8000/explorer/, log in with admin/admin, and begin exploring!

This will also run a Vite dev server with hot reloading for front-end changes.
2 changes: 1 addition & 1 deletion docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Run:

``docker compose up``

You can now navigate to 127.0.0.1:8000/explorer/ and begin exploring!
You can now navigate to 127.0.0.1:8000/explorer/, log in with admin/admin, and begin exploring!

Installing From Source
----------------------
Expand Down

0 comments on commit 0cd53fe

Please sign in to comment.