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

Optimize docker memory #976

Merged
merged 3 commits into from
Dec 22, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Optimized Docker container to run JS code directly with node instead of yarn, npm and tsx. Reduces memory usage.

## [2.4.3] - 2023-09-10

### Fixed
Expand Down
19 changes: 15 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@ COPY package.json .
COPY yarn.lock .

RUN yarn install --prod
RUN cp -R node_modules /usr/app/prod_node_modules
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend doing yarn install (all dependencies) in this dependencies layer, then not copying node_modules and instead running yarn install --prod in the runner image to simplify it a little

Copy link
Contributor Author

@ninele7 ninele7 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done it to avoid downloading prod dependencies twice. That way it seems to work faster. But if you prefer calling downloading twice I will change it.


RUN yarn install

FROM dependencies AS builder

COPY . .

# Run tsc build
RUN yarn prisma generate
RUN yarn build

# Only keep what's necessary to run
FROM base AS runner

WORKDIR /usr/app

COPY --from=dependencies /usr/app/node_modules node_modules
COPY --from=builder /usr/app/dist ./dist
COPY --from=dependencies /usr/app/prod_node_modules node_modules
COPY --from=builder /usr/app/node_modules/.prisma/client ./node_modules/.prisma/client

COPY . .

RUN yarn prisma generate

ARG COMMIT_HASH=unknown
ARG BUILD_DATE=unknown

Expand All @@ -34,4 +45,4 @@ ENV NODE_ENV production
ENV COMMIT_HASH $COMMIT_HASH
ENV BUILD_DATE $BUILD_DATE

CMD ["tini", "--", "yarn", "start"]
CMD ["tini", "--", "node", "--enable-source-maps", "dist/scripts/migrate-and-start.js"]
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"migrations:run": "npm run prisma:with-env migrate deploy",
"prisma:with-env": "npm run env:set-database-url prisma",
"env:set-database-url": "tsx src/scripts/run-with-database-url.ts",
"release": "release-it"
"release": "release-it",
"build": "tsc"
},
"devDependencies": {
"@release-it/keep-a-changelog": "^2.3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ChatInputCommandInteraction, EmbedBuilder, PermissionFlagsBits} from 'di
import {injectable} from 'inversify';
import {prisma} from '../utils/db.js';
import Command from './index.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

@injectable()
export default class implements Command {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player.js';
import Command from '.';
import {SlashCommandBuilder} from '@discordjs/builders';
import {STATUS} from '../services/player';
import {STATUS} from '../services/player.js';

@injectable()
export default class implements Command {
Expand Down
2 changes: 1 addition & 1 deletion src/events/voice-state-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import container from '../inversify.config.js';
import {TYPES} from '../types.js';
import PlayerManager from '../managers/player.js';
import {getSizeWithoutBots} from '../utils/channels.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

export default async (oldState: VoiceState, _: VoiceState): Promise<void> => {
const playerManager = container.get<PlayerManager>(TYPES.Managers.Player);
Expand Down
2 changes: 1 addition & 1 deletion src/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Config from './commands/config.js';
import Disconnect from './commands/disconnect.js';
import Favorites from './commands/favorites.js';
import ForwardSeek from './commands/fseek.js';
import Loop from './commands/loop';
import Loop from './commands/loop.js';
import Move from './commands/move.js';
import Next from './commands/next.js';
import NowPlaying from './commands/now-playing.js';
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/migrate-and-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import Prisma from '@prisma/client';
import ora from 'ora';
import {startBot} from '../index.js';
import logBanner from '../utils/log-banner.js';
import {createDatabasePath} from '../utils/create-database-url.js';
import createDatabaseUrl, {createDatabasePath} from '../utils/create-database-url.js';
import {DATA_DIR} from '../services/config.js';

const client = new Prisma.PrismaClient();

process.env.DATABASE_URL = process.env.DATABASE_URL ?? createDatabaseUrl(DATA_DIR);

const migrateFromSequelizeToPrisma = async () => {
await execa('prisma', ['migrate', 'resolve', '--applied', '20220101155430_migrate_from_sequelize'], {preferLocal: true});
};
Expand Down
2 changes: 1 addition & 1 deletion src/services/add-query-to-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {SongMetadata, STATUS} from './player.js';
import PlayerManager from '../managers/player.js';
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
import {getMemberVoiceChannel, getMostPopularVoiceChannel} from '../utils/channels.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

@injectable()
export default class AddQueryToQueue {
Expand Down
2 changes: 1 addition & 1 deletion src/services/get-songs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {inject, injectable} from 'inversify';
import * as spotifyURI from 'spotify-uri';
import {SongMetadata, QueuedPlaylist, MediaSource} from './player';
import {SongMetadata, QueuedPlaylist, MediaSource} from './player.js';
import {TYPES} from '../types.js';
import ffmpeg from 'fluent-ffmpeg';
import YoutubeAPI from './youtube-api.js';
Expand Down
2 changes: 1 addition & 1 deletion src/services/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@discordjs/voice';
import FileCacheProvider from './file-cache.js';
import debug from '../utils/debug.js';
import {getGuildSettings} from '../utils/get-guild-settings';
import {getGuildSettings} from '../utils/get-guild-settings.js';

export enum MediaSource {
Youtube,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/get-guild-settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Setting} from '@prisma/client';
import {prisma} from './db';
import {createGuildSettings} from '../events/guild-create';
import {prisma} from './db.js';
import {createGuildSettings} from '../events/guild-create.js';

export async function getGuildSettings(guildId: string): Promise<Setting> {
const config = await prisma.setting.findUnique({where: {guildId}});
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"esModuleInterop": true,
"sourceMap": true,
"resolveJsonModule": true,
"noEmit": true
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
Loading