Skip to content

Commit

Permalink
fix(docker): use lerna version + combined dockerfile (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagith authored Oct 8, 2024
1 parent 1940a88 commit 63933d4
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 63 deletions.
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM node:20-slim AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

RUN corepack enable

FROM base AS build

COPY . /usr/src/app
WORKDIR /usr/src/app
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm build
RUN pnpm deploy --filter=playground --prod /prod/playground
RUN ls -al /prod/playground

FROM nginx:stable AS playground

COPY packages/playground/.docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY packages/playground/.docker/expires.conf /etc/nginx/conf.d/expires.conf

WORKDIR /app
COPY --from=build /prod/playground/dist /app
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: '3.7'

services:
playground:
build:
context: .
dockerfile: packages/playground/Dockerfile
target: playground
ports:
- '8080:80'
tty: true
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "monorepo-typescript",
"private": true,
"author": "Coldrun <[email protected]>",
"homepage": "https://github.com/coldrun/monorepo-typescript",
"type": "module",
"packageManager": "[email protected]",
"engineStrict": true,
Expand Down Expand Up @@ -33,12 +34,12 @@
"globals": "^15.9.0",
"just-pnpm": "^1.0.2",
"lint-staged": "^15.2.10",
"minimist": "^1.2.8",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"simple-git-hooks": "^2.11.1",
"typescript": "~5.5.4",
"typescript-eslint": "^8.5.0"
"typescript-eslint": "^8.5.0",
"yargs": "^17.7.2"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
Expand Down
25 changes: 0 additions & 25 deletions packages/playground/Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build:docker": "node ../../scripts/docker.js playground",
"build:docker": "node ../../scripts/docker build playground",
"preview": "vite preview"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 60 additions & 29 deletions scripts/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { execa } from 'execa';
import minimist from 'minimist';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rootPath = path.resolve(__dirname, '../');
const packagesDir = 'packages';
const packagesPath = path.join(rootPath, packagesDir);

const packages = fs.readdirSync(packagesPath);

const {
version,
docker: { registry },
} = createRequire(import.meta.url)('../package.json');
homepage,
} = createRequire(`${rootPath}/`)('./package.json');
const { version } = createRequire(`${rootPath}/`)('./lerna.json');

const args = minimist(process.argv.slice(2), { alias: { t: 'tag', v: 'verbose' } });
const target = `${args._[0]}`;
const tag = args.tag || 'latest';

const run = (bin, args, opts = {}) =>
const exec = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', cwd: rootPath, ...opts });

async function main() {
const imageInfo = (target, tag) => {
if (!packages.includes(target)) {
throw new Error(`Invalid target: '${target}'`);
}
Expand All @@ -34,23 +32,56 @@ async function main() {
const currentImage = `${packageName}:v${version}`;
const tagImage = `${packageName}:${tag}`;

const dockerArgs = [
'build',
'.',
'-f',
`${targetPath}/Dockerfile`,
'-t',
currentImage,
'-t',
tagImage,
];
if (args.verbose) {
dockerArgs.push('--progress', 'plain');
}
await run('docker', dockerArgs);
}
return { packageName, currentImage, tagImage };
};

main().catch((error) => {
console.error(error);
process.exit(1);
});
yargs(hideBin(process.argv))
.command(
'build <target>',
'build docker image',
{
tag: { alias: 't', default: 'latest' },
verbose: { alias: 'v', type: 'boolean' },
},
async (args) => {
const { target, tag, verbose } = args;
const { currentImage, tagImage } = imageInfo(target, tag);
const dockerArgs = [
'build',
'.',
'--target',
target,
'--label',
`org.opencontainers.image.source=${homepage}`,
'-t',
currentImage,
'-t',
tagImage,
];
if (verbose) {
dockerArgs.push('--progress', 'plain');
}
console.log('Running docker:', dockerArgs.join(' '));
await exec('docker', dockerArgs);
},
)
.command(
'push <target>',
'push docker image',
{
tag: { alias: 't', default: 'latest' },
verbose: { alias: 'v', type: 'boolean' },
},
async (args) => {
const { target, tag, verbose } = args;
const { packageName } = imageInfo(target, tag);
const dockerArgs = ['push', packageName, '-a'];
if (verbose) {
dockerArgs.push('--progress', 'plain');
}
console.log('Running docker:', dockerArgs.join(' '));
await exec('docker', dockerArgs);
},
)
.help()
.parse();

0 comments on commit 63933d4

Please sign in to comment.