Skip to content

Commit

Permalink
feat: optimize docker build by multistage (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Apr 24, 2024
1 parent 660c13b commit 02c073d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
49 changes: 38 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM python:3.12-slim
#
# Builder image
#
FROM python:3.12-slim as builder

RUN apt-get update
RUN apt-get install -y gcc python3-dev
RUN apt-get update && \
apt-get install -y --no-install-recommends gcc python3-dev

# Directories paths as environment variables
ENV APP_HOME=/app
Expand All @@ -11,19 +14,43 @@ ENV VIRTUAL_ENV=/app/venv
WORKDIR $APP_HOME
RUN mkdir $VIRTUAL_ENV

# Copy requirements.txt
COPY requirements.txt $APP_HOME

# Active Python venv
RUN python -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Install requirements.txt
RUN pip install --upgrade pip && \
pip install --no-cache-dir -r requirements.txt

#
# Production size image
#
FROM python:3.12-slim as production

# Directories paths as environment variables
ENV APP_HOME=/app
ENV VIRTUAL_ENV=/app/venv

# Set PATH to include venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Working directory
WORKDIR $APP_HOME
RUN mkdir $VIRTUAL_ENV

# Use a non-root user
RUN useradd -m cli
RUN chown cli:cli $VIRTUAL_ENV
RUN useradd -m cli && \
chown cli:cli $VIRTUAL_ENV
USER cli

# Copy requirements.txt
COPY --chown=cli:cli requirements.txt $APP_HOME
# Copy sources from builder image
COPY --from=builder --chown=cli:cli $VIRTUAL_ENV $VIRTUAL_ENV

# Active venv
# Active Python venv
RUN python -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

ENTRYPOINT ["app-store-connect"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ We welcome contributions from the community. Whether it's improving the Dockerfi

## License

This project is released under the MIT License. See the `LICENSE` file in the repository for more details.
This project is released under the MIT License. See the [`LICENSE`](https://github.com/bednar/app-store-connect-cli?tab=MIT-1-ov-file#readme) file in the repository for more details.

## Contact

Expand Down

0 comments on commit 02c073d

Please sign in to comment.