Dockerization
Instead of running both database server and node application on our machine, using containerization we can run each of them as separate containers. Containers are isolated from one another and bundle their own software, libraries and configuration files.
To do that you need to install docker
, docker-compose
and create Dockerfile
and docker-compose.yml
files.
docker-compose.yml
version: "3"
services:
db:
container_name: nodejs-onboarding-dev-database
image: postgres:latest
restart: unless-stopped
volumes:
- ./postgresdb:/var/lib/postgresql/data
ports:
- 5432:5432
environment:
- POSTGRES_DB='<database_name>'
- POSTGRES_USER='<user_name>'
- POSTGRES_PASS='<user_password>'
app:
container_name: nodejs-onboarding-dev-app
image: nodejs-onboarding-dev-image
restart: unless-stopped
build:
context: .
volumes:
- ./:/app
ports:
- 8000:8000
depends_on:
- db
environment:
- POSTGRES_DB='<database_name>'
- POSTGRES_USER='<user_name>'
- POSTGRES_PASS='<user_password>'
- POSTGRES_HOST='db'
Dockerfile
FROM node:lts-alpine
WORKDIR /app
COPY package\*.json .
RUN npm install
COPY . .
EXPOSE 8000
CMD ["npm", "run", "dev:watch"]
Run docker-compose up
which will build, create and start containers. To bring down and remove containers use docker-compose down
.