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.
- It's a sample of a nodejs Dockerfile for image creating
- it's a very small size consumed odejs container (alpine OS)
- Need to install docker on your machine
- Docker installtion
- Pre-installed docker terminal for biginners and it used for lerning purpose.
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
Downloading the docker file from Git and build a image
Build completed and image is created
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
Build a container from previously created image
Container running on 80 port and output
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
- How to login Docker hub
docker login <----------- login with your credentials which you use in docker hub
- 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
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
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"
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.