Run Your Node Projects with Docker
I recently got an old Node project that needs to be updated. It’s an AngularJS + Ionic 1 project. There are a ton of issues that happened even when I just want to install the packages.
So what I did is to put entire project inside a Docker container that uses an older version of Node which is 6. For your project, you can use Node 8 or Node 10.
Basically, you can do this all any Node projects. What you need is a basic understanding of Docker, then you good to go.
The important thing is to write your Dockerfile. It controls the process of transferring your project to a container.
- The normal method
After finishing your Dockerfile, you have to build an image for your project.
docker build -t image-name .
Then run a container from your image.
docker run --rm -ti -p 3000:3000 image-name# --rm container will be removed after each run
# -p 3000:3000 you can communicate with the project through port: 3000
If there are no errors, you can open your browser and check the project
http://127.0.0.1:3000
Moreover, you can edit your files inside the docker container by open the second bash.
docker exec -ti container-id /bin/bash# Then using vim or nano to edit the files
You can copy the entire project from the container to your host machine
docker cp container-id:/usr/src/app .
2. The crazy method
In this method, I will create a temporary image that doesn’t copy the project files from the host machine or even needs to install bower or node packages. I will share a volume of the project directory from the host machine to the working directory of the container. So basically, I still can edit the project from my current folder from the host and the node commands will be run from the docker container @.@
docker run --name container-name -ti -v your-project-folder:/usr/src/app -p 3000:3000 node-image
After that, you just need to start the container whenever you want to run the project
docker start container-name
My mind was literally blown away at that moment @.@
Hope this helps :)