Dockerizing a Nodejs app

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

Screen Shot 2017-07-26 at 2.35.41 PM

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.
e.g.

MY_PROPERTY=myValue

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]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: