-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Dockerfile
102 lines (90 loc) · 3.11 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Dockerfile for helping with testing custom builds without having to set up a
# project.
#
# Build:
# docker build -t exifreader .
# You can select whether to use npm (default) or yarn by passing in `--build-arg pkg_mgr=<npm|yarn|yarn1>`:
# docker build -t exifreader . --build-arg pkg_mgr=yarn
# Add `--no-cache` to force a rebuild, e.g. when there is a new version of exifreader (when using a locally built package this is not necessary).
#
# Run:
# docker run -it --rm -v ./path/to/image.jpg:/image [-v ./path/to/file/with/custom/config.json:/custom.json] [-v ./path/to/locally/built/package.tgz:/exifreader.tgz] exifreader
# The JSON file should contain what would be placed in package.json for the `exifreader` property, e.g. {include: {jpeg: true, exif: ["DateTimeDigitized"]}}.
# Build a local package with `npm pack`.
ARG pkg_mgr=npm
FROM node:22 AS base
WORKDIR /app
# npm
FROM base AS pkg-mgr-npm
RUN npm init -y
RUN npm pkg set type="module"
RUN npm install --save exifreader
# yarn 2+
FROM base AS pkg-mgr-yarn
RUN yarn set version stable
RUN yarn config set nodeLinker node-modules
RUN yarn init -y
RUN npm pkg set type="module"
RUN yarn add exifreader
# yarn 1
FROM base AS pkg-mgr-yarn1
RUN yarn set version classic
RUN yarn init -y
RUN npm pkg set type="module"
RUN yarn add exifreader
# Common for all package managers.
FROM pkg-mgr-${pkg_mgr} AS final
ARG pkg_mgr
# Create scripts for later use inside container.
RUN cat <<EOF > /app/run.sh
#!/bin/bash
set -e
# Install locally built package if available.
if [ -f /exifreader.tgz ]; then
echo "Using locally built package."
PACKAGE_NAME=/exifreader.tgz
if [ "$pkg_mgr" = "yarn" ] || [ "$pkg_mgr" = "yarn1" ]; then
yarn add \$PACKAGE_NAME
else
$pkg_mgr install \$PACKAGE_NAME
fi
fi
# Load custom config file if it exists.
node custom-config.js
# Rebuild if there was a custom config file.
if [ -f /custom.json ]; then
if [ "$pkg_mgr" = "yarn" ]; then
yarn rebuild exifreader || (cat /tmp/*/build.log && false)
elif [ "$pkg_mgr" = "yarn1" ]; then
if [ -f /exifreader.tgz ]; then
# There doesn't seem to be any way to rebuild a locally built package with yarn 1.
yarn remove exifreader
fi
yarn add exifreader
else
npm rebuild exifreader
fi
fi
# List size of custom-built bundle.
ls -l node_modules/exifreader/dist/exif-reader.js
# Extract and print image tags.
node exif.js
EOF
RUN cat /app/run.sh
RUN chmod +x /app/run.sh
RUN cat <<EOF > /app/custom-config.js
import {existsSync, writeFileSync} from 'fs';
if (existsSync('/custom.json')) {
console.log('Using custom config file.');
const customConfig = (await import('/custom.json', {with: {type: 'json'}})).default;
const packageJson = (await import('./package.json', {with: {type: 'json'}})).default;
packageJson.exifreader = customConfig;
writeFileSync('package.json', JSON.stringify(packageJson, null, 2));
}
EOF
RUN cat <<EOF > /app/exif.js
import ExifReader from 'exifreader';
const tags = await ExifReader.load('/image', {expanded: true, async: true});
console.log(tags);
EOF
ENTRYPOINT ["/app/run.sh"]