A Lightweight Approach With Alpine Linux
Docker has become an extremely popular tool not only among DevOps and Infrastructure people, but also for the daily work of any developer. And as a developer, during the past week I decided it was time to have a look at it and see by myself all the good things people talk about. And I don’t regret it! In a matter of days I could understand the basics (the very basics) and get up and running with what I mostly do these days, a Node.js and MongoDB application.
As I said, these are my first experiences with Docker, so please don’t take this as an expert tutorial. This post is aimed to the Docker beginners like me, who want to get a fully working instance of an Express and MongoDB app up and running in a matter of seconds for daily work. This applies for any Node.js app, but for the matter of the post I’ll work with a dummy Express server that returns some dummy data from a MongoDB.
Before starting make sure you have Node.js, npm and Docker installed. The full code of the sample project can be found here
The dummy Node app
We’ll start by creating the Node.js app that will be “dockerized” later on. For this purpose we’ll use the fast express generator. Make sure you have express installed globally:
# you might need to add a `sudo` to the command npm i -g express
cd into any directory you want and create the
$ express dummy-app
Follow the instructions and go inside the
dummy-app dir and run
npm install to get all the needed dependencies. And after that run the app:
# The DEBUG variable lets us see in the console the express app logs DEBUG=dummy-app:* npm start > email@example.com start dummy-app > node ./bin/www dummy-app:server Listening on port 3000 +0ms
Now go try it in the browser, you should see the friendly “Welcome to Express” page.
Dockerizing: node vs alpine-node
Now that we have our very simple webapp, we can start with the dockerization process. If you don’t know yet what a docker image and container is, I highly recommend reading this.