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"
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
It is going to be like that after create last files:
> devto
server.js
package.json
> docker
docker-compose.yml
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"
}
}
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}`);
});
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"
}
}
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"
]
}
}
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}`);
});
Top comments (0)