8000 GitHub - quan0401/node_with_docker: Learning docker with node
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

quan0401/node_with_docker

Repository files navigation

Docker-Node

Learn Docker with a simple Node app

Table of Contents

Introduction

The main purpose of this project is to dockerize a simple NodeJS application and establish a development/production workflow using Docker and NodeJS.

Setup

Option 1: Run on the local machine

git clone https://github.com/quan0401/node_with_docker
cd node_with_docker
npm run dev # If NodeJS is already installed

If NodeJS is not installed, consider installing it, as we aim to learn Docker and NodeJS together.

Option 2: Run on Docker (recommended)

git clone https://github.com/quan0401/node_with_docker
cd node_with_docker
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

Prerequisites

Docker must be installed on your local machine.

Dockerfile Configurations

FROM node:18
WORKDIR /app
COPY package.json .

ARG NODE_ENV
RUN if [ "$NODE_ENV" = "development" ]; then npm install; else npm install -- fi

COPY . ./
ENV PORT 4000
EXPOSE ${PORT}
CMD ["node", "index.js"]

Explanation:

  • FROM: Specifies the base environment for the Docker image, using the official NodeJS image with version 18.
  • ...

Use docker-compose to run

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

Builds the Docker image based on the Dockerfile.

Docker Compose Base File

Create a docker-compose.yml file with the following content:

version: "3"

services:
  nginx:
    image: nginx:stable-alpine
    ports:
      - "3000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro

  node-app:
    platform: "linux/arm64"
    build:
      context: .
      platforms:
        - linux/amd64
        - linux/arm64
    image: quan0401/node-app
    volumes:
      - ./:/app
      - /app/node_modules
    environment:
      - PORT=3000
    depends_on:
      - mongo
      - redis

  mongo:
    image: mongo
    restart: unless-stopped
    volumes:
      - mongo-db:/data/db

  redis:
    image: redis

volumes:
  mongo-db:

Up Docker Compose

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

Uses the Docker Compose command to create and start Docker containers based on the configurations in the specified Compose file.

Additional Commands

  • docker exec -it node-app bash: Access the container terminal in detached mode using the Bash shell.
  • ...

Acknowledgments

Give credit to any third-party libraries, tools, or resources that you used or were inspired by.

Feel free to modify any part of it according to your needs!

About

Learning docker with node

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0