Learn Docker with a simple Node app
- Introduction
- Setup
- Dockerfile Configurations
- Run Docker Image (Without Using docker-compose)
- Docker Compose
- Additional Commands
- Acknowledgments
The main purpose of this project is to dockerize a simple NodeJS application and establish a development/production workflow using Docker and NodeJS.
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.
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
Docker must be installed on your local machine.
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"]
FROM
: Specifies the base environment for the Docker image, using the official NodeJS image with version 18.- ...
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
Builds the Docker image based on the Dockerfile.
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:
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.
docker exec -it node-app bash
: Access the container terminal in detached mode using the Bash shell.- ...
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!