What to do when you run into a docker-compose node_modules problem

What kind of problem?

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.

How did you solve it?

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)

in conclusion

I would be grateful if you could tell me if there is a good solution.

Recommended Posts

What to do when you run into a docker-compose node_modules problem
What to do when you become a Problem During Content Assist
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do if you get a port error when docker-compose up on Mac
What to do when a javax.el.PropertyNotWritableException occurs
What do you use when converting to String?
What to do when you want to delete a migration file that is "NO FILE"
<f: ajax> Unable to attach <f: ajax> to non-ClientBehaviorHolder What to do when you become a parent
What to do if you accidentally create a model
What to do when a null byte error occurs
What to do when rails creates a 〇〇 2.rb file
What to do if you get a java.io.IOException in GlassFish
What to do when javax.batch.operations.JobStartException occurs
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do if you get a gcc error in Docker
What to do if you get a DISPLAY error in gym.render ()
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do when undefined method ʻuser_signed_in?'
What to do if you get a groovy warning in Thymeleaf Layout
[Android Studio] What to try when you get into error spitting hell
What to do if you get an error when you hit Heroku logs
What to do when "call'Hoge.connection' to establish a connection" appears on rails c
What to do if you get a MiniMagick vulnerability alert on GitHub
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you push incorrect information
What to do when debugging "Source not found"
What to do when IllegalStateException occurs in PlayFramework
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you get a wrong number of arguments error in binding.pry
What to do when The SSL certificate has expired
What to do when JSF tags do not become HTML
What to do if ClassNotFoundException occurs when starting Tomcat
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
Error ExecJS :: RuntimeUnavailable: What to do when it occurs
ParseException: What to do when Unparseable date is reached
A memo to check when you try to use Lombok
<Dot installation> What to do if you cannot proceed due to an error when building a development environment for Rails learning.
What to do when Cannot format given Object as a Date in convertDateTime of JSF
What to do when a could not find driver appears when connecting to a DB in a Docker environment
What to do if a SAX Parser error occurs when using Liferay 7 / DXP on AWS
What to do if you get a "302" error in your controller unit test code in Rails
What you need to do to open a file from the menu in the document-Based App macOS app
[Rails] What to do when the view collapses when a message is displayed with the errors method
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
What to do when it becomes Unable to find CDI BeanManager.
What to do when Method not found in f: ajax
[IOS] What you need to know before creating a widget
# What to do if you accidentally do rails db: migrate: drop
What to do when Cannot apply expression operators to method binding
When you want to change the MySQL password of docker-compose
What to do if mysql2 gets a bundle install error
What to do if you can't use the rails command
What to do if you go offline due to SSL certificate error when running Jenkins on Mac
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do when you want to know the source position where the method is defined in binding.pry