A quick tutorial on how to dockerize your nodejs app.
I have a nodeapp contained in a folder called myNodeApp. Here’s what the folder structure looks like
Under myNodeApp, create a file called Dockerfile. Here are the contents of that file
FROM node:latest WORKDIR /app COPY . /app CMD node server.js EXPOSE 3333
If this directory is a git repo, like mine is, then you will want to create a file called .dockerignore to prevent your git info from being dockerized. Here are the contents of .dockerignore
.git .gitignore .DS_Store
My app takes configuration settings so one way to do that is through environment variables which you can pass in when you create the container. You can use the -e flag to pass in environment parameters but I prefer to use a properties file.
So I created a docker.env file with my environment properties.
Next we create the image. In the myNodeApp folder run:
docker build -t myNodeApp .
Now that you have the docker image, you can instantiate a container like so
docker run -t -p 3333:3333 --env-file ./docker.env myNodeApp
Remember you can start and stop your container with this command:
docker start/stop [container_id]