簡體   English   中英

如何在 Docker 構建中緩存 node_modules?

[英]How to cache node_modules on Docker build?

我一直在嘗試在 Docker 構建上緩存node_modules一段時間。 我嘗試了幾種方法,包括這里的一種,但沒有成功。

我緩存的主要原因是因為構建我的圖像需要30 多分鍾,這太多了。

我的Dockerfile

# This image will be based on the oficial nodejs docker image
FROM node:4.2.1

RUN npm install -g jspm@0.17.0-beta.7 && \
    npm install -g gulp && \
    npm install -g tsd

# Use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /src/package.json
RUN cd /src && npm install

# Put all our code inside that directory that lives in the container
ADD . /src

# Set in what directory commands will run
WORKDIR /src

# Install dependencies
RUN cd /src && \
    tsd reinstall -so && \
    jspm install && \
    gulp build -p

# Tell Docker we are going to use this port
EXPOSE 3000

# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]

我沒有.dockerignore文件。 我之前添加了一個,但它仍然沒有緩存我的node_modules

那么,如何緩存我的 node_modules 隨意建議修改Dockerfile

謝謝!

我不確定它是否是錯誤的根源,但嘗試在ADD命令中指定目標文件夾而不是目標文件

ADD package.json /src

此外,您可以使用COPY而不是ADDADD可以與 url 和檔案一起使用,但您在這里不需要它)。

您還可以在文件的前面指定您的工作目錄

嘗試使用此代碼:

    # This image will be based on the official nodejs docker image
    FROM node:4.2.1
    
    RUN npm install -g jspm@0.17.0-beta.7 && \
        npm install -g gulp && \
        npm install -g tsd

    # Set in what directory commands will run
    WORKDIR /src
    
    # Use changes to package.json to force Docker not to use the cache
    # when we change our application’s nodejs dependencies:
    COPY package.json ./

    RUN npm install
    
    # Put all our code inside that directory that lives in the container
    COPY . ./
    
    # Install dependencies
    RUN tsd reinstall -so && \
        jspm install && \
        gulp build -p
    
    # Tell Docker we are going to use this port
    EXPOSE 3000
    
    # The command to run our app when the container is run
    CMD ["npm", "run", "start-production"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM