https://itcreativelabs.com/blog/minimal-node-js-development-environment-using-docker-compose/
back to posts

Minimal Node.js Development Environment Using Docker Compose

Minimal Node.js Development Environment Using Docker Compose
Minimal Node.js Development Environment Using Docker Compose
Technology, Web Development
#Technology, #Web Development

Quick and painless setup of a basic local development environment for Node.js using docker-compose.

This is a quick tutorial on how to get a Node.js Docker container up and running for local development.

This approach does NOT require a Dockerfile and solves infamous insidious server response issues.  No more “localhost didn’t send any data” or “ERR_EMPTY_RESPONSE” or “127.0.0.1 didn’t send any data”.

You will need Docker Community Edition installed and running (aka Docker desktop) and exactly two files to fire up a Node.js app – “docker-compose.yml” and “app.js”.

local development environment for Node.js using docker-compose

docker-compose.yml

version: "3"
services:
    app:
        image: node:alpine
        volumes:
          - .:/app
        working_dir: /app
        ports:
          - 80:80
        command: node app.js

app.js

const http = require('http');
const hostname = '0.0.0.0';
const port = 80;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Live long and prosper!\n');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Then navigate to the newly-created folder using the console and type: “docker-compose up -d”

That’s all.
Now you can open your browser and access your app at “http://localhost

Отладка. Будет убрана потом