install node packages without restarting docker compose

Viewed 228

Is there a way to install node packages in a running docker environment without restarting? I have running a few containers via docker-compose and need to use npm i <packagename> while the containers are running.

So far, I have found no consistent answer to google.

3 Answers

You can run any command in an active container using docker exec. In your case it will be:

// Replace <your-container-id> and <your-package-name>
docker exec -it <your-container-id> "npm install <your-package-name>"

or if you want to use a container name instead of a container id you can use:

// Replace <your-container-id> and <your-package-name>
docker exec -it $(docker ps | grep <your-container-name> | awk '{ print $1 }') "npm install <your-package-name>"

Here you have more informations about the docker exec docker exec command. (Docker Docs)

docker exec -it [container-id] /bin/bash

cd to the workspace and npm install packagename . Should install the package you want and also add it in package.json

Related