Run Node with Docker in 5 Minutes

By John Papa

Run Node with Docker in 5 Minutes

Heard of the latest and greatest thing? Of course. It’s everywhere. Been avoiding it? Ugh, maybe if we just pretend it doesn’t exist, this fad will disappear before we need to learn it.

Do you ever feel this way? I think it affects a lot of people in our technical community. What if the time investment was just a few minutes, you could learn the basics of the “new thing”, and have something running? Just a little taste of success to at least say we’ve tried it? Sometimes that is all it takes to get started.

Let’s give this a go with Docker and see if it can take us less than 5 minutes to find success.

Here is what we will do:

  1. Create your own basic web server
  2. Install Docker
  3. Build your own image
  4. Run it
  5. Browse to our web page

Create a Web Server

First, let’s create a simple node.js app with express that serves the canonical Hello World. Why? Because the world needs a little more kindness.

mkdir success  
cd success  
npm init -y  
npm i express --save  

Now we’ll open this success folder with the best editor ever created, VS Code. We’ll create a new file named index.js and paste in this code:

const express = require('express');  
const app = express();  
app.get('/', (req, res) => {  
  const msg = 'hello world';
  console.log(msg);
  res.send(msg);
});
app.listen(8626, () => console.log('Our server is running'));  

Now we can test our simple node.js app by running node index.js from a terminal. Be sure to say “Hello” back to the app!

Then we’ll cancel the web server with good ol’ CTRL-C.

Let’s Get Some Docker

Docker … what docker? There’s no docker here yet. We started with an app, and now we’ll go get us some docker.

Go to this link and choose your platform for docker. I’m using macOS, but you pick yours of course. Once it downloads, install docker and follow the prompts.

Build it and They Will Come

OK, that’s kind of a creepy statement from Field of Dreams. But still, an excellent movie. In this case, we’re going to build a docker image from our Dockerfile. Hold on a second … what Dockerfile?

The Dockerfile tells docker what to do, kinda like how google maps tells me how to get somewhere. Let’s create one of these Dockerfile thingies now. Create a new file named Dockerfile. Yep, that’s right, it starts with a capital D and has no file extension. What can I tell ya?

Paste the following commands into the Dockerfile and save it.

FROM node:6.11-alpine  
RUN mkdir -p /app  
WORKDIR /app  
COPY package.json .  
RUN npm install  
COPY index.js .  
EXPOSE 8626  
CMD [ "node", "index.js" ]  

Wanna know what we’re telling docker to do? Well, I’ll tell you anyway. We’re asking it to get an existing docker image off the web that has node 6.11 installed. Then we create a folder named app in that image, copy our package.json to it, install our express package, copy the node server index.js to the image, expose our port, and start the server.

Now we build our image by running the following command from the terminal. This executes the commands in the Dockerfile, builds the image, and tags it with the name johnpapa/success. Feel free to change your tag name, but hey, who doesn’t want a tag called “success”!

docker build -t johnpapa/success .  

Run Docker, Run!

The movie references just keep coming! OK, now we can crank up our server using the following command.

docker run -d -p 8626:8626 johnpapa/success  

We can browse to http://localhost:8626 and the browser is saying hello again. How nice of it!

The Point

The point of this post is not to explain all-things docker. We’re not learning why, which is a big deal frankly. Why is super important. For now, this is just a quick path to some success with docker on your local machine.

If interested, I’ll follow up with more posts or videos on how docker can be helpful and solve real problems we face in Web development. For now, enjoy your success!

Source:: johnpapa