Docker compose missing yarn dependencies on build

匆匆过客 提交于 2019-12-05 20:20:34

This part of Dockerfile installs yarn packages:

RUN mkdir /myapp
WORKDIR /myapp
ADD ./package.json /myapp/
RUN yarn install

Folder /myapp is created, package.json is copied to it and yarn packages are installed. Build is successful and, of course, node_modules folder is inside built image.

But after that you start built image with:

volumes:
  - .:/myapp

which means that content of folder where docker-compose.yaml is is mounted to /myapp folder inside container, so it covers content of container's /myapp folder.

You don't need to mount current folder to container's folder to achieve what you want. Just delete it from your docker-compose.yaml:

version: '3'
services:
  web:
    build: .

Now you can:

$ docker-compose build
$ docker-compose run web bash
root@558d5b0c2ccb:/myapp# ls -la
total 268
drwxr-xr-x   3 root root   4096 Feb 23 22:25 .
drwxr-xr-x  65 root root   4096 Feb 23 22:36 ..
drwxr-xr-x 818 root root  36864 Feb 23 22:25 node_modules
-rw-rw-r--   1 root root    333 Feb 23 22:07 package.json
-rw-r--r--   1 root root 219075 Feb 23 22:25 yarn.lock

EDIT:

But what I want is when building the image, get these dependencies not when spinning up the containers. Otherwise I have another container which mounts de source code and needs this node_modules folder when running the "docker-compose up" and I'd like to avoid some kind of ugly sleep until the node_modules is finished. So I need present this folder on my root host before up the containers somehow

If you want to achieve the above goal, you can use the following workaround:

1. You modify Dockerfile a little:

FROM ruby:2.5

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && \
    apt-get install -qq -y build-essential libpq-dev nodejs yarn

RUN mkdir /build && mkdir /myapp
WORKDIR /build

ADD ./package.json /build/

RUN yarn install

WORKDIR /myapp

CMD cp -a /build/node_modules/ /myapp/

That's means that yarn packages will be built in /build folder inside image and copied to /myapp folder once container is started.

2. You use the original docker-compose.yaml file:

version: '3'
services:
  web:
    build: .
    volumes:
      - .:/myapp

when you start web container:

docker-compose up web

folder node_modules is copied to mounted folder i.e. to . folder on your host machine.

3. Now you can start any container and it will be contain node_modules folder inside /myapp:

docker-compose run web bash

So, you will be able to achieve your goal the following way:

$ docker-compose build && docker-compose up web
$ docker-compose run web bash
root@4b38e60adfa3:/myapp# ls -la
total 64
drwxrwxr-x   3 1000 1000  4096 Feb 24 10:59 .
drwxr-xr-x  66 root root  4096 Feb 24 11:13 ..
-rw-rw-r--   1 1000 1000   497 Feb 24 10:55 Dockerfile
-rw-rw-r--   1 1000 1000    73 Feb 24 09:02 docker-compose.yaml
drwxr-xr-x 818 root root 40960 Feb 24 10:57 node_modules
-rw-rw-r--   1 root root   333 Feb 23 22:07 package.json
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!