DEV Community


Posted on


Creating Docker Container to Develop a Sails.JS Application with VSCode

Before I start a new project, just for fun or for a client, I like to have some basics set up. My editor of choice with its appropriate plugins, docker, so I don’t have to install things directly on the machine, and one very important thing, debugging has to be possible, with a proper tool, not prints.
At this moment, my editor of choice is VS Code.

Right now, I need to do some tests for a potential project with SailsJS. This project will not use, at least right now, the latest version of node or sails, so, this is one other point that docker will help me with.
I will also create a docker compose for the project, because the first thing I will try to do with sails in this setup is a simple crud.
So, if you want to follow along, you will need docker and docker compose installed. And also, visual code with the docker extension.

The code can be found here: ([]

The Docker Structure

The initial Dockerfile is this:

FROM node:6.16.0
VOLUME [ "/opt/app" ]
WORKDIR /opt/app
RUN chown -R node:node /opt/app
USER root
RUN cd /opt/app && npm install -g sails@0.11.2  && npm install -g node-inspect
USER node
CMD [ "tail", "-f", "/dev/null" ]

You can see that as I said before, the node version is not the latest, and sails version is an specific version I need to use. I also installed node-inspect that I will use to setup debugging later.

The ports that are exposed are for the application server and the debug port respectively.

The command at the end, is just so the docker container does not exit, because no process is running, and we can log in the container and create the project itself.

The docker compose is also simple:

version: '2'
   driver: bridge
    build: node
      - '${NODE_APP}:/opt/app:rw'
       - '1337:1337'
       - '9229:9229'
      - sails

Note that I the build parameter is building from node, which is the folder where my Dockerfile is.

The NODE_APP variable, is set a .env file, and is the path on the host machine where the project will be kept.

With that set up, we should be able to start the container.

## Creating the SailsJS project

If you want to use a project you already have, skip this. Once you log into the container, type:

```sails new test-project```

Let’s now create a very simple controller. The purpose of creating this, is just so I can check that the debugging configuration I am going to do next is working.
So to create a controller:

```sails generate api user```

This will create a *UserController.js* file inside of the */api/controllers* folder. To have something we can debug, inside the controller let’s create a function:

module.exports = {
list: function(req, res) {

This will simple redirect me to a view, or page, called list. So in the views folder of your project, create a *list.ejs* file, with some content, it can be as simple as a string.


For the routing to work, in the *config/routes.js* file, add this:

```'get /user/list': 'UserController.list'```

Which translates to, when a get is made to */user/list*, call the function *list* in the *UserController*.

To avoid receiving a message about the database migration every time the server is started, and also to not allow automatic migrations in the *config/model.js* file add the following line:

```migrate: 'safe'```

Now to start the server inside your docker container:

sails lift

The server will start on port 1337, and if you go to *http://localhost:1337/user/list*, you should see a page with the word *LIST* on it.
The only thing left to do now, is setup the debugging.

## Debugging

To be able to debug, in visual studio code, you should have the docker extension installed, and a debug configuration like the following:

"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/opt/app",
"protocol": "inspector"

Now, one last step: start the docker container with the appropriate command to boot the application ready to be debugged. To do this, replace the command in the docker file. Delete this line:

CMD [ "tail", "-f", "/dev/null" ]

And add this one:

CMD [ "node", "--inspect=", "/opt/app/app.js" ]

Rebuild and restart your container, and you should be ready to start debugging.

Top comments (0)