Creating a simple Nodejsapp with help of Dockerfile to print the "Hello World".
- Simple nodejs Dockerfile for image creation.
- Usage of Alpine OS as base image to reduce the size of image that built.
- Need to have Docker installed in your machine.
- Need to have Git installed in your machine.
- Docker installation
- Alternatively from the above, you can use Pre-installed Docker Terminal (For the easy access to configured Docker environment especially for begineers)
- Git installation
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
Downloading the docker file from Git and build a 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
Building a container from Image pulled from Docker Hub
Container running on port 3000 and output
We can upload the Docker image to Docker hub.
- How to login Docker hub
docker login <----------- login with your credentials which you use in docker hub
- 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
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
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"
The Intention of this Reposistory is to create awareness about the Dockerfile and the usage of containerisation.