DEV Community

Cover image for Docker 101: Use it with NodeJS in 2020! πŸ“¦ πŸ™Œ  (practical guide)
Shahjada Talukdar for The Destro Dev Show

Posted on

Docker 101: Use it with NodeJS in 2020! πŸ“¦ πŸ™Œ (practical guide)

I will skip "What/Why Docker?" part to make it straight to the point! 🀝

Let's see this in action!


I already have installed -

  • NodeJS
  • Docker

We will create a simple Node Server. We will use http module from node and make a simple http server. server.js file contains the code of our server-

var http = require('http');

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Node & Docker Running...");
});

server.listen(3333);

console.log("Node HTTP Server started at http://localhost:3333/");
Enter fullscreen mode Exit fullscreen mode

If we run node server.js on our terminal, it will start the server at 3333 port on localhost.
We can open a browser and hit http://localhost:3333/ and we can see server is sending the text Node & Docker Running....
Awesome πŸ‘

Now, we want to create a docker image for our Node Server. To do this, we need to create a file named Dockerfile in the root directory with the below commands-

FROM mhart/alpine-node
COPY server.js .
EXPOSE 3333
CMD node server.js
Enter fullscreen mode Exit fullscreen mode

Here, I have used mhart/alpine-node(Minimal Node.js Docker Image) for having NodeJS environment.

EXPOSE 3333 means - 3333 port is intended to be published.

Now that we have our Dockerfile ready, we will build a Docker image from this file. We can run on the terminal-

docker build -t docknode .
Enter fullscreen mode Exit fullscreen mode

*Here, docknode is the image name. We can use any name.

After running, we can see some steps on our terminal.Like-

Alt Text

Here ^ 4 Steps are basically representing the 4 lines of the Dockerfile, which makes sense! πŸ‘Œ

To verify that, our image has been created successfully we can run the below command to get all the docker images-

docker images
Enter fullscreen mode Exit fullscreen mode

Alt Text

Awesome, we can see our newly created image docknode there 😎

I will run the docker image now which will make a container for us-

docker run -p 3333:3333 docknode
Enter fullscreen mode Exit fullscreen mode

We will see Our Node server is running-
Alt Text

Now, if we open any Browser and hit http://localhost:3333, we can see the server is running πŸŽ‰

If you haven't used Docker yet, I think, you should Try it in 2020!
I will write more on Docker in my future articles!

Till then,
Cheers!
πŸ‘‹

Top comments (4)

Collapse
 
doubles078 profile image
Daniel Donohue

So that docker command line docker run -p 3333:3333 docknode is essentially saying map/expose the docker containers port 3333 to the local computers 3333?

Collapse
 
destro_mas profile image
Shahjada Talukdar

The left one is the localhost one.
We can give any unused port there like 4444:3333

Collapse
 
asdftd profile image
Milebroke

Please read the docs: docs.docker.com/engine/reference/b...

expose does not expose the port for the outside world to reach

Collapse
 
destro_mas profile image
Shahjada Talukdar

Thanks, πŸ‘ I changed the wording.