NodeJS webpack建设在docker容器中

时间:2017-08-13 05:42:27

标签: node.js caching docker webpack yarnpkg

我读到了关于long-term caching的内容,并尝试在我的项目中实现这一点,但是当我尝试在docker容器中构建它时,清单文件会生成错误的资产链接,但生成过程运行良好。

Dockerfile.web

FROM node:8.2.1-alpine

WORKDIR /web

ADD /tmp/app.tar.gz /web

# At the end node_modules will be removed because of bug with npm prune.
# In this case we need re-install production-only deps to reduce container weight.
RUN yarn install && \
yarn run build-production && \
rm -rf node_modules && \
yarn install --production && \
rm -rf src

RUN adduser -D mySecretUser
USER mySecretUser

任何人都知道它是什么,为什么在docker容器中构建不同?

我尝试删除所有图片,关闭docker容器缓存,在生成之前删除dist目录 - 不起作用。

1 个答案:

答案 0 :(得分:0)

我发现问题是由我的建筑脚本引入的,该脚本使用了docker-compose。

<强>多克尔-compose.yml

version: '3'
services:

  # Web-server which is responsible for server-side rendering.
  # It also add additional middleware layers when proxify requests
  # for dependent systems such as API (for example enrich with auth data before sending).
  web:
    build:
      context: .
      dockerfile: ./docker/Dockerfile.web
    ports:
      - "${WEBSERVER_PORT}"
    env_file:
      - ./docker/web.env
    volumes:
      - assets:/web/dist/assets
    command: ["yarn", "run", "_production"]

  # Nginx used as proxy-wrapper which is placed before web-server.
  # It's resposible for static files and proxy to web-server.
  # It also can be used for proxify websocket connection to specific game server.
  nginx:
    build:
      context: .
      dockerfile: ./docker/Dockerfile.nginx
    volumes:
      - assets:/www/assets
    ports:
      - "80:${NGINX_PORT}"
    env_file:
      - ./docker/web.env
      - ./nginx/nginx.env
    depends_on:
      - web
    command: ["/bin/sh", "-c", "envsubst < /etc/nginx/templates/default.template > /etc/nginx/sites-enabled/default && nginx -g 'daemon off;'"]

volumes:
  assets:

在那里我使用了共享卷assets,并且在再次启动docker-compose时我没有清除它。

我使用此命令构建:
rm -rf tmp && mkdir -p tmp && tar -czvf tmp/app.tar.gz src config .babelrc mq.json postcss.config.js webpack.*.js package.json yarn.lock && export $(cat ./docker/web.env | grep -v ^# | xargs) && docker-compose -p cruiserwars build
我在哪里准备tar.gz与源,设置环境变量,然后使用docker-compose.yml来构建我的项目。但在那里我忘了删除之前创建的音量......

所以解决方法是改为使用此命令:
rm -rf tmp && mkdir -p tmp && tar -czvf tmp/app.tar.gz src config .babelrc mq.json postcss.config.js webpack.*.js package.json yarn.lock && export $(cat ./docker/web.env | grep -v ^# | xargs) && docker-compose down -v && docker-compose -p cruiserwars build

您可以看到我添加了docker-compose down -v 来停止容器并删除之前创建的卷。