1

I need to dockerize a nodejs application that uses webpack. I have this script below:

"scripts": {
    "start": "cross-env NODE_ENV=development webpack-dev-server --open webpack --env.envConfig=hello --config configs/webpack.dev.js ",
    "build-dev": "cross-env NODE_ENV=development webpack --env.project=rm --env.envConfig=hello --env.publicPath=/ --config configs/webpack.dev.js",
    "build": "cross-env NODE_ENV=production webpack --env.project=rm --env.envConfig=hello --env.publicPath=/ --config configs/webpack.prod.js",
  },

This is my docker code snippet

FROM node:10-alpine as builder

# copy the package.json to install dependencies
COPY . /build

WORKDIR /build

# Install the dependencies and make the folder
RUN npm install && npm run build

However, I want to choose between npm run build and npm run build-dev whenever I execute docker run. Is it possible? If not, can I pass the webpack options on docker run instead?

2
  • no, you cannot choose between them. the image is built with those commands. but what you can do is create later which runs a command, and another later for another command. sometimes it is used as a build layer and a code execution. sometimes a layer to run tests and so on. Commented Oct 15, 2020 at 8:03
  • You can run any command after the docker image is built... but the files will only exist while that container exists. docker run npm build docker run npm build-dev. Maybe you want to choose which image to build? rather than what command to run after the image is built. Commented Oct 15, 2020 at 8:10

2 Answers 2

1

The container image can be selectively built with a build argument using --build-arg

FROM node:10-alpine as builder

# copy the package.json to install dependencies
COPY . /build

WORKDIR /build

ARG BUILD_TYPE build

# Install the dependencies and make the folder
RUN set -uex; \
    npm install; \
    npm run $BUILD_TYPE
$ docker build --build-arg BUILD_TYPE=build-dev --tag me/app:build-dev .
$ docker build --tag me/app .
Sign up to request clarification or add additional context in comments.

Comments

1

no, you cannot choose between them. the image is built with those commands. but what you can do is create layer which runs a different command.

sometimes it is used as a build layer, sometimes a layer to run tests and so on.

a sample of a multilayer build is this one:

https://github.com/BretFisher/docker-mastery-for-nodejs/blob/master/multi-stage-test/Dockerfile

you can use docker-compose to run a specific layer:

version: "3.7"


services:

  my-service:
    build:
      dockerfile: ./Dockerfile
      target: dev
    image: my-service

you can change the target to be the layer that you want to run

Comments

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.