We can use docker container to intialize a new project(utlity container) and then use it to create a new project. This is a good way to keep the project clean and avoid installing unnecessary packages on the host machine.
- Create a new container
- Install the required packages
- Create a new project
docker image build -t node_utils .
docker container run -it --rm $(pwd):/app utility-container npm init
It will ask for the project name, version, description, entry point, test command, git repository, keywords, author, license. You can skip any of these by pressing enter. And it will create a package.json file.
We can use ENTRYPOINT
in the Dockerfile to append the command , so that we don't have to type it everytime.
docker container run -it --rm utility-container -v $(pwd):/app init
docker container run -it --rm utility-container $(pwd):/app install express
Here, we are using -it
to run the container in interactive mode and --rm
to remove the container after it exits. We are mounting the current directory to /app
in the container using -v
flag.