1

I'm using Docker containers to run my CRA application. I use 2 different DockerFile, one for running CRA in development and one to generate the build. 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.

I had search the solution everywhere, but I didn't find anything to resolve this problem.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Alex Ferreli
  • 548
  • 1
  • 7
  • 14
  • 2
    I'd recommend doing things in the opposite order: run `yarn upgrade` on your host, run unit tests, and then run `docker build` to build a new image. (Shouldn't that Dockerfile have a `COPY . .` line to copy the application into the image?) – David Maze Jan 12 '21 at 21:05

3 Answers3

1

docker cp, for example docker cp [running container id]:/home/node/code/yarn.lock yarn.lock. You can also use volumes rather than copying things in and out.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
0

I stumbled my way here, a possible solution is to use:

RUN yarn install --frozen-lockfile

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I solved it moving it into a folder and syncing it using a volume (examples include only relevant code for this case).

File ./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:

#!/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:

    volumes:
        - "../backend/sync-package-lock:/app/sync-package-lock"
Rashomon
  • 5,962
  • 4
  • 29
  • 67