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 our own basic web server
  2. Install Docker
  3. Build our own image
  4. Run it
  5. Browse to our web page

Create a Web Server

First, I assume you have node.js installed. Then 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 in our success folder, 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 and open the browser to http://localhost:8626. 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. Let's pick the stable channel because the opposite of stable is unstable ... and well ... let's not go there. 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 tell 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.

VS Code may give you warning/message saying that it should have a docker extension. It's pretty darn awesome, but right now let's pass on this. I'll show how to use that in a video on Twitter in the future. Move along, nothing to see here.

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 .  

Don't forget that . at the end of the docker build command. It's not dirt on your screen. It's quite essential. But if your screen is dirty, please go clean it ... otherwise, it can get filthy.

You'll see a bunch of logging when you run this. All sorts of colorful logs fly at us. Whites and reds. Don't worry, that's natural.

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!

Clean Up on Aisle Four

OK, that's cool, but let's clean up and stop it from running. Run this command to stop it. Don't ask me what it does, nobody knows.

docker stop $(docker ps -a -q --filter ancestor=johnpapa/success)  
docker rm $(docker ps -a -q --filter status=exited)  
docker rmi johnpapa/success  

Well, if you must know, these commands find the container we just created from the image, stops the container from running, removes the container, then removes the image. It's like it was never here!

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.

Maybe this wasn't 5 minutes ... but hopefully, it was short enough for you to get to success before I lost your attention.

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!

Special thanks to my friend Ward Bell for proofreading this article.

Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders

Tags:
node.js javascript docker