Skip to content

It's a sample Dockerfile. Also, it's creating a small size container that includes nodejs with a sample hello world. Maybe it's helpful for beginners.

Notifications You must be signed in to change notification settings

yousafkhamza/nodejs-dockerfile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

Nodejs Containeraisation (Docker)

Builds


Description

It's a sample Dockerfile. Also, it's creating a small size container that includes nodejs with a sample hello world. Maybe it's helpful for beginners.


Features

  • It's a sample of a nodejs Dockerfile for image creating
  • it's a very small size consumed odejs container (alpine OS)

Pre-Requests

  • Need to install docker on your machine

Installation

  • Docker installtion
  • Pre-installed docker terminal for biginners and it used for lerning purpose.

How to build a image with Dockerfile

Steps:

yum install docker -y
yum install git -y
git clone https://github.com/yousafkhamza/nodejs-dockerfile.git
cd nodejs-dockerfile

building steps:

docker build -t <your_image_name:tag> . 
#eg: docker build -t nodejs:1.1 .
docker image ls <------------------ image will list here

Screenshots

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

Build completed and image is created alt text


How to build a Container from the image

Stpes

docker container run --name node -p 80:3000 -d nodejs:1.1
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

Build a container from previously created image

alt text

Container running on 80 port and output

alt text alt text

Additional Informations


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

I have uploaded the same Docker image to Docker hub. Also, why we use docker hub it's a registry of docker and we can upload our image to docker hub and then we can download the image any system like a git hub

Steps

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

Docker Login alt text

  1. Docker 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.1  <your_username>/<image_name>:tag
docker push <your_username>/<image_name>:tag
# eg:
docker tag nodejs:1.1 yousafkhamza/nodejs:1.1        <------- tag is using for rename but the old image is alive but both are using same image id
docker push yousafkhamza/nodejs:1.1

Docker tag and push alt text


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 pull <your_username>/<image_name>:tag
# eg:
docker pull yousafkhamza/nodejs:1.1

Docker pull from Docker hub alt text


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 installtion
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

It's just a basic explanation for installing docker and how write Docker file and image and container building with a smallest nodejs. I hope you guys are understood. If you have any doubts regarding the Repo please ping me via LinkedIn.

⚙️ Connect with Me

About

It's a sample Dockerfile. Also, it's creating a small size container that includes nodejs with a sample hello world. Maybe it's helpful for beginners.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published