Skip to main content

Node Code - hello express on docker

July 06, 2016 mgroves 0 Comments
Tags: node express docker

I got my "Hello Express" example working on Docker for Windows. I see the potential benefit of putting a (node) app in docker, but am I meant to actually develop this way? It seems like a lot of steps to create a package each time I change code. There must be something I'm missing: please leave a comment to let me know.

I also took the time to update the version of Node I was using. It was painless: I uninstalled the old version and used choco install nodejs. This installed node 6.2.2 (code named "slim"?) and npm 3.9.5. I ran my hello world and hello express code, and they still worked fine.

To start with, I created a brand new express project:

express hello-express-docker
cd hello-express-docker
npm install

And then, I opened it with Visual Studio Code.

code .

By the way, you can open up a console window from within Visual Studio Code. View -> Toggle Integrated Terminal (or Ctrl+`).

Next, I pretty much followed the instructions on how to Dockerize a Node.js web app. First, I created a Dockerfile in the hello-express-docker folder. This is just a text file named "Dockerfile". Inside that file, I put:

# I used node:slim which corresponds to 6.2.2 I guess
FROM node:slim

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

# Expose the port
EXPOSE 3000

# kick off the express site
CMD [ "npm", "start" ]

Note that I used a different port number than the instructions, because that's what express's default seems to be (not 8080 as the instructions indicate).

Next I created a docker package.

docker build . -t ccc/hello-express-docker

The tag isn't strictly necessary; the ccc stands for "Cross Cutting Concerns" :)

Once that's done, you can list all the images you have with:

docker images

I have some other images that I downloaded from the docker hub, including couchbase, nginx, ubuntu. You should see at least the ccc/hello-express-docker package listed.

Now that I have an image, I'll run it just like any other docker image:

docker run --name hello_express_docker -p 49160:3000 -d ccc/hello-express-docker

Port 49160 is just an arbitrary port. Once that's running, I can visit my site in a browser: http://docker:49160 (docker is the host name that Docker for Windows uses. If you are using Mac, Linux, Docker Toolbox, etc that may vary).

And that's it! Now my hello-express site is running in a container. If I were done, I guess I could give this container to devops for deployment. I'm struggling to see why I would do this on a day-to-day basis as a developer. Maybe I can have the docker container "mount" my working folder so I can make changes with Visual Studio Code. Or vice versa?

Comments

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter