Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chris-luiz-16/simple-nodejs-hello-world-docker-image-and-container-build
Simple Nodejs Hello world Docker image and container
https://github.com/chris-luiz-16/simple-nodejs-hello-world-docker-image-and-container-build
docker docker-container docker-image dockerfile nodejs
Last synced: 28 days ago
JSON representation
Simple Nodejs Hello world Docker image and container
- Host: GitHub
- URL: https://github.com/chris-luiz-16/simple-nodejs-hello-world-docker-image-and-container-build
- Owner: Chris-luiz-16
- License: mit
- Created: 2023-06-03T13:48:40.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-09-22T13:00:10.000Z (over 1 year ago)
- Last Synced: 2024-11-15T02:26:47.883Z (3 months ago)
- Topics: docker, docker-container, docker-image, dockerfile, nodejs
- Language: JavaScript
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Nodejs Hello world Docker image and container build
Hi there, Today I'm going to showing how we can dockerise a simple node js Hello World application
## Table of Contents
* [A simple hello world node js based application](#a-simple-hello-world-node-js-based-application)
* [Build a Docker image for the node js application](#build-a-docker-image-for-the-node-js-application)
* [Building a Container from our mynodeimage](#building-a-container-from-our-mynodeimage )### A simple hello world node js based application
***
Here is a simple hello word code running in node js in which the aplication listens to the port 3000. I've also enabled access-log for the application as wellBefore we begin, let's clone
```sh
mkdir node-js
mkdir -p node-js/code
touch node-js/code/app.js
```Time to paste the below node js code to our **app.js** file
```java
const http = require('http');
const accesslog = require('access-log');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
accesslog(req, res);
res.end('Hello World! this is a simple nodejs app running in a Docker container
');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
```### Build a Docker image for the node js application
***Let's build a docker image for the nodejs application from the code we made so that we can build a container from the custom image that we build.
Before beginning lets' make a **Dockerfile** in our project directory **node.js**
```sh
cd node-js
touch Dockerfile
```Time to build the docker image make sure to copy the below content in **Dockerfile**
```sh
# building from the alpine image which will consume less space in our host machine
FROM alpine:latest# This is an Enviornment variable where I defined my working directory of application
ENV HOME_DIR /var/nodjs/# This is an Enviornment variable where I defined the user which will run on the appliaction
ENV USER node
# This is an Enviornment variable where I defined which port should I run in the application
ENV PORT 3000WORKDIR $HOME_DIR
# Installing necessary packages for the node js application to run
RUN apk update && apk add nodejs npm --no-cache
# Installing an addition package in order to show the access logs for the applicatiom
RUN npm install access-log
# Copying the code to the default home directory
COPY ./code/ .
# Adding a new user with shell access
RUN adduser -h $HOME_DIR -D -s /bin/sh $USER
# User should have full privilege to home directory
RUN chown -R $USER. $HOME_DIR
# Once a container is build from image, the container should run as node user, this is for security purpose
USER $USER
# The port where my application should run
EXPOSE $PORT
ENTRYPOINT ["node"]
CMD ["app.js"]
```I've set some Enviornment variables where I defined home directory, user and port of the application, you can change as per your needs
Building a docker image. Make you're in the node-js directory before executing the command
```
docker image build -t mynodeimage:1 .
```After executing the above command, you can see your custom docker image by executing ```docker image ls ``` with the name as mynodeimage
### Building a Container from our mynodeimage
***It's time to build the container from our custom image
```sh
docker container run --name nodeapp -d -p 80:3000 mynodeimage:1
```You can see your container running with a port mapping
```sh
]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21ecde87b960 mynodeimage:1 "node app.js" 5 seconds ago Up 4 seconds 0.0.0.0:80->3000/tcp, :::80->3000/tcp nodeapp```
Once everything is donce you can call the IP address of the server either ```http://``` to see the result.
![image](https://github.com/Chris-luiz-16/Simple-Nodejs-Hello-world-Docker-image-and-container-build/assets/128575317/b6056311-0add-4b06-b99d-80b820cbcff1)