Forem

Dênis Mendes
Dênis Mendes

Posted on

1

Node with Docker (JS/TS)

I'd like to share how to run Node application using docker.

First things first, you need to install Docker so take a look in https://www.docker.com/ and download a version which fits with your OS. The install step is easy and I won't cover here.
Don't forget to active the option: Use Docker Compose V2, you can read more here https://docs.docker.com/compose/compose-file/.

After you have docker installed we could create a directory to have our configurations file, here I'm going to name as devto/docker, so into docker I created a file named docker-compose.yml:

services:
  node_backend:
    image: node:18.15.0
    ports:
      - "8087:8080"
    volumes:
      - ../:/var/www/app
    working_dir: /var/www/app
    command: bash -c "npm install && npm run dev"
Enter fullscreen mode Exit fullscreen mode

Now into that directory docker you can run:

docker-compose up -d

It is going to build and run a new container with Node (v. 18.15.0).

But we are not ready yet! It's needed to add files to start our server.

Then, next step here is to create package.json and server.js into devto directory. Remember our directory tree is:

> devto 
    > docker
        docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

It is going to be like that after create last files:

> devto
    server.js
    package.json
    > docker
        docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

Ok, lets to create these files and start our server:

package.json

{
    "name": "devto",
    "version": "1.0.0",
    "description": "Node.js on Docker",
    "author": "First Last <first.last@example.com>",
    "main": "server.js",
    "scripts": {
      "start": "node server.js",
      "dev": "nodemon server.js"
    },
    "dependencies": {
      "express": "^4.18.2"
    },
    "devDependencies": {
      "nodemon": "2.0.22"
    }
  }
Enter fullscreen mode Exit fullscreen mode

server.js

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

Now you can run:

docker-compose up -d

And in order to check if everything is working you can open your browser and type localhost:8087

If something wrong happened you can list containers are up:

docker ps

If it is not there your container named node_backend then you can run that command:

docker ps -a

After find your container you can see what went wrong running that command:

docker logs CONTAINER_ID

That's it :) I hope everything went right and you are seeing a Hello World page.

Last thing, if you want to use Typescript as your default language in while developing I will let below the files with that configuration, so you can decide which one is going to use, anything let a message in the comments below.

Typescript configuration files:

package.json

{
    "name": "devto",
    "version": "1.0.0",
    "description": "Node.js on Docker",
    "author": "First Last <first.last@example.com>",
    "main": "dist/server.js",
    "scripts": {
      "start": "tsc && node dist/server.js",
      "dev": "nodemon server.ts"
    },
    "dependencies": {
      "express": "^4.18.2"
    },
    "devDependencies": {
      "nodemon": "2.0.22",
      "ts-node": "10.9.1",
      "@types/express": "^4.17.1",
      "@typescript-eslint/eslint-plugin": "^5.4.0",
      "@typescript-eslint/parser": "^5.4.0",
      "eslint": "^8.3.0",
      "typescript": "^4.5.2"
    }
  }
Enter fullscreen mode Exit fullscreen mode

tsconfig.ts

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es6",
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist"
    },
    "lib": [
        "es2015"
    ],
    // NEW: Options for file/directory watching
    "watchOptions": {
        "excludeDirectories": [
            "**/node_modules",
            "_build"
        ],
        "excludeFiles": [
            "build/fileWhichChangesOften.ts"
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

server.ts

import express from 'express';
const app = express();
const port = 8080;

app.get('/', (req, res) => {
  res.send('Hello World! :)');
});

app.listen(port, () => {
  return console.log(`Express is listening at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay