Add docker to your NodeJS workflow in just 4 simple steps

Add docker to your NodeJS workflow in just 4 simple steps

What is docker and why should you use it?

Docker is a platform as a service which allows you to isolate an entire operating system via Linux containers. The files we create below are instructions for how Docker should build the Linux container. Dockerizing (is that a word?) your application should be the first step in your workflow as it provides a good base for your development and production environments. Onboarding new members to the project is easy too, just provide them the respective Dockerfile and they’re good to go. Docker can become extremely complicated and you can add a lot of configuration to your application with Docker alone.

You will definitely benefit from spending time researching complex features of Docker!

Add docker to your NodeJS app in 4 steps

I don’t explain what the following code does, but docker has great documentation on it, docs.docker.com/engine/reference/builder . All of the following config files will be at the root of your project. These files will work for probably 90% of your NodeJS applications, barring specific configuration. If this is an application that will be shipped to production, create a separate Dockerfile.prod that contains configuration for your production application. These config settings can be googled because there are many people who have used Docker in production. This helps separate dev vs prod configuration with ease.

  1. Create a Dockerfile.dev with the following code:

  1. Create a docker-compose.yml with the following code:
  1. Create a .dockerignore and add node_modules to it. Any files/folders in the .dockerignore will not be copied over to the Docker container. Since node_modules is usually a large directory, adding this to a .dockerignore this will speed up your build times. Add other files/directories you do not want to be copied into your docker container here!
  1. Execute the following commands in the root directory of your project depending on what you need:

  2. docker-compose up -d --build : Builds the container using Dockerfile.dev and starts your docker container in detached mode

  3. docker-compose up -d Starts your docker container in detached mode

  4. docker-compose down ALWAYS run this command when stopping your container

  5. docker ps Lists your current active containers

After the initial build is successful execute docker ps and ensure that your container is running. If so, head on over to localhost:5000 where your application is hosted locally. You can change this port through the port setting in the docker-compose.yml .