Minimal Node.js Development Environment Using Docker Compose

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

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