2

I'm trying to create a Dockerfile to deploy a Vue.js app as a docker in Cloud Run (GCP). From the Vue official documentation and get a Dockerfile to deploy the app in a "production mode":

FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 8080
CMD [ "http-server", "dist" ]

It works properly but I want to modify the Dockerfile to make it dynamic. I mean, I want that this file is able to deploy either to production or to DEV using a kind of input parameter

I've tried several attempts, one of those was to add these new lines

ENV ENT=production
RUN NODE_ENV=${ENT} npm run build

With the intension to define a default value for the environment variable as "production", and in the Cloud Run service define the same environment variable as "dev". I thought the variable defined on the cloud run service will overwrite the local variable (Dockerfile) at the moment of running the docker. It was not overwritten and again the app is running in production

Do you know if there is a way to overwrite a Dockerfile environment variable or if It is possible to create something as

npm run build:dev
npm run build:prod

dynamically depending on the environment I want to deploy?

This is my package.json

{
  /// ...
  "scripts": {
    "dev": "vue-cli-service serve --mode development",
    "test": "cross-env NODE_ENV=testing vue-cli-service serve --mode testing",
    "prod": "vue-cli-service serve --mode production",
    "build:dev": "vue-cli-service build --mode development",
    "build": "vue-cli-service build --mode production",
    "lint": "vue-cli-service lint"
  },
  /// ...
}
2
  • can you please share package json Commented Jun 5, 2020 at 17:58
  • shared. I think scripts part is enough Commented Jun 5, 2020 at 18:12

2 Answers 2

1

Based on the Muni Kumar's answer, I found a way to do it because it is a little bit tricky the tools to deploy a container in GCP. Cloud build to create a docker image and Cloud Run to deploy it

in scripts:

"build:dev": "vue-cli-service build --mode development",
"build:prod": "vue-cli-service build --mode production",

in Dockerfile:

ARG ENV
RUN npm run build:${ENV}

new file called: cloudbuild.yaml to add some custom configuration to Cloud Build

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build',
         '--build-arg',
        'ENV=${_ENV}',
         '-t',
         'gcr.io/$PROJECT_ID/landing',
         '.']
substitutions:
    _ENV: test # default value
images: [
    'gcr.io/$PROJECT_ID/landing',
]

and, create run next command

gcloud builds submit --config=cloudbuild.yaml --substitutions=_ENV="prod"
Sign up to request clarification or add additional context in comments.

Comments

0

1st way:

Pass docker arguments

ARG NODE_ENV 
ENV NODE_ENV $NODE_ENV
RUN /bin/bash -c '[[ "${NODE_ENV}" == "production" ]] && npm run build:prod || npm run build:dev'

2nd way:

pass arguments to the npm scripts

in scripts

  "build": "vue-cli-service build --mode production $npm_config_environment"

In docker file

ARG NODE_ENV 
RUN npm run build --environment "$NODE_ENV"

while building docker

docker build --build-arg NODE_ENV=production

6 Comments

Neither both worked but I think it is due to the hosting provider. In the first case, the Cloud Run service always run "npm run build:dev" no matter if I set "production" as a env variable. In the second case, variable "npm_config_environment" is like was not recognized
There were no errors. Condition "${NODE_ENV}" == "production" were never True. I set this env variable in Cloud Run but it did not work.
I asked about this npm_config_environment. this is a args for node scripts. It should work. If not we need to replace that with process.env.npm_config_environment
The error was: The command '/bin/sh -c npm run build --environment $NODE_ENV' returned a non-zero code: 1 ERROR due to vue-cli-service build --mode production $npm_config_environment "production". It just works modifying the Dockerfile RUN npm run build --environment $NODE_ENV, the package "build": "vue-cli-service build --mode", and creating the file cloudbuild.yaml
I got your point friend, but my question is why it is failing? I used the above two methods to build vue docker with dynamic environment 1 year back. These are primitives. I can understand you are using yaml to build docker. passing arguments may be different but core concept is same. If you have npm log please share. That might be useful for others.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.