Problem:

You have never used Docker before and you are a JavaScript developer. You want to understand how to use containers together with Node. Lets’ go!

1. Setup

In this tutorial I will be using my favorite editor Visual Studio Code. You will also need Nodejs and Docker installed. In my case I’m using Windows, so I got Docker for Windows from here.

Next command creates a new folder with name test-node-app, then creates npm package file and opens folder in VS Code.

mkdir test-node-app && cd test-node-app && npm init -y && code .

Also please install express to create a server.

npm i express

2. Create basic Node app

Open package.json file and modify it like this:

{

"name": "test-node-app",

"version": "1.0.0",

"description": "",

"main": "server.js",

"scripts": {

"start": "node server.js",

"test": "echo \"Error: no test specified\" && exit 1"

},

"keywords": [],

"author": "",

"license": "ISC",

"dependencies": {

"express": "^4.17.1"

}

}

Now add new file with name server.js:

Super basic basic basic app is ready. Now let’s test it locally. Run this command inside app folder:

npm start

You should see following in the console and will be able to access the server on the link http://localhost:8080/

I am using git bash terminal

3. Prepare Docker file

To build our image we need a docker file. Create a file with name Dockerfile

touch Dockerfile

This file tells docker to download Node.js of version 8, create directory for the app, copy there package.json files, run npm install for node_modules, tells which port this app wants to use and finally runs it with npm start .

Uhhh.. a lot yeah? 😮

We will also need a file called .dockerignore . Create and paste following inside:

node_modules

npm-debug.log

4. Build an Docker image

First of all, build our image with this command.

docker build -t test-node-app .

Wait until it finishes and now we can see new image created.

docker images

If you are using VS Code, you can install a very nice Docker extension. It adds a button to side panel which opens Docker explorer. Here you can see all available images and containers. From there you can run, stop and remove containers which is pretty useful.

If you want to remove an image run this:

docker rmi <image id>

5. Run Docker container (finally)

Time to run a container based on our image (our application in isolated environment). The important thing is that we need to tell docker how to map the port. Now we run our container and map 8080 port to 49165.

docker run -p 49165:8080 -d test-node-app

Which means we can now access the app by this url http://localhost:49165/

There is another very useful command to see all running containers:

docker ps

You can stop container by right click in VS Code Docker Extension or by this command:

docker stop <container id>

Now if you run docker ps again you will not see your container running. If you want to see stopped containers you need to add — all flag to the command like this:

docker ps -a

If you still have some questions you can easy refer to official documentation here.