Recently, when building an environment for node
with docker-compose
, I encountered a problem that node_modules
in the container went somewhere.
After a lot of research, it seems that when I mount the local directory inside the container with docker-compose
, it is overwritten and the node_modules
inside the container disappears.
Looking at the article such as https://qiita.com/suin/items/e53eee56da23d476addc, it seems that it can be avoided by creating volumes
and mounting node_modules
there.
However, in this case, node_modules
is not generated in the local environment, so the type definition of the package cannot be referenced with TypeScript
, and the taste of using TypeScript
is diminished.
To solve this problem, you can also do yarn install
with command
of docker-compose
to make node_modules
in the container mounted locally and have node_modules
locally I saw an article that I was trying to come up with.
However, with this method, yarn install
runs when doing docker-compose up
, so it took a reasonable amount of time with docker-compose up
. (It seemed like it took a lot of time to bring the node_modules
in the container locally)
Therefore, I tried to solve it by the following method. (If this solution is Bad, please let me know and you will learn)
.
├── app
│ ├── Dockerfile
│ └── src
│ ├── package.json
│ └── yarn.lock
└── docker-compose.yml
Dockerfile
FROM node
WORKDIR /app
ADD src/package.json /app/
ADD src/yarn.lock /app/
RUN yarn install
docker-compose.yml
volumes: #← Add
app_node_modules: #← Add
services:
app:
image: ./app
volumes:
- ./app/src:/app/
- ./app/src/node_modules:/app/node_modules #← Add
#The following is omitted
So far, it's the same as the article at https://qiita.com/suin/items/e53eee56da23d476addc.
On top of this, by running the following script when adding a package (instead of docker-compose build
), node_modules
is also generated locally.
build.sh
docker-compose build
cd app/src && yarn install
For the time being, this solved the problem that node_modules
disappeared and the problem that there was no type definition.
However, since it is assumed that there is a node
in the local environment, I wish there was a better solution. (But I didn't understand)
I would be grateful if you could tell me if there is a good solution.