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

[New Example] with docker - multiple deployment environments #34015

Merged
merged 2 commits into from Feb 5, 2022
Merged

[New Example] with docker - multiple deployment environments #34015

merged 2 commits into from Feb 5, 2022

Conversation

nguyenyou
Copy link
Contributor

@nguyenyou nguyenyou commented Feb 5, 2022

Documentation / Examples

  • 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:

Using NEXT_PUBLIC_API_URL for the baseUrl 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.

> 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.

@ijjk ijjk added the examples Issue/PR related to examples label Feb 5, 2022
@nguyenyou nguyenyou closed this Feb 5, 2022
@leerob
Copy link
Member

leerob commented Feb 5, 2022

This is great 😄 Any reason for closing? Happy to review and merge.

@nguyenyou nguyenyou reopened this Feb 5, 2022
@nguyenyou
Copy link
Contributor Author

nguyenyou commented Feb 5, 2022

@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
leerob previously approved these changes Feb 5, 2022
Copy link
Member

@leerob leerob left a 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!

@nguyenyou
Copy link
Contributor Author

@leerob I just fixed the linter error 🥲

@nguyenyou nguyenyou requested a review from leerob February 5, 2022 16:41
@kodiakhq kodiakhq bot merged commit 3cd927e into vercel:canary Feb 5, 2022
@nguyenyou nguyenyou deleted the with-docker-multi-env branch February 5, 2022 18:14
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>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants