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

Can't build on docker. #86

Closed
jey3dayo opened this issue Mar 4, 2020 · 10 comments
Closed

Can't build on docker. #86

jey3dayo opened this issue Mar 4, 2020 · 10 comments
Labels
needs investigation This needs to be investigated further before proceeding

Comments

@jey3dayo
Copy link

jey3dayo commented Mar 4, 2020

Description

When doing a docker build, an error occurs because there is no environment variable in the next build.
I want to next build without env.

# Dockerfile
FROM node:12.15-alpine

WORKDIR /usr/src/app
COPY package.json .
COPY yarn.lock .
RUN yarn

COPY . .

RUN yarn build

EXPOSE 3000

CMD ["yarn", "start"]
Automatically optimizing pages...
> Build error occurred
Error: A valid Auth0 Domain must be provided
    at Object.createInstance [as default] (/usr/src/app/node_modules/@auth0/nextjs-auth0/dist/instance.node.js:10:15)
    at initAuth0 (/usr/src/app/node_modules/@auth0/nextjs-auth0/dist/index.js:8:46)
    at Object.xMDF (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:781:127)
    at __webpack_require__ (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:23:31)
    at Module.1TCz (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:147:13)
    at __webpack_require__ (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:23:31)
    at Object.0 (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:99:18)
    at __webpack_require__ (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:23:31)
    at /usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:91:18
    at Object.<anonymous> (/usr/src/app/.next/server/static/GDDYxjIWKG2mlWWc4bTrE/pages/_app.js:94:10)
error Command failed with exit code 1

Environment

  • node@v12.15.0
  • @auth0/nextjs-auth0@0.10.0
  • next@9.2.2
@huang47
Copy link

huang47 commented Apr 9, 2020

when I do next build it works for me, however, when I tried to deploy to zeit.co (through now) which ends up seeing this error.

@huang47
Copy link

huang47 commented Apr 10, 2020

I used env variables via process.env and when use now it doesn't take .env into account even with dotenv-cli. hardcode it in initAuth0 which works for me tho

@huang47
Copy link

huang47 commented Apr 10, 2020

https://zeit.co/docs/v2/build-step#using-environment-variables-and-secrets

environment variables for build time needs now.json plus now secrets once you have those configured in docker file it should work fine

@tomclark
Copy link

tomclark commented Jun 5, 2020

I recently encountered this problem too. I'm deploying to a bare metal Kubernetes cluster, not Zeit/Vercel, so I don't have the option of creating environment variables there (if I understand @huang47 correctly). I cheated and created a .env.staging file which I copy to .env as part of my Docker build for my staging environment. When I get to production, I'll need a .env.production, meaning I end up with Docker images for staging and production. Not ideal, but it solves the immediate problem.

@tomclark
Copy link

tomclark commented Jun 6, 2020

I've noticed something that may be useful. I'm deploying to a Kubernetes cluster, and as long as the relevant environment variables are present and correct in the application deployment, they'll override whatever was in the .env file used during the build. So, you can just copy your dev .env file (or some template) into the Docker build to get you through the build stage without the error, and then when you run the container, the environment variables set in the environment will take precedence.

My Dockerfile looks like this:

FROM node:14.4.0-stretch as builder

WORKDIR /app

COPY src .
COPY next.config.js .
COPY package.json .
COPY .env.template .env

RUN yarn install
RUN yarn build

FROM node:14.4.0-stretch
WORKDIR /app

COPY --from=builder /app .

EXPOSE 3000

CMD ["node_modules/.bin/next", "start"]

My .env.template file looks like this:

AUTH0_DOMAIN=domain.eu.auth0.com
AUTH0_CLIENT_ID=auth0clientidauth0clientidauth0c
AUTH0_CLIENT_SECRET=abcdefghilklmnopqrstuvwxyz123456790abdefghijklmnopqrstuvwxyz0123
AUTH0_SCOPE=scopes
AUTH0_AUDIENCE=https://api
REDIRECT_URI=https://site/api/callback
POST_LOGOUT_REDIRECT_URI=https://site/logout
SESSION_COOKIE_SECRET=abcdefghilklmnopqrstuvwxyz123456790abdefghijklmnopqrstuvwxyz012
SESSION_COOKIE_LIFETIME=86400

And I mean actually like that. Those aren't dummy values for the purpose of posting on Github - that's what's actually in the file (and it's checked into source control so my CI build just works as well). The point is that it gets through the Docker build. These values will never be actually used because they'll all be set in the environment (and actually, if they're not set, the app won't work and won't pass smoke tests so will alert me to missing values).

My Kubernetes deployment looks something like this:

spec:
    containers:
        - image: registry.gitlab.com/myaccount/myproject/mynextclient:latest
          imagePullPolicy: Always
          name: web-client
          ports:
            - containerPort: 3000
          env:
            - name: AUTH0_CLIENT_ID
              value: 'l2wMlA2sxAs697kaZ1pWkpA5UHODwsNj'
            - name: AUTH0_DOMAIN
              value: 'mydomain.eu.auth0.com'

In this example, mydomain.eu.auth0.com is a dummy value for posting this answer. The actual value is my Auth0 domain. If you're running the image through Docker, I assume you could just pas the correct environment variables through the command line, or in the environment section of a Compose file.

I expect there's a more correct way of doing this, but it works for my purposes. I hope it's of some help.

@jey3dayo
Copy link
Author

jey3dayo commented Jun 8, 2020

The way I used the dummy env is a good idea.
I was able to build it with docker-compose.
Thank you for the information.

@pedropiloto
Copy link

pedropiloto commented Dec 27, 2020

I found a solution that worked for my case (I'm deploying it using caprover). Add the build and the start to the Dockerfile CMD:

CMD bash -c "yarn build && yarn start"

@jey3dayo
Copy link
Author

jey3dayo commented Jan 4, 2021

I think this looks good.

#154 (comment)

@Widcket Widcket added the needs investigation This needs to be investigated further before proceeding label Jan 18, 2021
@adamjmcgrath
Copy link
Contributor

Closing this in favour of #154 - see #154 (comment)

@aimproxy
Copy link

aimproxy commented Dec 19, 2022

@tomclark How do you exactly set the AUTH0_BASE_URL under your k8s deployment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation This needs to be investigated further before proceeding
Projects
None yet
Development

No branches or pull requests

7 participants