Dockerize node app

Ahmed Moataz
Feb 23, 2017 · 1 min read

Steps to dockerize an node app:

  • Create a new file in your project directory named “Dockerfile”
  • This file will hold the configuration for your docker image
  • The content of the Dockerfile is :

#node:latest is the image name.

FROM node:latest

# Create app directory

RUN mkdir -p /opt/my-app

WORKDIR /opt/my-app

# Copy package.json file to the image

COPY package.json /opt/my-app

#Install app dependencies

RUN npm install

# Bundle app source

COPY . /opt/my-app

#Expose the node port

EXPOSE 3000

#Setup the command to be run when the container from that image start

CMD [ “npm”, “start” ]

  • To build the image run this command: “docker build -t <username>/<app-name> .”
  • To push the image to the public docker hub signup https://hub.docker.com/
  • Login through the cli using this command “docker login”
  • Tag the built image using this command “docker tag <username>/<app-name>:<version>”
  • Push the tagged image to the public hub “docker push <username>/<app-name>:<version>”
  • Login to https://hub.docker.com/ and you will find the pushed image