Let’s Dockerize a Nodejs Express API

From Hello World to Continous Development.

NodeJS Hello World!

To start with, let’s create a minimal nodejs api consisiting a simple GET route. For this example i’ll be using expressjs which is a popular framework for nodejs.

Initialize a new npm project

npm init

2. Create required files

touch .gitignore

touch .dockerignore

touch server.js

3. Ignore your .env and secrets from commiting accidently into git using .gitignore Similarly, use .dockeringore to prevent files from packaging into a docker image.

4. Let’s create a simple server using expressjs. For this we’ll be needing the following packages

npm install --save express cors body-parser @rama41222/node-logger

6. For continuous development, you’ll need nodemon. Install nodemon as a dev dependancy.

npm install --save-dev nodemon

5. After installing these packages, create the server.js as follows.

server.js

6. Goto the package.json and under scripts, create the following commands

"scripts": {

"start": "node server.js",

"dev": "nodemon server.js"

}

7. Let’s run it

npm run dev

8. You should see the following log if your api is running well.

2019–03–27 00:25:56 info: sample-express-app running on 0.0.0.0:3000

We are almost done with nodejs coding. Now let’s get into docker.

Docker File

First create a Dockerfile and a docker-compose.yml to run the nodejs api using docker.

touch Dockerfile

touch docker-compose.yml

2. Let’s creat the Dockerfile and docker-compose.yml as follows

docker-compse.yml and Dockerfile

Let’s quickly walk through the Dockerfile

So in the docker file, we are using the alpine image since it’s very simple, light weight and more secure. Alpine Linux is based on musl libc and busybox.

alpine linux image sizes

FROM node:11-alpine

RUN is an image build step, the state of the container after a RUN command will be committed to the docker image. A Dockerfile can have many RUN steps that layer on top of one another to build the image.

RUN mkdir -p /usr/src/app

Then we set the default working directory by follwing command

WORKDIR /usr/src/app

Copy all files to the image

COPY . .

Install the packages

RUN npm install

Now we must expose the API running port to the outside world inorder to recieve requests.

EXPOSE 3000

CMD is the command the container executes by default when you launch the built image. A Dockerfile can only have one CMD . The CMD can be overridden when starting a container with docker run $image $other_command .

ENTRYPOINT is also closely related to CMD and can modify the way a container starts an image

CMD ["npm", "run", "start"]

Let’s run the API using docker

Let’s build the docker image

docker-compose build

2. Let’s run the container.

docker-compose up

3. Let’s run the above command in the background

docker-compose up -d

Take a look at the following commands. They are very useful

docker-compose stop

docker-compose start

docker-compose run <custom service>

When you are running a docker container in background and still if you want to see the console outputs, use the below command.

docker logs $(sudo docker ps -aq --filter name=<>

For live console output