简体   繁体   English

在 docker 容器中安装依赖项后如何复制回主机 package-lock.json/yarn.lock?

[英]How to copy back to host package-lock.json/yarn.lock after install dependencies inside docker container?

I'm using Docker containers to run my CRA application.我正在使用 Docker 容器来运行我的 CRA 应用程序。 I use 2 different DockerFile, one for running CRA in development and one to generate the build.我使用 2 个不同的 DockerFile,一个用于在开发中运行 CRA,另一个用于生成构建。 During the step of installing dependencies:在安装依赖项的步骤中:

FROM node:15.5.0-alpine3.10

USER node

RUN mkdir /home/node/code
WORKDIR /home/node/code

COPY package.json yarn.lock ./
RUN yarn

ENV PATH /home/node/code/node_modules/.bin:$PATH

CMD yarn start

I need to copy my updated yarn.lock file (or package-lock.json file is using NPM) back to host after the container generate the new version of the file.在容器生成文件的新版本后,我需要将更新的 yarn.lock 文件(或 package-lock.json 文件正在使用 NPM)复制回主机。

I had search the solution everywhere, but I didn't find anything to resolve this problem.我到处搜索解决方案,但没有找到任何解决此问题的方法。

I'm using Docker containers to run my CRA application.我正在使用 Docker 容器来运行我的 CRA 应用程序。 I use 2 different DockerFile, one for running CRA in development and one to generate the build.我使用 2 种不同的 DockerFile,一种用于在开发中运行 CRA,另一种用于生成构建。 During the step of installing dependencies:在安装依赖项的步骤中:

FROM node:15.5.0-alpine3.10

USER node

RUN mkdir /home/node/code
WORKDIR /home/node/code

COPY package.json yarn.lock ./
RUN yarn

ENV PATH /home/node/code/node_modules/.bin:$PATH

CMD yarn start

I need to copy my updated yarn.lock file (or package-lock.json file is using NPM) back to host after the container generate the new version of the file.在容器生成文件的新版本后,我需要将更新的 yarn.lock 文件(或 package-lock.json 文件正在使用 NPM)复制回主机。

I had search the solution everywhere, but I didn't find anything to resolve this problem.我到处搜索解决方案,但没有找到任何解决此问题的方法。

I stumbled my way here, a possible solution is to use:我在这里偶然发现,一个可能的解决方案是使用:

RUN yarn install --frozen-lockfile运行纱线安装 --frozen-lockfile

Didn't find any way to copy back the package-lock.json / yarn.lock file after install dependencies inside docker container.在 docker 容器中安装依赖项后,没有找到任何方法来复制回package-lock.json / yarn.lock文件。

But I solved it moving it into a folder and syncing it using a volume (examples include only relevant code for this case).但我解决了将它移动到一个文件夹中并使用卷同步它的问题(示例仅包含与此案例相关的代码)。

File ./Dockerfile :文件./Dockerfile

FROM node:12-alpine

# Copy all files inside sync-package-lock into workdir of docker 
COPY sync-package-lock ./

# If package-lock.json is not found in previous step, it will be created in the following npm install
RUN npm install
RUN npm run build

# Following entrypoint will copy the package-lock.json to the sync volume
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

ENTRYPOINT [ "/docker-entrypoint.sh" ]
CMD [ "npm", "run", "serve" ]

File ./docker-entrypoint.sh :文件./docker-entrypoint.sh

#!/bin/sh

# copy package-lock.json to the shared volume "sync-package-lock" so its synced between host and container
cp -a /app/package-lock.json /app/sync-package-lock/

echo "Finised docker-entrypoint.sh"

# Finish entrypoint. Run the CMD
exec "$@"

File docker-compose.yml :文件docker-compose.yml

    volumes:
        - "../backend/sync-package-lock:/app/sync-package-lock"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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