Skip to content

Commit 63933d4

Browse files
authored
fix(docker): use lerna version + combined dockerfile (#14)
1 parent 1940a88 commit 63933d4

File tree

7 files changed

+91
-63
lines changed

7 files changed

+91
-63
lines changed

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:20-slim AS base
2+
3+
ENV PNPM_HOME="/pnpm"
4+
ENV PATH="$PNPM_HOME:$PATH"
5+
6+
RUN corepack enable
7+
8+
FROM base AS build
9+
10+
COPY . /usr/src/app
11+
WORKDIR /usr/src/app
12+
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
13+
RUN pnpm build
14+
RUN pnpm deploy --filter=playground --prod /prod/playground
15+
RUN ls -al /prod/playground
16+
17+
FROM nginx:stable AS playground
18+
19+
COPY packages/playground/.docker/nginx.conf /etc/nginx/conf.d/default.conf
20+
COPY packages/playground/.docker/expires.conf /etc/nginx/conf.d/expires.conf
21+
22+
WORKDIR /app
23+
COPY --from=build /prod/playground/dist /app

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
version: '3.7'
2-
31
services:
42
playground:
53
build:
64
context: .
7-
dockerfile: packages/playground/Dockerfile
5+
target: playground
86
ports:
97
- '8080:80'
108
tty: true

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "monorepo-typescript",
33
"private": true,
44
"author": "Coldrun <info@coldrun.com>",
5+
"homepage": "https://github.com/coldrun/monorepo-typescript",
56
"type": "module",
67
"packageManager": "pnpm@9.12.1",
78
"engineStrict": true,
@@ -33,12 +34,12 @@
3334
"globals": "^15.9.0",
3435
"just-pnpm": "^1.0.2",
3536
"lint-staged": "^15.2.10",
36-
"minimist": "^1.2.8",
3737
"prettier": "^3.3.3",
3838
"rimraf": "^6.0.1",
3939
"simple-git-hooks": "^2.11.1",
4040
"typescript": "~5.5.4",
41-
"typescript-eslint": "^8.5.0"
41+
"typescript-eslint": "^8.5.0",
42+
"yargs": "^17.7.2"
4243
},
4344
"simple-git-hooks": {
4445
"pre-commit": "pnpm lint-staged"

packages/playground/Dockerfile

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "vite",
88
"build": "tsc && vite build",
9-
"build:docker": "node ../../scripts/docker.js playground",
9+
"build:docker": "node ../../scripts/docker build playground",
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/docker.js

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@ import { createRequire } from 'node:module';
33
import path from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import { execa } from 'execa';
6-
import minimist from 'minimist';
6+
import yargs from 'yargs';
7+
import { hideBin } from 'yargs/helpers';
78

89
const __dirname = path.dirname(fileURLToPath(import.meta.url));
910
const rootPath = path.resolve(__dirname, '../');
1011
const packagesDir = 'packages';
1112
const packagesPath = path.join(rootPath, packagesDir);
12-
1313
const packages = fs.readdirSync(packagesPath);
14+
1415
const {
15-
version,
1616
docker: { registry },
17-
} = createRequire(import.meta.url)('../package.json');
17+
homepage,
18+
} = createRequire(`${rootPath}/`)('./package.json');
19+
const { version } = createRequire(`${rootPath}/`)('./lerna.json');
1820

19-
const args = minimist(process.argv.slice(2), { alias: { t: 'tag', v: 'verbose' } });
20-
const target = `${args._[0]}`;
21-
const tag = args.tag || 'latest';
22-
23-
const run = (bin, args, opts = {}) =>
21+
const exec = (bin, args, opts = {}) =>
2422
execa(bin, args, { stdio: 'inherit', cwd: rootPath, ...opts });
2523

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

37-
const dockerArgs = [
38-
'build',
39-
'.',
40-
'-f',
41-
`${targetPath}/Dockerfile`,
42-
'-t',
43-
currentImage,
44-
'-t',
45-
tagImage,
46-
];
47-
if (args.verbose) {
48-
dockerArgs.push('--progress', 'plain');
49-
}
50-
await run('docker', dockerArgs);
51-
}
35+
return { packageName, currentImage, tagImage };
36+
};
5237

53-
main().catch((error) => {
54-
console.error(error);
55-
process.exit(1);
56-
});
38+
yargs(hideBin(process.argv))
39+
.command(
40+
'build <target>',
41+
'build docker image',
42+
{
43+
tag: { alias: 't', default: 'latest' },
44+
verbose: { alias: 'v', type: 'boolean' },
45+
},
46+
async (args) => {
47+
const { target, tag, verbose } = args;
48+
const { currentImage, tagImage } = imageInfo(target, tag);
49+
const dockerArgs = [
50+
'build',
51+
'.',
52+
'--target',
53+
target,
54+
'--label',
55+
`org.opencontainers.image.source=${homepage}`,
56+
'-t',
57+
currentImage,
58+
'-t',
59+
tagImage,
60+
];
61+
if (verbose) {
62+
dockerArgs.push('--progress', 'plain');
63+
}
64+
console.log('Running docker:', dockerArgs.join(' '));
65+
await exec('docker', dockerArgs);
66+
},
67+
)
68+
.command(
69+
'push <target>',
70+
'push docker image',
71+
{
72+
tag: { alias: 't', default: 'latest' },
73+
verbose: { alias: 'v', type: 'boolean' },
74+
},
75+
async (args) => {
76+
const { target, tag, verbose } = args;
77+
const { packageName } = imageInfo(target, tag);
78+
const dockerArgs = ['push', packageName, '-a'];
79+
if (verbose) {
80+
dockerArgs.push('--progress', 'plain');
81+
}
82+
console.log('Running docker:', dockerArgs.join(' '));
83+
await exec('docker', dockerArgs);
84+
},
85+
)
86+
.help()
87+
.parse();

0 commit comments

Comments
 (0)