Historically, Docker has been a popular choice for containerizing applications. In the cloud native era, there is another popular player for performing app image build and getting lots of attention in devops community. It's quite easy to use cloud native buildpacks to containerize the app. There is no specific file or config required in most of the cases. Cloud native buildpacks supports all the popular coding languages and work smoothly to identify the language based on the source-code to containerize app image.
Post data is missing
In this blog we would go through the steps to containerize the Next.js based webapp using simple steps. For building Next.js app image with Docker build, please have a look into another blog - How to containerize Next.js app with docker build.
Initial setup
We can build a Next.js based webapp with npx create-next-app@latest. We can extend the web app with more functionalities and API's.
Based of the usecase, developer can build a standalone output for the Next.js app. Related feature in Next.js for generating standalone output can be enabled in the next.config.js.
Above config will create a folder at .next/standalone which can then be deployed on its own without installing node_modules.
NOTE: Though standalone output feature is good to have for containerizing app with docker build, but for buildpacks, it's an optional step.
Introduce Pack CLI
The simplest way to use buildpacks is by installing Pack CLI in the dev environment. There is no need to install docker or any other dependency except Pack CLI for image build.
NOTE: We might need docker to perform a test-run for the built image with docker run.
Pack Build requires an image name, which will be generated from the source code. Build defaults to the current directory, but you can use --path to specify another source code directory. Build requires a builder, which can either be provided directly to build using --builder, or can be set using the set-default-builder command. To specify build time environment variables, we can pass via --env or can specify path for env-file with --env-file. All the other options can be checked with --help for pack build sub-command.
NOTE1: A builder is an image that contains all the components necessary to execute a build.
NOTE2 A buildpack is a set of executables that inspects your app source code and creates a plan to build and run your application.
Containerizing Next.js app to image is as simple as running the pack build command with certain options.
--name : The name and the tag of the app image.
--builder : The builder to use for image build.
--buildpack : The buildpack for nodejs.
We can run below command to start the image creation process.
Considerations for Next.js app
By default, buildpacks look into package.json for the build script and runs the command during build. So, to ensure that the build gets the env params from buildpack for production, after the next build we can either specify the build env param or we can add 2nd build script as "build:prod": "set NODE_ENV=production & next build".
In case we are using Next.js standalone output generation during build for getting the app deploy resources, we need to change the start script for app as "start": "node .next/standalone/server.js". If we are not using standalone output, then we do not need any change as start script can be as "start": "next start".
NOTE: Above package.json considers start script for Next.js app with standalone output.
Build process
With above considerations, we can run the pack build command and track the image generation. The build process downloads the builder image and corresponding buildpacks image in first attempt. The process identifies the supporting buildpacks needed for the app based on the source-code and configs defined. Also adds environment variables, which can be overridden with CLI.
Conclusion
We can verify that the image is created with docker images and can be tested with docker run
Hope this blog will help my friends to get the steps to build app image with buildpacks in quick and easy steps. 🙂