Dockerizing a Full-Stack Ionic + Node.js Application
Overview
Deploying full-stack applications can be a headache, especially when dependencies, OS versions, or tools differ across environments. That’s where Docker shines. It allows you to create isolated, reproducible environments for your Ionic frontend, Node.js backend, and even MongoDB database.
In this guide, you'll learn how to:
- Containerize the frontend (Ionic + Angular)
- Containerize the backend (Node.js + Express or NestJS)
- Connect everything using
docker-compose
- Run your stack with a single command
Project Structure
For clarity, let’s assume this layout:
my-app/
├── client # Ionic frontend project
├── server # Back end Project
├── docker-compose.yml
Step 1: Backend Dockerfile (Node.js)
Create Dockerfile
inside the server-node/
directory:
# server/Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
Step 2: Frontend Dockerfile (Ionic)
Inside client/
, create the Dockerfile:
# client/Dockerfile
FROM node:18 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Serve with Nginx
FROM nginx:alpine
COPY --from=build /app/www /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Step 3: Docker Compose Setup
In your root directory, create docker-compose.yml
:
version: '3.8'
services:
backend:
build: ./server
ports:
- "3000:3000"
volumes:
- ./server:/app
restart: always
frontend:
build: ./client
ports:
- "8080:80"
depends_on:
- backend
restart: always
This configuration builds and runs both the frontend and backend containers, linking them together.
Optional: Add MongoDB Support
For using MongoDB in your backend, add this service:
mongo:
image: mongo
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
Running the Stack
docker-compose up --build
Benefits of Dockerizing Your Stack
- Consistent environments across dev/staging/prod
- Simple to test and debug full-stack features
- Easy onboarding—developers just run one command
- Ready for deployment on any cloud or VPS
Production Tip: Use Environment Files
For secrets or configuration (like DB URLs, ports, etc.), use .env
files and map them inside docker-compose.yml
using the env_file
option.
Final Words
Docker brings order to full-stack development chaos. Whether you're building a prototype or deploying a production app, containerizing your Ionic + Node.js stack ensures everything runs smoothly and predictably.
0 Comments