Skip to content

Commit

Permalink
feat: use chown during copy to reduce image size and build time
Browse files Browse the repository at this point in the history
Using the `--chown` flag during `COPY` is much faster than running
`chown -R`. It also removes a layer from the image, resulting in faster
and smaller builds.

Resolves #55

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
  • Loading branch information
LucasRoesler authored and alexellis committed Oct 4, 2022
1 parent bf64898 commit 56b349d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
9 changes: 4 additions & 5 deletions template/python27-flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .
COPY --chown=app:app index.py .
COPY --chown=app:app requirements.txt .
USER root
RUN pip install -r requirements.txt
USER app

RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
COPY --chown=app:app function/requirements.txt .
RUN pip install --user -r requirements.txt

WORKDIR /home/app/

USER root
COPY function function
RUN chown -R app:app ./
COPY --chown=app:app function function
USER app

ENV fprocess="python index.py"
Expand Down
9 changes: 4 additions & 5 deletions template/python3-flask-debian/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .
COPY --chown=app:app index.py .
COPY --chown=app:app requirements.txt .

USER root
RUN pip install -r requirements.txt
Expand All @@ -31,14 +31,13 @@ USER app
RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
COPY --chown=app:app function/requirements.txt .
RUN pip install --user -r requirements.txt

#install function code
USER root

COPY function/ .
RUN chown -R app:app ../
COPY --chown=app:app function/ .

ARG TEST_COMMAND=tox
ARG TEST_ENABLED=true
Expand Down
9 changes: 4 additions & 5 deletions template/python3-flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .
COPY --chown=app:app index.py .
COPY --chown=app:app requirements.txt .

USER root
RUN pip install -r requirements.txt
Expand All @@ -31,14 +31,13 @@ USER app
RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
COPY --chown=app:app function/requirements.txt .
RUN pip install --user -r requirements.txt

#install function code
USER root

COPY function/ .
RUN chown -R app:app ../
COPY --chown=app:app function/ .

ARG TEST_COMMAND=tox
ARG TEST_ENABLED=true
Expand Down
9 changes: 4 additions & 5 deletions template/python3-http-debian/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .
COPY --chown=app:app index.py .
COPY --chown=app:app requirements.txt .
USER root
RUN pip install -r requirements.txt
USER app

RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
COPY --chown=app:app function/requirements.txt .
RUN pip install --user -r requirements.txt

USER root
COPY function/ .
RUN chown -R app:app ../
COPY --chown=app:app function/ .

ARG TEST_COMMAND=tox
ARG TEST_ENABLED=true
Expand Down
9 changes: 4 additions & 5 deletions template/python3-http/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py .
COPY requirements.txt .
COPY --chown=app:app index.py .
COPY --chown=app:app requirements.txt .
USER root
RUN pip install -r requirements.txt

Expand All @@ -30,13 +30,12 @@ USER app
RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
COPY --chown=app:app function/requirements.txt .
RUN pip install --user -r requirements.txt

# install function code
USER root
COPY function/ .
RUN chown -R app:app ../
COPY --chown=app:app function/ .

ARG TEST_COMMAND=tox
ARG TEST_ENABLED=true
Expand Down

0 comments on commit 56b349d

Please sign in to comment.