Docker packages an application, its code, and its dependencies into a container so it can run consistently across environments. Robinson briefly compares containers to cargo on a ship and notes that Kubernetes can orchestrate Docker containers.
Using Docker with Next.js
Lee Robinson, a developer at Vercel, explains how to package a Next.js app with Docker and deploy it to Google Cloud Run.
The Dockerfile uses three Alpine-based stages: install dependencies, build the app, and assemble a production image that starts with `yarn start`. A `.dockerignore` file excludes files that do not need to go into the container.
Robinson builds the image with `docker build` and the tag `nextjs-docker-youtube`. The first build takes about 41 seconds, while a repeat build with no changes takes about 0.5 seconds because Docker reuses cached steps. He runs the app on port 3000, passing `PORT=3000` as an environment variable.
For deployment, Robinson installs the Google Cloud SDK, creates or selects a project, signs in with `gcloud auth login`, and enables billing. He uses Cloud Build to create the image, then deploys it to Cloud Run in `us-central1`, allows the requested permissions, and gets a URL for the running Next.js app.
Takeaways
- Use multi-stage Docker builds to keep the production image focused and reuse cached build steps.
- Add a `.dockerignore` file to keep unnecessary files out of the container.
- Pass the port through an environment variable so the app can use the port expected by its deployment environment.
- Cloud Run deployment requires a Google Cloud project with billing enabled and the image built in Cloud Build.
Mentioned in this talk
Search inside this talk
What was claimed 7
- featureDocker: Uses containers to package applications and dependencies for consistent deployment.
“Containers allow a developer to package up their application”
YouTube Unverified1 source· Talk
- usesKubernetes uses Docker
“a container orchestration system that can use Docker”
YouTube Unverified1 source· Talk
- featureDocker multi-stage build: Reuses unchanged build stages and dependencies to make rebuilds faster.
“0.5 seconds to rebuild this Docker container, whereas initially it took about 41 seconds”
YouTube Unverified1 source· Talk
- uses
Cloud Run uses
Google Cloud“Google Cloud Run, which is part of GCP, or Google Cloud Platform”
YouTube Unverified1 source· Talk
- usesGoogle Cloud Build uses Docker
“build the container image of our application using Google Cloud Build”
YouTube Unverified1 source· Talk
- uses
“build that Docker container in the cloud”
YouTube Unverified1 source· Talk
- uses
“deploy our container to Google Cloud Run”
YouTube Unverified1 source· Talk
Related talks
45:12
59:33
17:51
21:38
14:29
9:32
