Dockerizing a Node.js Web Application

If you've ever developed anything that needs to 'live' somewhere besides your local machine, you know that getting an application up and running on a different machine is no simple task. There are countless considerations to be had, from the very basics of "how do I get my environment variables set" to which runtimes you'll need and which dependencies those will rely on, not to mention the need to automate the process. It's simply not feasible for software teams to rely on a manual deploy process anymore.

A number of technologies have sought to solve this problem of differing environments, automation, and deployment configuration, but the most well-known and perhaps most notable attempt in recent years is Docker.

By the end of this tutorial you should be able to:

  • understand what Docker is and what it does
  • create a simple Dockerfile
  • run a Node.js application using Docker
  • use Continuous Integration to automatically build and test Docker containers

Thanks for the step to step guide. I really learned a lot from following the entire process of Dockerizing a Node.JS web app.

I found a few issues that could potentially help other users:

  • docker run -it -p 5432:5432 postgres may give an error requiring a password for the user. You can solve it by using the following command: docker run -it -e POSTGRES_PASSWORD=password -p 5432:5432 postgres. Make sure to also edit your database.js to add in process.env.DB_PASSWORD || 'password',

  • RUN useradd app may also give an error. This is a fix for it: RUN adduser -D -g '' app

  • docker-compose run addressbook npm test also does not work. It shows: sh: jest: not found. I have done with npm install for jest and the command works without running it in Docker. I haven’t found a fix for this yet.

1 Like

Hi, thank you for your suggestions. I’ll try them out and see if we can get the tutorial updated.

Hi fuit-ninja . We’ve updated both the demo repository and the post.
The jest issue was related with duplicated records on the table. The new version truncates the test table before running the tests, so they should pass.

Thank you very much for your suggestions. Things should be working now but let us know if you think we’ve missed anything or have any doubts.