Home > Mobile >  Dockerfile not working on backend with react
Dockerfile not working on backend with react

Time:12-07

I'm trying to run a react app with 2 node servers. One for the front end and one for the back-end connected with a mysql data-base.

I'm trying to use docker for the container and I managed to get the database and the front-end server up. However,When the back-end server is fired it seems like it doesn't acknowledge the Dockerfile.

node_server | npm WARN exec The following package was not found and will be installed: nodemon
node_server |   Usage: nodemon [nodemon options] [script.js[args]
node_server | 
node_server |   See "nodemon --help" for more.
node_server | 
node_server exited with code 0

Dockerfile - client:

FROM node:latest

RUN mkdir -p /usr/src/app

WORKDIR /usr/scr/app

EXPOSE 3000

COPY package.json .

RUN npm install express body-parser nano nodemon cors

COPY . .

Dockerfile - server

FROM node:latest

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

RUN npm init -y

RUN npm install express body-parser nano nodemon cors

EXPOSE 5000

CMD ["npx", "nodemon", "src/server.js"]

docker-compose

version: '3'
services:

  backend:
    build:
      context: ./server
      dockerfile: ./Dockerfile
    depends_on:
      - mysql
    container_name: node_server
    image:
      raff/node_server
    ports:
      - "5000:5000"
    volumes:
      - "./server:/usr/src/app"
 

  frontend:
    build:
      context: ./client
      dockerfile: ./Dockerfile
    container_name: node_client
    image:
      raff/node_client
    ports:
      - "3000:3000"
    volumes:
      - "./client:/usr/src/app"
    

  mysql:
    image: mysql:5.7.31
    container_name: db
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_DATABASE: assignment

The server side is not done yet, but i don't believe it's causing this error.

Server.js

"use strict";

const path = require("path");
const express = require("express");
const app = express();

const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());

const mysql = require("mysql");

let con = mysql.createConnection({
  host: "mysql",
  port: "3306",
  user: "root",
  password: "admin",
});   
 
const PORT = 5000;
const HOST = "0.0.0.0";

app.post("/posting", (req, res) => {
  var topic = req.body.param1;
  var data = req.body.param2;

  sql_insertion(topic, data);
});

// Helper
const panic = (err) => console.error(err);

// Connect to database
con.connect((err) => {
  if (err) {
    panic(err);
  }
  console.log("Connected!");
  con.query("CREATE DATABASE IF NOT EXISTS assignment", (err, result) => {
    if (err) {
      panic(err);
    } else {
      console.log("Database created!");
    }
  });
});

//select database
con.query("use assignment", (err, result) => {
  if (err) {
    panic(err);
  }
});

// Create Table
let table =
  "CREATE TABLE IF NOT EXISTS posts (ID int NOT NULL AUTO_INCREMENT, Topic varchar(255), Data varchar(255), Timestamp varchar(255), PRIMARY KEY(ID));";
con.query(table, (err) => {
  if (err) {
    panic(err);
  } else {
    console.log("Table created!");
  }
});

app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "client/build" , "index.html"));
});


app.listen(PORT, HOST);
console.log("up!");

CodePudding user response:

Modify this line

CMD ["npx", "nodemon", "src/server.js"]

By

CMD ["npx", "nodemon", "--exec", "node src/server.js"]

While putting the command in package.json under scripts section is better.

CodePudding user response:

Your volumes: declarations are hiding everything that's in the image, including its node_modules directory. That's not normally required, and you should be able to trim the frontend: container definition down to

backend:
  build: ./server          # default `dockerfile:` location
  depends_on:
    - mysql
  image: raff/node_server  # only if you plan to `docker-compose push`
  ports:
    - "5000:5000"

The image then contains a fixed copy of the application, so there's no particular need to use nodemon; just run the application directly.

FROM node:latest
WORKDIR /usr/src/app  # also creates the directory
COPY package.json package-lock.json .
RUN npm ci            # do not `npm install` unmanaged packages
COPY . .              # CHECK: `.dockerignore` must include `node_modules`
EXPOSE 5000
CMD ["node", "src/server.js"]

This apparently isn't a problem for your frontend application, because there's a typo in WORKDIR -- the image installs and runs its code in /usr/scr/app but the bind mount is over /usr/src/app, so the actual application's /usr/scr/app/node_modules directory isn't hidden.

  • Related