[OCI,Linux]๋„์ปค + mysql + express / Docker + mysql + express(2)

๐Ÿ‘‰๐Ÿป ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ / directory structure

app.js (or server.js)
 โ”œโ”€ db.js
 โ”œโ”€ routes/
 โ”‚   โ””โ”€ index.js
 โ”‚   โ””โ”€ health.js

๐Ÿ‘‰๐Ÿป app.js

const express = require("express");
const app = express();
const port = 3000;

// ๋ฏธ๋“ค์›จ์–ด / middleware
app.use(express.json());

// ๋ผ์šฐํ„ฐ / router
const indexRouter = require("./routes/index");
app.use("/", indexRouter);

app.use("/health", require("./routes/health"));

app.listen(port, () => {
  console.log(`Express server running on port ${port}`);
});
~                 

๐Ÿ‘‰๐Ÿป db.js

// db.js
const mysql = require("mysql2");

const db = mysql.createPool({
host: "host.docker.internal",
//  host:"localhost",
  user: "myapp",
  password: "Myapp@1234",
  database: "myapp",
  port: 3306,
  waitForConnections: true,
  connectionLimit: 10,
  connectTimeout: 10000,
});

module.exports = db;

๐Ÿ‘‰๐Ÿป routes/index.js

// routes/index.js
const express = require("express");
const router = express.Router();
const db = require("../db");

router.get("/", (req, res) => {
  db.query("SELECT NOW() AS now", (err, results) => {
    if (err) {
      console.error(err);
      return res.status(500).json({
        success: false,
        error: "Database connection failed",
      });
    }

    res.json({
      success: true,
      message: "Connected to external MySQL!",
      serverTime: results[0].now,
    });
  });
});

module.exports = router;

๐Ÿ‘‰๐Ÿป routes/health.js

const express = require("express");
const router = express.Router();
const db = require("../db");

router.get("/", (req, res) => {
  db.query("SELECT 1", (err) => {
    if (err) {
      return res.status(500).json({ status: "DOWN" });
    }
    res.json({ status: "UP" });

  });
});

module.exports = router;

๐Ÿ‘‰๐ŸปDockerfile

FROM node:24
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node","app.js"]

๐Ÿ‘‰๐Ÿป ์ด๋ฏธ์ง€ ๋นŒ๋“œ / image build

sudo docker build -t myexpressapp .

โญ๏ธ์ด๋ฏธ์ง€ ๋ฐ ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ ๋ช…๋ น์–ด / Image and container verification commands

# ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ / Check running containers
sudo docker ps

# ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ / Check all containers
sudo docker ps -a

# ๋ชจ๋“  ์ด๋ฏธ์ง€ ํ™•์ธ / Check all images
sudo docker images

# ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ / Delete container
sudo docker rm container name

# ์ด๋ฏธ์ง€ ์‚ญ์ œ / delete image
sudo docker rmi image name

๐Ÿ‘‰๐Ÿป ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ / Container run

sudo docker run --name yourexpressapp -d -p 8000:3000 --add-host=host.docker.internal:host-gateway myexpressapp

๐Ÿ‘‰๐Ÿป๋ธŒ๋ผ์šฐ์ € ์ ‘์†ํ…Œ์ŠคํŠธ / Browser connection test
โœ”๏ธ http://ipaddress:8000์ ‘์†์‹œ ์•„๋ž˜์˜ ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด ์ •์ƒ
If the message below is displayed when accessing http://ipaddress:8000, it is normal.

{"success":true,"message":"Connected to external MySQL!","serverTime":"2025-12-22T15:25:30.000Z"}

โœ”๏ธ http://ipaddress:8000/health์ ‘์†์‹œ ์•„๋ž˜์˜ ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด ์ •์ƒ
If the message below is displayed when accessing http://ipaddress:8000/health, it is normal.

{"status":"UP"}

โญ๏ธ ๋„์ปค์— ์˜ฌ๋ฆฌ๊ธฐ ์ „์— ํ…Œ์ŠคํŠธ ํ•˜๋ ค๋ฉด db์—์„œ host ์ˆ˜์ •ํ•˜๊ณ  npm install๋กœ mysql2์™€ express ๋ชจ๋“ˆ ์„ค์น˜ ํ• ๊ฒƒ
To test before uploading to Docker, modify host in db and install mysql2 and express modules with npm install.

Leave a Reply