DevOps ~ Docker ~ Containerize Node app - rohit120582sharma/Documentation GitHub Wiki

Create a "Dockerfile" file in the project

FROM node:alpine

WORKDIR /usr/src/app

COPY ./package*.json ./

RUN npm install

COPY ./ ./

CMD ["npm", "run", "start"]

Create a "docker-compose.yml" file

version: '3'
services:
	my-mongo:
		image: mongo
		volumes:
			- mongo-data:/data/db
		expose:
			- '27017'
	app:
		container_name: docker-node-app
		restart: always
		build:
			context: .
			dockerfile: Dockerfile
		ports:
			- '3000:3000'
		links:
			- my-mongo
		depends_on:
			- my-mongo
		environment:
			- CHOKIDAR_USEPOLLING=true
			- MONGO_URL=mongodb://my-mongo/<db-name>
volumes:
	mongo-data:

Run the app

Run Docker Compose from the same directory:

$ docker-compose up --build

You now have a MongoDB container and Node container running on your host. Navigate to http://localhost:3000 in a web browser to see your application. You can also use docker ps to further explore the resulting configuration.

⚠️ **GitHub.com Fallback** ⚠️