简体   繁体   中英

Docker Build Fails To Create server.js

I am working on a tutorial for Docker, and I am learning about docker build. In the tutorial, this is the docker file

FROM ubuntu:14.04

RUN apt-get update -y
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -

RUN apt-get install -y node.js

COPY server.js /

EXPOSE 8080

CMD [ "node", "/server.js"]

In the same directory as the dockerfile is a server.js file:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "Text/plain"});
  response.end("Hello world!");
})

// Listen on port 8080
server.listen(8080, function() {
  console.log('Server listening...');
})

From command line I run docker build: docker build -t ahawkins/docker-into-hello-world.

Then I run the container from the above image: docker run -d -p 8080:8080 ahawkins/docker-intro-hello-world

I expect a curl localhost:8080 to respond with "Hello world!" , but instead I get:

curl: (52) Empty reply from server

I ssh into the container and see that server.js is no where to be found, and moreover I cannot even find the node installation.

Have I installed node correctly? Should I expect to see a server.js file at the root of the operating system? Why am I not seeing a, "Hello World,"? from my curl command?

NodeJS coming with 14.04 image can be bogous. I would recomend using ready node image to build nodejs apps. Try this manual - https://nodejs.org/fr/docs/guides/nodejs-docker-webapp/

I would try the below in your Dockerfile, build and try to run again. I replaced "/" with "." from the COPY command to insert it into the Image freely. (Having server.js in the same directory where you are building your Dockerfile.)

FROM ubuntu:14.04

RUN apt-get update -y
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -

RUN apt-get install -y node.js

COPY server.js .

EXPOSE 8080

CMD [ "node", "server.js"]

Have you tried to open a browser and type "localhost:8080" to see the "Hello World" message?

I hope this might help. The answer that was already before given, also has a great link to smooth documentation.

My References/ resources used: Udemy Class from Bret Fisher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM