Skip to content

Commit c8a035a

Browse files
committed
feat: actualiza dockerfile
1 parent e214487 commit c8a035a

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

.dockerignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
.venv
1+
.*/
2+
.venv
3+
node_modules
4+
*.egg-info

Dockerfile

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
# From https://docs.astral.sh/uv/guides/integration/docker/
2-
FROM python:3.11-slim
1+
# First, build the application in the `/app` directory.
2+
FROM ghcr.io/astral-sh/uv:0.8.0-python3.12-bookworm-slim AS builder
3+
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
34

4-
# uv installer requires curl (and certificates) to download the release archive
5-
# ssh and git are required by setuptools_scm for automatic versioning using git
6-
RUN apt-get -yq update && apt-get -yqq install --no-install-recommends ssh git curl ca-certificates
5+
# Disable Python downloads, because we want to use the system interpreter
6+
# across both images. If using a managed Python version, it needs to be
7+
# copied from the build image into the final image
8+
ENV UV_PYTHON_DOWNLOADS=0
79

8-
# Download the latest installer
9-
ADD https://astral.sh/uv/0.4.3/install.sh /uv-installer.sh
10+
# Required for setuptools_scm: https://setuptools-scm.readthedocs.io/en/stable/usage/
11+
ARG SETUPTOOLS_SCM_PRETEND_VERSION=1
1012

11-
# Run the installer then remove it
12-
RUN sh /uv-installer.sh && rm /uv-installer.sh
13+
WORKDIR /app
14+
RUN --mount=type=cache,target=/root/.cache/uv \
15+
--mount=type=bind,source=uv.lock,target=uv.lock \
16+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
17+
uv sync --frozen --no-install-project --no-dev
1318

14-
# Ensure the installed binary is on the `PATH`
15-
ENV PATH="/root/.cargo/bin/:$PATH"
19+
ADD . /app
20+
RUN --mount=type=cache,target=/root/.cache/uv \
21+
uv sync --frozen --no-dev
1622

17-
# Change the working directory to the `app` directory
18-
WORKDIR /app
1923

20-
# Copy the lockfile and `pyproject.toml` into the image
21-
COPY uv.lock /app/uv.lock
22-
COPY pyproject.toml /app/pyproject.toml
24+
# Then, use a final image without uv
25+
FROM python:3.12-slim-bookworm
26+
# It is important to use the image that matches the builder, as the path to the
27+
# Python executable must be the same, e.g., using `python:3.11-slim-bookworm`
28+
# will fail.
2329

24-
# Install dependencies
25-
RUN uv sync --frozen --no-cache --no-dev --no-install-project
30+
# Copy the application from the builder
31+
COPY --from=builder --chown=app:app /app /app
2632

27-
# Copy the project into the image
28-
COPY . /app
33+
# Place executables in the environment at the front of the path
34+
ENV PATH="/app/.venv/bin:$PATH"
2935

30-
# Sync the project into a new environment
31-
# Mounting the git directory is required by setuptools_scm: https://setuptools-scm.readthedocs.io/en/stable/usage/
32-
RUN --mount=source=.git,target=.git,type=bind \
33-
uv sync --frozen --no-cache --no-dev
36+
WORKDIR /app
3437

35-
CMD ["uv", "run", "uvicorn", "template.api:app", "--host", "0.0.0.0", "--port", "80"]
38+
# Run the FastAPI application by default
39+
CMD ["uvicorn", "template.api:app", "--host", "0.0.0.0", "--port", "80"]

Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.ONESHELL:
2-
.PHONY: install hooks hooks-update ruff test mypy docker build run debug push
2+
.PHONY: install hooks hooks-update ruff test test-api docker build run debug attach push
33

44
SHELL=/bin/bash
55
DOCKER_IMG_NAME=ghcr.io/komorebi-ai/python-template
@@ -34,8 +34,8 @@ ruff:
3434
test:
3535
uv run pytest
3636

37-
mypy:
38-
uv run mypy --install-types --non-interactive
37+
test-api:
38+
uvx --from httpie http GET localhost:6000
3939

4040
docker: build run
4141

@@ -45,11 +45,14 @@ build:
4545
# https://stackoverflow.com/questions/26564825/what-is-the-meaning-of-a-double-dollar-sign-in-bash-makefile
4646
run:
4747
[ "$$(docker ps -a | grep $(DOCKER_CONTAINER))" ] && docker stop $(DOCKER_CONTAINER) && docker rm $(DOCKER_CONTAINER)
48-
docker run -d --restart=unless-stopped --name $(DOCKER_CONTAINER) -p 5000:80 $(DOCKER_IMG_NAME)
48+
docker run -d --restart=unless-stopped --name $(DOCKER_CONTAINER) -p 6000:80 $(DOCKER_IMG_NAME)
4949

5050
debug:
5151
docker run -it $(DOCKER_IMG_NAME) /bin/bash
5252

53+
attach:
54+
docker exec -it $(DOCKER_CONTAINER) /bin/bash
55+
5356
push:
5457
docker login https://ghcr.io/komorebi-ai -u $(GH_USER) --password-stdin < $(GH_TOKEN_FILE)
55-
docker push $(DOCKER_IMG_NAME):latest
58+
docker push $(DOCKER_IMG_NAME):latest

0 commit comments

Comments
 (0)