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
[New Example] with docker - multiple deployment environments #34015
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nguyenyou
requested review from
ijjk,
leerob,
shuding,
steven-tey and
timneutkens
as code owners
February 5, 2022 07:42
This is great 😄 Any reason for closing? Happy to review and merge. |
@leerob I'm glad to here that 😄 I closed it because I'm not so confident that this example will be reviewed and merged. 😅 |
leerob
previously approved these changes
Feb 5, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me!
@leerob I just fixed the linter error 🥲 |
leerob
approved these changes
Feb 5, 2022
natew
pushed a commit
to natew/next.js
that referenced
this pull request
Feb 16, 2022
…34015) ## Documentation / Examples - [x] Make sure the linting passes by running `yarn lint` --- ## Context Having 3 environments: - Development: for doing testing - Staging: for doing UAT testing - Production: for users In each environment, the Next.js application makes API calls to the corresponding API gateway: - Development: https://api-development.com - Staging: https://api-staging.com - Production: https://api-production.com Using `NEXT_PUBLIC_API_URL` for the `baseUrl` of [axios](https://axios-http.com/docs/intro). Since the `NEXT_PUBLIC_API_URL` is replaced during _build time_, we have to manage to provide the corresponding `.env.production` files for Docker at _build time_ for each environment. ## Solution Since we are using CI services for dockerization, we could setup the CI to inject the correct `.env.production` file into the cloned source code, (this is actually what we did). Doing that would require us to touch the CI settings. Another way is using multiple Dockerfile (the former only need to use one Dockerfile), and the trick is copying the corresponding `env*.sample` and rename it to `.env.production` then putting it into the Docker context. Doing this way, everything is managed in the source code. ``` > Dockerfile # Development environment COPY .env.development.sample .env.production # Staging environment COPY .env.staging.sample .env.production # Production environment COPY .env.production.sample .env.production ``` Testing these images locally is also simple, by issuing the corresponding Makefile commands we can simulate exactly how the image will be built in the CI environment. ## How to use For development environment: ``` make build-development make start-development ``` For staging environment: ``` make build-staging make start-staging ``` For production environment: ``` make build-production make start-production ``` ## Conclusion This example shows one way to solve the three-environment model in software development when building a Next.js application. There might be another better way and I would love to know about them as well. I'm making this example because I can't find any example about this kind of problem. Co-authored-by: Tú Nguyễn <93700515+tunguyen-ct@users.noreply.github.com>
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Documentation / Examples
yarn lint
Context
Having 3 environments:
In each environment, the Next.js application makes API calls to the corresponding API gateway:
Using
NEXT_PUBLIC_API_URL
for thebaseUrl
of axios.Since the
NEXT_PUBLIC_API_URL
is replaced during build time, we have to manage to provide the corresponding.env.production
files for Docker at build time for each environment.Solution
Since we are using CI services for dockerization, we could setup the CI to inject the correct
.env.production
file into the cloned source code, (this is actually what we did). Doing that would require us to touch the CI settings.Another way is using multiple Dockerfile (the former only need to use one Dockerfile), and the trick is copying the corresponding
env*.sample
and rename it to.env.production
then putting it into the Docker context. Doing this way, everything is managed in the source code.Testing these images locally is also simple, by issuing the corresponding Makefile commands we can simulate exactly how the image will be built in the CI environment.
How to use
For development environment:
For staging environment:
For production environment:
Conclusion
This example shows one way to solve the three-environment model in software development when building a Next.js application. There might be another better way and I would love to know about them as well.
I'm making this example because I can't find any example about this kind of problem.