Skip to content

Instantly share code, notes, and snippets.

@EddiG
Last active April 20, 2021 06:48
Show Gist options
  • Save EddiG/589587bc07570377ea31c0c661000754 to your computer and use it in GitHub Desktop.
Save EddiG/589587bc07570377ea31c0c661000754 to your computer and use it in GitHub Desktop.
I will try to consolidate my knowledge about Docker here

How to use Docker

As Node.js powered development environment

Initialize new container with name my_project
docker run -it -d -v "$(PWD)":/app -v /app/node_modules -w /app -p 8080:8080 --name my_project node:8 /bin/bash

Notice how we mount new volume above node_modules folder. In this way, we significantly improve performance for host machines powered by OSX or Windows. Otherwise, we will stay with a poor native performance when the Docker synchronize file system state on host and container on npm install command execution.

Install project dependencies
docker exec -it my_project npm install
Remove container and clean up volumes
docker stop my_project
docker rm my_project
docker volume prune
Enter inside container
docker start my_project
docker exec -it my_project /bin/bash
Run NPM script in container
docker start my_project
docker exec -it my_project npm run test

We can group those commands in Makefile

build:
	docker run -it -d -v '$(CURDIR)':/app -v /app/node_modules -w /app -p 8080:8080 --name my_project node:8 /bin/bash
	docker exec -it my_project npm install

clean:
	docker stop my_project
	docker rm my_project
	docker volume prune

rebuild: clean build

start:
	docker start my_project

connect: start
	docker exec -it my_project /bin/bash
	
test: start
	docker exec -it my_project npm run test

Then just execute any of those like make build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment