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 scratch as base image #1254

Merged
merged 5 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 31 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#### Builder ####
FROM node:18-alpine3.16 as builder

WORKDIR /usr/src/app
WORKDIR /app

COPY . .

Expand Down Expand Up @@ -42,34 +42,50 @@ RUN yarn workspace @gardener-dashboard/backend prod-install --pack /usr/src/
# run frontend build
RUN yarn workspace @gardener-dashboard/frontend run build

# copy files to production directory
RUN cp -r frontend/dist /usr/src/build/public \
&& find /usr/src/build/.yarn -mindepth 1 -name cache -prune -o -exec rm -rf {} +
WORKDIR /volume

RUN apk add --no-cache tini \
# tini and node binaries
&& mkdir -p ./sbin ./usr/local/bin \
&& cp /sbin/tini ./sbin/ \
&& cp /usr/local/bin/node ./usr/local/bin/ \
# root ca certificates
&& mkdir -p ./etc/ssl \
&& cp -r /etc/ssl/certs ./etc/ssl \
# node user
&& echo 'node:x:1000:1000:node,,,:/home/node:/sbin/nologin' > ./etc/passwd \
&& echo 'node:x:1000:node' > ./etc/group \
&& mkdir -p ./home/node \
&& chown 1000:1000 ./home/node \
# libc, libgcc and libstdc++ libraries
&& mkdir -p ./lib ./usr/lib \
&& cp -d /lib/ld-musl-x86_64.so.* ./lib \
&& cp -d /lib/libc.musl-x86_64.so.* ./lib \
&& cp -d /usr/lib/libgcc_s.so.* ./usr/lib \
&& cp -d /usr/lib/libstdc++.so.* ./usr/lib \
# application
&& mv /usr/src/build ./app \
&& find ./app/.yarn -mindepth 1 -name cache -prune -o -exec rm -rf {} + \
&& mv /app/frontend/dist ./app/public \
&& chown -R 1000:1000 ./app

#### Release ####
FROM alpine:3.16 as release
FROM scratch as release

RUN addgroup -g 1000 node && adduser -u 1000 -G node -s /bin/sh -D node
RUN apk add --no-cache tini libstdc++

WORKDIR /usr/src/app
WORKDIR /app

ENV NODE_ENV "production"

ARG PORT=8080
ENV PORT $PORT

# copy node binary
COPY --from=builder /usr/local/bin/node /usr/local/bin/

# copy production directory
COPY --chown=node:node --from=builder /usr/src/build .
COPY --from=builder /volume /

USER node

EXPOSE $PORT

VOLUME ["/home/node"]

ENTRYPOINT [ "/sbin/tini", "--", "node", "--require=/usr/src/app/.pnp.cjs", "--experimental-loader=/usr/src/app/.pnp.loader.mjs"]
ENTRYPOINT [ "tini", "--", "node", "--require=/app/.pnp.cjs", "--experimental-loader=/app/.pnp.loader.mjs"]
CMD ["server.js"]
25 changes: 2 additions & 23 deletions backend/test/docker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ const _ = require('lodash')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
const { DockerfileParser } = require('dockerfile-ast')
const { extend, globalAgent } = jest.requireActual('@gardener-dashboard/request')
const client = extend({
prefixUrl: 'https://raw.githubusercontent.com/nodejs/docker-node/main/'
})

/* Nodejs release schedule (see https://nodejs.org/en/about/releases/) */
const activeNodeReleases = {
Expand All @@ -33,24 +29,13 @@ const activeNodeReleases = {
}
}

async function getNodeDockerfile (nodeVersion, alpineVersion) {
const body = await client.request(`${nodeVersion}/alpine${alpineVersion}/Dockerfile`)
return DockerfileParser.parse(body)
}

async function getDashboardDockerfile () {
const filename = path.join(__dirname, '..', '..', 'Dockerfile')
const data = await readFile(filename, 'utf8')
return DockerfileParser.parse(data)
}

describe('dockerfile', function () {
const timeout = 15 * 1000

afterAll(() => {
globalAgent.destroy()
})

it('should have the same alpine base image as the corresponding node image', async function () {
const dashboardDockerfile = await getDashboardDockerfile()

Expand All @@ -66,12 +51,6 @@ describe('dockerfile', function () {
const endOfLife = activeNodeReleases[nodeRelease].endOfLife
// Node release ${nodeRelease} reached end of life. Update node base image in Dockerfile.
expect(endOfLife.getTime()).toBeGreaterThan(Date.now())
const dashboardReleaseBaseImage = buildStages.release.getImage()
const [, alpineVersion] = /alpine:(\d+\.\d+)/.exec(dashboardReleaseBaseImage)
const nodeDockerfile = await getNodeDockerfile(nodeRelease, alpineVersion)
expect(nodeDockerfile.getFROMs()).toHaveLength(1)
const nodeBaseImage = _.first(nodeDockerfile.getFROMs()).getImage()
// Alpine base images of "dashboard-release" image and "node" image do not match!
expect(dashboardReleaseBaseImage.endsWith(nodeBaseImage)).toBe(true)
}, timeout)
expect(buildStages.release.getImage()).toBe('scratch')
})
})