In my previous article (here), we did setup our development environment for building node.js application, setup docker environment which we will refer in this article, created sample application which we will containerize in this article & setup command line interface for interacting with google cloud. Before continuing just make sure you have docker service up and running.

Verify docker installation.

To verify if docker is working fine, execute following command. This should list all the docker process running in local environment.

docker ps
Docker ps for fetch running docker container

Output from above command.

docker ps output

Create Dockerfile

Now let us get back to our node.js app which we created in our earlier article. Open node.js source code in VSCode or your preferred editor. Create a new file with name Dockerfile at root of node.js application. Please note this file should not have any file extension.

Dockerfile for node.js application

Open Dockerfile in VSCode / text editor and add following code.

FROM node:12-alphine3.10

COPY . /app

RUN npm install --prod

CMD ["npm", "start"]
Dockerfile details

Dockerfile content explanation

  • FROM : Will let docker know which file should be used as a base for our application. You can create your base docker image or get one from docker hub. node:12-alphine3.10 is official docker image published & maintained by node.
  • WORKDIR : Will let docker know which directory will work has a working directory
  • COPY : Will copy all the files to docker images based on parameter passed. Here we have passed two parameter . which is source. In this case source will be current directory and /app which will be destination inside docker image.
  • RUN : Will let docker know executable command while creating image. In our case we are making sure all our npm packages are installed will creating image. Reason we don't directly copy our local ./node_modules some of the code are compiled based on your current development environment. We don't want this compilation to be copied as it is. Let this compilation happen again inside docker image based on it's image base file.
  • EXPOSE : Will let docker know which port we will be accessing from external environment.
  • CMD : Will let docker know which command should be execute when current images is started as a process. Here we are just running npm start which should start our application inside docker process.

Docker image creation

Ok now we have our dockerfile which should help us creating docker image with our node.js application. Open a command prompt / CMDer and navigate to node.js application root folder and execute following command for create a docker image.

docker build -t sampleapp .
Docker build command

This will create docker image sampleapp with configuration detail mentioned in Dockerfile. To verify image was create successfully please use following command

docker images
List docker images

This should list all the images which are available locally. If this is first time you got docker up and running you should see two image. One for node.js image which docker will download for mapping base image and second should be sampleapp image which you just created.

Just in case if you want to verify files inside your docker images, you execute following command inside cmder / command prompt.

docker run -it --rm --name sampleappservice sampleapp /bin/sh
Run docker in interactive mode

Note : -it  will run docker process in interactive mode and --rm will delete process once you exit from docker process. /bin/sh will defer based on your based image. You can also use docker inspect.

Once you run above command you should see command prompt running inside docker process. If you execute ls command you should be able to see files which were copied from your local system. You can also run npm start to test application. Please note application will only be available inside docker image since we have not mapped port with docker process. Use -p portName:portName and -d to run docker process in background. Here is how the command should look like after including does command.

docker run -d -p 8080:80 --name sampleappservice sampleapp
Run docker process in background

In my next article here, I have covered how to setup a kubernetes cluster on Google Cloud.