Created
April 5, 2024 21:50
-
-
Save LetsCreatProjects/0dc71ced6c2496b1241297f1b2d344a9 to your computer and use it in GitHub Desktop.
Docker Hello world : create image of NodeJS web server with Docker, on Linux Ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Installation on Linux Ubuntu: | |
install docker with those commands: | |
- sudo apt-get update | |
- sudo apt-get upgrade | |
- sudo apt install docker.io | |
- systemctl start docker | |
- systemctl enable docker | |
- docker --version | |
install nodejs and npm with those commands | |
- sudo apt-get install nodejs -y | |
- sudo apt-get install npm -y | |
- npm -v | |
- nodejs -v | |
run app and go to localhost:8080 | |
- node index.js | |
- go to localhost:8080 | |
- if all good, the exit by shortcut CTRL+C | |
create docker image + check if created + run docker image | |
- sudo docker build -t image_name . | |
- sudo docker run -it -p 8080:8080 image_name | |
- go to localhost:8080 | |
- to check the size of the container, then open new terminal and run this command: sudo docker container ls -s | |
- to remove docker image by force, run this command: sudo docker image rm e20bb4abe4ee -f | |
- to delete all images by force, run this command: sudo docker rmi $(sudo docker images -aq) --force | |
- sudo docker images | |
- sudo docker image ls -a | |
index.js | |
const http = require("http"); | |
const port = 8080; | |
const server = http.createServer(function (req, res) { | |
res.write( | |
` | |
Hello Dockernized NodeJS People! | |
` | |
); | |
res.end(); | |
}); | |
server.listen(port, function (error) { | |
if (error) { | |
console.log("Something went wrong", error); | |
} else { | |
console.log("Server is listening on port " + port); | |
} | |
}); | |
Dockerfile | |
FROM ubuntu:18.04 | |
WORKDIR /app | |
COPY index.js /app | |
RUN apt-get update | |
RUN apt-get upgrade -y | |
RUN apt-get install nodejs -y | |
RUN apt-get install npm -y | |
COPY . /app | |
CMD node index.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment