Skip to content

Commit

Permalink
Create smaller docker images
Browse files Browse the repository at this point in the history
Utilise multi-stage docker builds to reduce size from over 500MB to 100MB

* Base image uses minimal dependencies to run scripts in final stage e.g. only
using node in final CMD means we can move npm to compile stage.
* Reduced dbus-python build dependencies to those actually required.
* Using `pip install --user` to easily copy over Python requirements.
* Bumped Python base image version to 3.10
* Excluded unneeded files from final image.
  • Loading branch information
cas-- committed Feb 15, 2023
1 parent 3fdca1b commit a10b8de
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
node_modules
.git
.gitignore
.github
Dockerfile
*.md
LICENSE
44 changes: 34 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
FROM python:3.9.16-alpine3.17
FROM python:3.10.10-alpine3.17 as base
LABEL maintainer="Ben Hardill [email protected]"
RUN apk add --no-cache --update \
dbus-libs \
'nodejs<19'

RUN \
apk add --update autoconf automake make cmake pkgconfig gcc libc-dev g++ glib-dev linux-headers dbus dbus-dev && \
apk add --update 'nodejs<19' 'npm<10' && \
pip install -U pip && pip install pipenv && \
rm -rf /var/lib/apt/lists/*
# Install dependencies
FROM base as compile-image

WORKDIR /usr/src/app

COPY . .
RUN pip install mdns-publisher && \
npm install
RUN apk add --no-cache --update \
cmake \
g++ \
glib-dev \
dbus \
dbus-dev \
glib-dev \
'npm<10' && \
pip install --upgrade --no-cache-dir pip

CMD ["npm", "start"]
RUN pip install --user --no-cache-dir mdns-publisher

COPY package.json package-lock.json .
RUN npm ci --production

# Build application
FROM base as build-image

WORKDIR /usr/src/app

# app
COPY cname.py index.js .
# npm packages
COPY --from=compile-image /usr/src/app/node_modules node_modules
# pip packages
COPY --from=compile-image /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH

CMD ["node", "index.js"]

0 comments on commit a10b8de

Please sign in to comment.