Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use smaller base image #46

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.github
config
modules
build.sh
test-build.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the .git directly, should it also be ignored?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comment #46 (comment)

56 changes: 44 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
FROM node:12-buster

RUN set -e; \
apt update; \
apt install -y gettext; \
rm -rf /var/lib/apt/lists/*
FROM node:12-alpine AS BUILD_IMAGE

ARG branch=master

ENV NODE_ENV production
WORKDIR /opt/magic_mirror

RUN git clone --depth 1 -b ${branch} https://github.com/MichMich/MagicMirror.git .
RUN cp -R modules /opt/default_modules
RUN cp -R config /opt/default_config
RUN npm install --unsafe-perm --silent
# get magic mirror
RUN apk update && apk add --no-cache git \
&& git clone --depth 1 -c advice.detachedHead=false -b ${branch} https://github.com/MichMich/MagicMirror.git . \
&& apk del git

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I will remove apk del git in the next commit. Nevertheless, I would prefer to stick with the same base image and definitely not switch between alpine and ubuntu or debian. Using the same base image in all the stages makes the build actually faster because docker does not have to pull different base images to start the build.

Build times on my machine

Stage 0 Stage 1 time
node:12  node:12-alpine 225,53 s
node:12-alpine node:12-alpine 139,66 s

Between the builds I did run docker system prune --allto start from scratch. The time was recorderd with time docker build -t mm:latest .


COPY mm-docker-config.js docker-entrypoint.sh ./
RUN chmod +x ./docker-entrypoint.sh
# save default modules and configuration and install dependencies
RUN set -o pipefail \
&& mkdir dist \
&& cp -R config /opt/magic_mirror/dist/default_config \
&& cp -R modules /opt/magic_mirror/dist/default_modules \
&& npm set unsafe-perm true \
&& npm ci \
# removes required depdencies and should not be used
# && npm prune --production --json \
# prune unnecessary files from ./node_modules, such as markdown, typescript source files, and so on. https://github.com/tj/node-prune
&& wget -q https://install.goreleaser.com/github.com/tj/node-prune.sh | sh \
# it is intentional that modules are not copied to dist folder. Please keep alphabetically sorted
&& cp -R \
config \
css \
fonts \
index.html \
js \
node_modules \
package.json \
package-lock.json \
serveronly \
translations \
vendor /opt/magic_mirror/dist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to copy over all files and just delete the test directory and some dot files, but excluding the small text files are only some KB size. The most size comes from the base image and node_modules directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the benefit of this would be. Adding specifically the files and folders required is what docker recommends and I think it makes clearer what is used and required for MagicMirror to run and what can be left aside. Also, if the structure of MagicMirror changes or other folders are added we would have to update the Dockerfile to remove them as well, whereas now we have to add them if they are required for it to work, otherwise there's no change needed.

In short, even though it's quite the copying taking place it is much more tranparent what is added to the docker image.



FROM node:12-alpine

WORKDIR /opt/magic_mirror

RUN set -x \
&& apk add --no-cache --update libintl \
&& apk add --no-cache --virtual build_deps gettext \
&& apk del build_deps

COPY --from=BUILD_IMAGE /opt/magic_mirror/dist/ .

EXPOSE 8080

COPY mm-docker-config.js docker-entrypoint.sh ./

ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["node", "serveronly"]
4 changes: 2 additions & 2 deletions docker-entrypoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
#!/bin/sh
set -e

if [ ! "$(ls -A /opt/magic_mirror/modules)" ]; then
cp -Rn /opt/default_modules/. /opt/magic_mirror/modules
cp -R /opt/magic_mirror/default_modules/. /opt/magic_mirror/modules
fi

if [ ! "$(ls -A /opt/magic_mirror/config)" ]; then
Expand Down
3 changes: 3 additions & 0 deletions test-build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/bin/bash
set -e

rm -rf ./config
rm -rf ./modules
mkdir config
Expand Down