
#Docker nodejs how to
To start our application in debug mode, we need to add a line to our package.json file to tell npm how to start our application in debug mode. The reason we use mongo is because that is what we have named our MongoDB service in the Compose file as. So we are now able to use “mongo” in our connection string. Second, youll need to expose the debug port (9229) and the server port (3000) so either the browser or VSCode can watch it and attach a debugger statement. One other really cool feature of using a Compose file is that we have service resolution set up to use the service names. First, youll need to execute the Docker build command on your folder to build the app running docker build -t app.
#Docker nodejs code
We are also mapping our local source code into the running container so that we can make changes in our text editor and have those changes picked up in the container. We are exposing port 9229 so that we can attach a debugger. We can declaratively do that in the Compose file. This Compose file is super convenient as we do not have to type all the parameters to pass to the docker run command. :/app command : npm run debug mongo : image : mongo:4.2.8 ports : - 27017:27017 volumes : - mongodb:/data/db - mongodb_config:/data/configdb volumes : mongodb : mongodb_config : Version : ' 3.8' services : notes : build : context. We’ll create one for the data and one for configuration of MongoDB. Let’s use the managed volumes feature that docker provides instead of using bind mounts. Instead of downloading MongoDB, installing, configuring and then running the Mongo database as a service, we can use the Docker Official Image for MongoDB and run it in a container.īefore we run MongoDB in a container, we want to create a couple of volumes that Docker can manage to store our persistent data and configuration. Finally, we’ll take a look at connecting a debugger to our application running inside a container.

Then we’ll pull everything together into a compose file which will allow us to setup and run a local development environment with one command.

Local database and containersįirst, we’ll take a look at running a database in a container and how we use volumes and networking to persist our data and allow our application to talk with the database. We’ll use Docker to build our images and Docker Compose to make everything a whole lot easier. In this module, we’ll walk through setting up a local development environment for the application we built in the previous modules. Work through the steps to build an image and run it as a containerized application in Run your image as a container.
