简体   繁体   中英

Docker best practice nodejs and static file

Actually to create my dockerfile for node js, i use the following command on a nodejs base image :

RUN \
    apt-get update && \
    apt-get install -y --no-install-recommends git python build-essential libpng-dev && \
    apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install production package only
COPY package.json /var/www/site

RUN \
    npm install && \
    npm cache clean

COPY . /var/www/site

RUN \
    npm run build && \
    npm run doc

RUN \
    npm prune --production && \
    npm cache clean

ENV NODE_ENV=production

I'm not happy with this method because i need to run "npm run build" that create the client part of the application (based on webpack, backbone, marionnette).

As i run the "npm run build" command i must install the developpement package and not only the production package, and some debian package to minimize image.

I would like that the image only contains the production package and not dev stuff.

In another hand, if i put only productions files, i must generate the client code on the build machine. So i must have nodejs and all necessary package installed on the host machine. I prefere build all with docker.

In a second time i would like to serve static file directly from nginx and not from the nodejs component. But i don't want install node on the nginx image.

What is the base practice of docker ? Build all in the docker to have reproductible image ? Build on the host and package in docker only the result ?

Thanks

I think the best way would be to use two steps, because using one build is hard and the image would not be clean in most cases.

The first conatiner would have the sources in a volume and builds the static files on your machine (or CI). You wouldn't have to install anything else and it would stay reproducible.

Then you can use the static files to build a nginx image.

You can use eg a Makefile, so you only have to type in one short command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM