Skip to content

Launching a Nodejs app in docker container which prints Hello World.

Notifications You must be signed in to change notification settings

amalbosemathew/simple_nodejsapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Nodejs Containeraisation (Docker)

Builds


Description

Creating a simple Nodejsapp with help of Dockerfile to print the "Hello World".


Features

  • Simple nodejs Dockerfile for image creation.
  • Usage of Alpine OS as base image to reduce the size of image that built.

Prerequisites

  • Need to have Docker installed in your machine.
  • Need to have Git installed in your machine.

Installation


How to build a image with Dockerfile

Steps:

yum install docker -y
systemctl start docker
yum install git -y
git clone https://github.com/amalbosemathew/simple_nodejsapp
cd simple_nodejsapp/
docker build -t <your_image_name:tag> . 
#eg: docker build -t nodejs:1.0 .
docker image ls <------------------ image will list here

Screenshots

Downloading the docker file from Git and build a image_ alt text


How to build a Container from the image

Steps

docker container run --name nodejs -p 3000:3000 -d nodejs:1.0
docker container ls  <--------- container listing with status
Argument explanation:
--name <----- Using for container name otherwise docker select a random name
-p     <----- Using port publish like our local port assign for that container it means localport forwards to docker container
-d     <----- Detach the container otherwise its try to enter the container

Screenshots

Building a container from Image pulled from Docker Hub

alt text

Container running on port 3000 and output

alt text

Additional Information


Push Image to Docker Hub (upload docker image to registry)

We can upload the Docker image to Docker hub.

Steps

  1. How to login Docker hub
docker login <----------- login with your credentials which you use in docker hub
  1. Docker Tag and Push

Docker push is working with your docker hub username so you need to change the image name with your username

docker tag nodejs:1.0 amalbosemathew/nodejs:1.0
docker tag nodejs:1.0 amalbosemathew/nodejs:latest
docker image push amalbosemathew/nodejs:latest

How to pull this image from Docker hub(Download image from registry)

Download image from Docker hub and it no needs to login docker hub.

docker image pull <your_username>/<image_name>:tag
docker image pull amalbosemathew/nodejs:latest

Docker File Explanation

FROM alpine:3.8               <-------- Base Image
RUN mkdir /var/node/          <-------- RUN is using for exicute shell command
WORKDIR /var/node/            <-------- Image working directory
COPY ./app.js ./              <-------- Copy node file to WD
RUN apk update
RUN apk add nodejs npm        <-------- nodejs and npm installation
RUN npm init -y
RUN npm install express       <-------- nodejs module installtion 
EXPOSE 3000                   <-------- Just expose which port we use in container
ENTRYPOINT [ "node" ]         <--------- EntryPoint we using our image default command and if you need to change container runing time you can use "docker run --entrypoint sh <image>:tag" when you enter this your image default command is shell 
CMD [ "app.js" ]              <--------- CMD is working the same image default command but when you use ENTRYPOINT at that time this following entry point and it works as a argument of ENTRYPOINT eg: "node app.js"

Conclusion

The Intention of this Reposistory is to create awareness about the Dockerfile and the usage of containerisation.

About

Launching a Nodejs app in docker container which prints Hello World.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published