Skip to content

Commit

Permalink
prep for alpine
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Dec 16, 2023
1 parent a7e3805 commit 2b3141f
Show file tree
Hide file tree
Showing 44 changed files with 122 additions and 106 deletions.
39 changes: 0 additions & 39 deletions Dockerfile

This file was deleted.

65 changes: 58 additions & 7 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as ShellQuote from 'shell-quote'

// defaults for all the flags that will be saved
export const defaults = {
alpine: false,
build: '',
cache: false,
cmd: '',
Expand All @@ -34,6 +35,16 @@ export const defaults = {
secrets: []
}

const ALPINE_MAPPINGS = {
'build-essential': 'build-base',
'chromium-sandbox': 'chromium-chromedriver',
'node-gyp': 'gyp',
'pkg-config': 'pkgconfig',
python: 'python3',
'python-is-python3': 'python3',
sqlite3: 'sqlite'
}

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

// Generate Dockerfile class
Expand Down Expand Up @@ -66,6 +77,38 @@ export class GDF {
// previous answer to conflict prompt
#answer = ''

get variant() {
return this.options.alpine ? 'alpine' : 'slim'
}

alpinize(packages) {
packages = packages.map(name => ALPINE_MAPPINGS[name] || name)
return [...new Set(packages)].sort()
}

get pkg_update() {
return this.options.alpine ? 'apk update' : 'apt-get update -qq'
}

get pkg_install() {
return this.options.alpine ? 'apk add' : 'apt-get install --no-install-recommends -y'
}

get pkg_cache() {
if (this.options.alpine) {
return { cache: '/var/cache/apk' }
} else {
return {
cache: '/var/cache/apt',
lib: '/var/lib/apt'
}
}
}

get pkg_cleanup() {
return this.options.alpine ? '/var/cache/apk/*' : '/var/lib/apt/lists /var/cache/apt/archives'
}

get vite() {
return !!(this.#pj.scripts?.dev === 'vite')
}
Expand Down Expand Up @@ -165,9 +208,11 @@ export class GDF {
get basePackages() {
const packages = [...this.options.packages.base]

packages.sort()

return packages
if (this.options.alpine) {
return this.alpinize(packages)
} else {
return packages.sort()
}
}

// Packages needed for build stage
Expand All @@ -178,9 +223,11 @@ export class GDF {

packages.push(...this.options.packages.build)

packages.sort()

return packages
if (this.options.alpine) {
return this.alpinize(packages)
} else {
return packages.sort()
}
}

// packages needed for deploy stage
Expand All @@ -194,7 +241,11 @@ export class GDF {
if (this.#pj.dependencies?.['fluent-ffmpeg']) packages.push('ffmpeg')
if (this.puppeteer) packages.push('chromium', 'chromium-sandbox')

return packages.sort()
if (this.options.alpine) {
return this.alpinize(packages)
} else {
return packages.sort()
}
}

sortEnv(env) {
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ try {
const options = yargs((hideBin(process.argv)))
.usage('$0 [args]')
.epilog('Options are saved between runs into package.json. more info:\n https://github.com/fly-apps/dockerfile-node#readme')
.option('alpine', {
describe: 'use alpine as base image',
type: 'boolean'
})
.option('build', {
describe: 'if set to "defer" will run build at deploy time',
type: 'string'
Expand Down
20 changes: 10 additions & 10 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<% if (packager === "bun") { -%>
# Adjust BUN_VERSION as desired
ARG BUN_VERSION=<%= bunVersion %>
FROM oven/bun:${BUN_VERSION}-slim as base
FROM oven/bun:${BUN_VERSION}-<%= variant %> as base
<% } else { -%>
# Adjust NODE_VERSION as desired
ARG NODE_VERSION=<%= distroless ? (parseInt(nodeVersion) & ~1) : nodeVersion %>
FROM node:${NODE_VERSION}-slim as <% if (distroless) { -%>build<% } else { %>base<% } %>
FROM node:${NODE_VERSION}-<%= variant %> as <% if (distroless) { -%>build<% } else { %>base<% } %>
<% } -%>

<% if (Object.keys(options.args.base).length > 0) { -%>
Expand All @@ -21,9 +21,9 @@ LABEL fly_launch_runtime="<%= runtime %>"
<% } -%>
<% if (basePackages.length > 0) { -%>
# Install packages needed for both build and deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y <%= basePackages.join(' ')%> && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
RUN <%= pkg_update %> && \
<%= pkg_install %> <%= basePackages.join(' ')%> && \
rm -rf <%= pkg_cleanup %>
<% } -%>
# <%= runtime %> app lives here
Expand Down Expand Up @@ -67,8 +67,8 @@ FROM base as build
<% } -%>
# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y <%= buildPackages.join(' ') %>
RUN <%= pkg_update %> && \
<%= pkg_install %> <%= buildPackages.join(' ') %>

# Install node modules
COPY<% if (options.link) { %> --link<% } %> <%= packageFiles.join(' ') %> ./
Expand Down Expand Up @@ -123,9 +123,9 @@ COPY --link <%= configDir %>litefs.yml /etc/litefs.yml
<% } -%>
<% if (deployPackages.length > 0) { -%>
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y <%= deployPackages.join(' ')%> && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
RUN <%= pkg_update %> && \
a<%= pkg_install %> <%= deployPackages.join(' ')%> && \
rm -rf <%= pkg_cleanup %>
<% } -%>
# Copy built application
Expand Down
2 changes: 1 addition & 1 deletion test/base/build-secret/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/bun/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link bun.lockb package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/cache-bun/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link bun.lockb package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/cache-npm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/cache-pnpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ENV PATH="$PNPM_HOME:$PATH"

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package.json pnpm-lock.yaml ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/cache-yarn/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package.json yarn.lock ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/cmd-entrypoint/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
4 changes: 2 additions & 2 deletions test/base/defer-build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential openssl pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential openssl pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand All @@ -37,7 +37,7 @@ FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y openssl && \
aapt-get install --no-install-recommends -y openssl && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built application
Expand Down
2 changes: 1 addition & 1 deletion test/base/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/distroless/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ENV NODE_ENV="production"

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/envargs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ENV E2="V2"

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
4 changes: 2 additions & 2 deletions test/base/fluent-ffmpeg/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package.json ./
Expand All @@ -33,7 +33,7 @@ FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y ffmpeg && \
aapt-get install --no-install-recommends -y ffmpeg && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built application
Expand Down
2 changes: 1 addition & 1 deletion test/base/ignore-scripts/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/instructions/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/legacy-peer-deps/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand Down
4 changes: 2 additions & 2 deletions test/base/litefs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
Expand All @@ -37,7 +37,7 @@ COPY --link litefs.yml /etc/litefs.yml

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y ca-certificates fuse3 && \
aapt-get install --no-install-recommends -y ca-certificates fuse3 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built application
Expand Down
Loading

0 comments on commit 2b3141f

Please sign in to comment.