I am running docker for the first time and my container is exiting immediately after docker run command. The docker ps is showing me an empty list. When I run docker ps -a I am getting the results of all the containers I pushed in Exited state.
I have tried using -ti command but the container is going in exit state.
I am using following commands to run the file: $ sudo docker build -t test_api3 . and $ sudo docker run -p 8080:3000 -ti test_api3 or ($ sudo docker run -p 8080:3000 -d test_api3)
Dockerfile
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm","start" ]
package.json
{
"name": "firstapp",
"version": "1.0.0",
"description": "first demo app",
"main": "http-server.js",
"scripts": {
"start": "node http-server.js"
},
"keywords": [
"S"
],
"author": "test",
"license": "ISC"
}
http-server.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I am running ubuntu os and it works (Let me know your setup if it still cannot work). The only problem with the code is that you cannot use the address 127.0.0.1 and must use 0.0.0.0 in your http-server.js. For more explanation refer to the link below
https://forums.docker.com/t/network-param-for-connecting-to-127-0-0-1-in-container/2333
Related
i want to build a easy web server to get in touch with docker compose but there are two images build and the webserver doesnt run.
app.js:
'use strict';
const cors = require('cors');
const { request } = require('express');
const express = require('express');
const {flaschenpost} = request('flaschenpost');
const http = require('http');
const logger = flaschenpost.getLogger();
const api = express();
api.use(cors());
api.get('/', (req,res) => {
res.json(
{
now: Date.now()
});
});
const server = http.createServer(api);
const port = 3_000;
server.listen(post, ()=> {
logger.info('Server started.', {port});
});
my Dockerfile looks like this:
FROM node:16.13.0-alpine
USER node
WORKDIR /home/node
COPY --chown=node:node ./package.json ./package.json
COPY --chown=node:node ./package-lock.json ./package-lock.json
RUN npm install --production
COPY --chown=node:node . .
CMD [ "node", "app.js"]
sudo docker build -t api
sudo docker run -d --init -p 3000:3000 --name api api
The output is after running docker ps
|REPO |TAG |IMAGE_ID |CREATED |SIZE |
|api |latest |f164f3da6ad2|4 minutes ago|127MB|
|node |16.13.0-alpine|44e24535dfbf|12 months ago|110MB|
curl http://localhost:3000
outputs this:
curl: (7) Failed to connect to localhost port 3000 after 0 ms: Connection refused
Something is wrong.
Dockerfile
FROM node:17
ENV NODE_ENV=production
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm install --production
COPY . .
CMD [ "node", "index.js" ]
Now build it
docker build -t api .
Inspect image to if you get what you really want
docker inspect image api:latest
[
{
"Id": "sha256:7bbad5c790f4583e19b1dcf3b3c1aeb0a45fc46ad997d54222a51f1867e8789b",
"RepoTags": [
"api:latest"
],
"RepoDigests": [],
"Parent": "sha256:e58f7a2e8bb5c548475104eb813a6f12b8f610aa939b69de92d349116c2d9a00",
"Comment": "",
"Created": "2022-11-14T09:51:03.762665424Z",
I'm trying to run from Docker a simple "hello world" in NodeJS, following a video tutorial. This is my first attempt in nodejs and in Docker so I'm sure I miss something obvious but passing through the video compared to the code I wrote many times, I still can't find the error.
Locally it works fine, but in Docker is unable to run. In Docker Desktop the container becomes green and immediately gray again.
I've tried to change the path many times, thinking that the problem is there but without any clue. I really haven't the experience to investigate further, please help me to solve the issue.
My Dockerfile
FROM node:16
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["npm", "run", "dev"]
package.json
{
"dependencies": {
"express": "^4.18.1",
"node": "^18.5.0"
},
"name": "contratti",
"version": "1.0.0",
"description": "prova",
"main": "App.js",
"scripts": {
"start": "node App.js",
"dev": "nodemon -L App.js"
},
"author": "jegor",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.19"
}
}
App.js
const express = require("express");
const app = express();
app.get("/", (req,res) => {
res.send("<h2> Ciao ciao ..</h2>");
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server running at ${port}`));
ZSH
docker run -v $(pwd):/usr/src/app -p 3000:3000 -d --name contratti contratti
Error returned
> contratti#1.0.0 dev
> nodemon -L App.js
/usr/src/app/node_modules/.bin/node: 1: /usr/src/app/node_modules/.bin/node: �����: Input/output error
/usr/src/app/node_modules/.bin/node: 3: /usr/src/app/node_modules/.bin/node: Syntax error: Unterminated quoted string
It appears that my Hapi app is running in a Docker container, but I can't hit it in the browser. I thought that docker run -d -p 8080:3000 would have done it, but I guess not. I'm running boot to docker and neither http://localhost:8080/hello nor http://192.168.99.100:8080/hello is working.
I've tried tons of variations on this as well.
This is what I see when I run docker inspect <container id>:
Server running at: http://localhost:8080
Here's my Hapi.js server:
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 3000
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, h) {
return 'hello world';
}
});
async function start() {
try {
await server.start();
}
catch (err) {
console.log(err);
process.exit(1);
}
console.log(`App running at: ${server.info.uri}/hello`);
}
start();
Here's my Dockerfile:
FROM node:8.9.3
MAINTAINER My Name <email#email.com>
ENV NODE_ENV=production
ENV PORT=3000
ENV user node
WORKDIR /var/www
COPY package.json yarn.lock ./
RUN cd /var/www && yarn
COPY . .
EXPOSE $PORT
ENTRYPOINT ["yarn", "start"]
Here's my package.json:
{
"name": "my-app",
"version": "1.0.0",
"repository": "https://github.com/myname/myrepo.git",
"author": "My Name",
"license": "MIT",
"private": true,
"dependencies": {
"hapi": "17.2.0"
},
"scripts": {
"start": "node ./src/server"
}
}
The issue is not with Docker but how you configure the node server.
If you bind to localhost it will only be available from within the docker container. If you want to allow connections from the docker host either don't provide a hostname or use 0.0.0.0.
const server = Hapi.server({
host: '0.0.0.0',
port: 3000
});
It appears that my Hapi app is running in a Docker container, but I can't hit it in the browser. I thought that docker run -d -p 8080:3000 would have done it, but I guess not. I'm running boot to docker and neither http://localhost:8080/hello nor http://192.168.99.100:8080/hello is working.
I've tried tons of variations on this as well.
This is what I see when I run docker inspect <container id>:
Server running at: http://localhost:8080
Here's my Hapi.js server:
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 3000
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, h) {
return 'hello world';
}
});
async function start() {
try {
await server.start();
}
catch (err) {
console.log(err);
process.exit(1);
}
console.log(`App running at: ${server.info.uri}/hello`);
}
start();
Here's my Dockerfile:
FROM node:8.9.3
MAINTAINER My Name <email#email.com>
ENV NODE_ENV=production
ENV PORT=3000
ENV user node
WORKDIR /var/www
COPY package.json yarn.lock ./
RUN cd /var/www && yarn
COPY . .
EXPOSE $PORT
ENTRYPOINT ["yarn", "start"]
Here's my package.json:
{
"name": "my-app",
"version": "1.0.0",
"repository": "https://github.com/myname/myrepo.git",
"author": "My Name",
"license": "MIT",
"private": true,
"dependencies": {
"hapi": "17.2.0"
},
"scripts": {
"start": "node ./src/server"
}
}
The issue is not with Docker but how you configure the node server.
If you bind to localhost it will only be available from within the docker container. If you want to allow connections from the docker host either don't provide a hostname or use 0.0.0.0.
const server = Hapi.server({
host: '0.0.0.0',
port: 3000
});
While trying to dockerize node app, when I visit localhost:8000 I get this error:
The connection was reset - the connection to the server was reset
while the page was loading.
In the terminal when I use the run command on image, I get the desired output in console. It says:
Server running at http://localhost:8000/
Dockerfile:
FROM node
RUN mkdir -p /app/
WORKDIR /app
COPY package.json /app
RUN cd /app
RUN npm install
COPY . /app
CMD ["node", "index.js"]
EXPOSE 8000
index.js:
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8000, 'localhost');
console.log('Server running at http://localhost:8000/');
package.json:
{
"name": "server1",
"version": "1.0.0",
"description": "Dockerizing node-app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Himanshu",
"license": "ISC"
}
Here is the run command that I used
sudo docker run -p 8000:8000 -it --name node-container2 my-node-image
All these files are saved in same directory.
Just change your index.js to work on 0.0.0.0 inside container:
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8000, '0.0.0.0');
console.log('Server running at http://0.0.0.0:8000/');
And you will be able to access your app via localhost on a host machine.