Docker and symlinks

前端 未结 4 1219
别那么骄傲
别那么骄傲 2020-12-23 16:28

I\'ve got a repo set up like this:

/config
   config.json
/worker-a
   Dockerfile
   
   /code
/worker-b
   Dockerfile
   

        
4条回答
  •  旧巷少年郎
    2020-12-23 16:46

    I also ran into this problem, and would like to share another method that hasn't been mentioned above. Instead of using npm link in my Dockerfile, I used yalc.

    1. Install yalc in your container, e.g. RUN npm i -g yalc.
    2. Build your library in Docker, and run yalc publish (add the --private flag if your shared lib is private). This will 'publish' your library locally.
    3. Run yalc add my-lib in each repo that would normally use npm link before running npm install. It will create a local .yalc folder in your Docker container, create a symlink in node_modules that works inside Docker to this folder, and rewrite your package.json to refer to this folder too, so you can safely run install.
    4. Optionally, if you do a two stage build, make sure that you also copy the .yalc folder to your final image.

    Below an example Dockerfile, assuming you have a mono repository with three packages: models, gui and server, and the models repository must be shared and named my-models.

    # You can access the container using:
    #   docker run -it my-name sh
    # To start it stand-alone:
    #   docker run -it -p 8888:3000 my-name
    
    FROM node:alpine AS builder
    # Install yalc globally (the apk add... line is only needed if your installation requires it)
    RUN apk add --no-cache --virtual .gyp python make g++ && \
      npm i -g yalc
    RUN mkdir /packages && \
      mkdir /packages/models && \
      mkdir /packages/gui && \
      mkdir /packages/server
    COPY ./packages/models /packages/models
    WORKDIR /packages/models
    RUN npm install && \
      npm run build && \
      yalc publish --private
    COPY ./packages/gui /packages/gui
    WORKDIR /packages/gui
    RUN yalc add my-models && \
      npm install && \
      npm run build
    COPY ./packages/server /packages/server
    WORKDIR /packages/server
    RUN yalc add my-models && \
      npm install && \
      npm run build
    
    FROM node:alpine
    RUN mkdir -p /app
    COPY --from=builder /packages/server/package.json /app/package.json
    COPY --from=builder /packages/server/dist /app/dist
    # Make sure you copy the yalc registry too.
    COPY --from=builder /packages/server/.yalc /app/.yalc
    COPY --from=builder /packages/server/node_modules /app/node_modules
    COPY --from=builder /packages/gui/dist /app/dist/public
    WORKDIR /app
    EXPOSE 3000
    CMD ["node", "./dist/index.js"]
    

    Hope that helps...

提交回复
热议问题