Dockerize a Node.js app with VS Code
Tags: node, docker, javascript, vscode
Published: 2019-06-23 20:05:01

---
title: Dockerize a Node.js app with VS Code
published: true
description: 
tags: nodejs, docker, javascript, vscode
cover_image: https://images.unsplash.com/photo-1454991727061-be514eae86f7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80
series: docker
---

Hello dear coder, welcome to my tech articles series dedicated to *Node.js* and *Docker*. Hope you enjoy!

##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](https://nodejs.org/en/download/) and Docker installed. In my case I’m using Windows, so I got [Docker for Windows from here.](https://hub.docker.com/editions/community/docker-ce-desktop-windows)

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:

{% gist https://gist.github.com/vguleaev/423daaba88c72d4f5871ce0c22e85e27 file=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/

![](https://thepracticaldev.s3.amazonaws.com/i/vo5ru1xxz1c4rar86n9m.png)

##3. Prepare Docker file

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

```
touch Dockerfile
```

And paste this inside:

```dockerfile
FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

EXPOSE 8080

CMD [ "npm", "start" ]
```

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](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker). 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.

![](https://thepracticaldev.s3.amazonaws.com/i/a2azbqzmsbr0q9gu1f9o.png)

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.](https://nodejs.org/de/docs/guides/nodejs-docker-webapp/)

<hr/>

🚀 If you read something interesting from that article, please like and follow me for more posts. Thank you dear coder! 😏