Hello,
My YAML file is as follows:
version: '3.9'
services:
web:
image: nginx:latest
container_name: Nginx
ports:
- '8080:80'
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- /var/www/html:/usr/share/nginx/html
depends_on:
- php-fpm
links:
- php-fpm
- nodejs
php-fpm:
image: php:8-fpm
container_name: PHP
volumes:
- /var/www/html:/usr/share/nginx/html/
nodejs:
image: node:latest
container_name: Nodejs
command:
- bash
- -c
- |
npm install
npm start
volumes:
- /var/www/html:/usr/src/app
The default.conf file is as follows:
server {
listen 80;
server_name default_server;
error_log /var/log/nginx/error.system-default.log;
access_log /var/log/nginx/access.system-default.log;
charset utf-8;
root /usr/share/nginx/html;
index index.html index.php index.js;
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
add_header script-filename $document_root$fastcgi_script_name alway
s;
include fastcgi_params;
}
location ~ \.js$ {
proxy_pass http://nodejs:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / {
autoindex on;
try_files $uri $uri/ $uri.html =404;
}
}
I have copied two files index.js and package.json to /var/www/html directory. The contents of the index.js file is as follows:
const http = require('http')
const server = http.createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/html' })
if (req.url === '/') {
res.write('<h1>Node and Nginx on Docker is Working</h1>')
res.end()
} else {
res.write('<h1>404 Nout Found</h1>')
res.end()
}
})
server.listen(process.env.PORT || 3000, () => console.log(`server running on ${server.address().port}`))
The contents of the package.json file is as follows:
{
"name": "node-nginx",
"version": "0.0.1",
"description": "node with nginx on docker for beginner",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": ["node","nginx","docker","ingress"],
"author": "restuwahyu13",
"license": "MIT"
}
After running the containers, I got the following errors:
Nodejs | npm ERR! Tracker "idealTree" already exists
Nodejs |
Nodejs | npm ERR! A complete log of this run can be found in: /root/.npm/_logs/
2024-04-01T13_41_35_166Z-debug-0.log
Nodejs | npm ERR! code ENOENT
Nodejs | npm ERR! syscall open
Nodejs | npm ERR! path /package.json
Nodejs | npm ERR! errno -2
Nodejs | npm ERR! enoent Could not read package.json: Error: ENOENT: no such fi
le or directory, open '/package.json'
Nodejs | npm ERR! enoent This is related to npm not being able to find a file.
Nodejs | npm ERR! enoent
Nodejs |
Nodejs | npm ERR! A complete log of this run can be found in: /root/.npm/_logs/
2024-04-01T13_41_36_118Z-debug-0.log
What is wrong?
Cheers.