How to connect Visual Studio Code to my Docker Container?

colleagues.

I just installed Debian 12 on my PC for work; I also use Visual Studio Code and Docker. In my company’s project, we use ESLint, Prettier, and Husky. The issue is that I’m using my development environment with Docker, and therefore, the libraries that allow me to use the aforementioned tools are not utilized by my Visual Studio Code.

This is my ‘./0.Project_info/Dockerfile’ (which I use for my local development environment):

FROM alpine

RUN apk add --update nodejs npm

COPY ["package.json","package-lock.json","/usr/src/"]

WORKDIR /usr/src

RUN npm ci

COPY [".","/usr/src/"]

WORKDIR /usr/src

This is my ‘./docker-compose.yml’:

version: '3.8'

services:
  app:
    container_name: dasboard_api
    image: dasboard_api:1.0.0
    build: .
    command: npm run start
    restart: on-failure
    volumes:
      - .:/usr/src
      - /usr/src/node_modules
    ports:
      - ${PORT}:${PORT}
    networks:
      - dasboard_api

networks:
  dasboard_api:

This is my ‘./docker-compose.override.yml’ (which I use for my local development):

services:
  app:
    build:
      context: .
      dockerfile: ./0.Project_info/Dockerfile
    command: 'npm run dev'

This is the configuration of my Visual Studio Code:

{
    "workbench.sideBar.location": "right",
    "workbench.colorTheme": "Sublime Monokai",
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "eslint.options": {
        "configFile": "/path/in/container/.eslintrc.js"
    },
    "eslint.nodePath": "/path/in/container/node_modules"
}

And finally, this is my ‘package.json’:

{
  "name": "api",
  "version": "1.0.0",
  "description": "Removed for security",
  "main": "app.js",
  "scripts": {
    "preinstall": "npx only-allow npm",
    "prepare": "husky install",
    "start": "node ./bin/www",
    "dev": "nodemon ./bin/www",
    "format": "prettier --write .",
    "format:check": "prettier --check \"{controllers,services,docs,helpers,middlewares,routes}/**/*.js\"",
    "format:write": "prettier --write \"{controllers,services,docs,helpers,middlewares,routes}/**/*.js\"",
    "lint": "eslint .",
    "lint:check": "eslint \"{controllers,services,docs,helpers,middlewares,routes}/**/*.js\"",
    "lint:fix": "eslint \"{controllers,services,docs,helpers,middlewares,routes}/**/*.js\" --fix",
    "docker:build:dev": "docker-compose -p dash_dev build && docker-compose -p dash_dev down && docker-compose -p dash_dev up -d",
    "docker:build:prod": "docker-compose -p dash_prod build && docker-compose -p dash_prod down && docker-compose -p dash_prod up -d",
    "docker:restart:dev": "docker-compose -p dash_dev restart",
    "docker:restart:prod": "docker-compose -p dash_prod restart"
  },
  "lint-staged": {
    "**/*": "prettier --write --ignore-unknown"
  },
  "author": "Removed for security",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-s3": "^3.445.0",
    "axios": "^0.21.1",
    "bcryptjs": "^2.4.3",
    "bottleneck": "^2.19.5",
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "crypto-js": "^4.1.1",
    "debug": "^4.3.4",
    "dotenv": "^10.0.0",
    "ejs": "^3.1.9",
    "express": "^4.17.1",
    "express-validator": "^6.12.0",
    "jsonwebtoken": "^9.0.0",
    "moment": "^2.29.1",
    "morgan": "^1.10.0",
    "mysql2": "^2.2.5",
    "nodemailer": "^6.7.2",
    "pcloud-sdk-js": "^1.4.3",
    "qs": "^6.7.0",
    "sequelize": "^6.6.2",
    "swagger-ui-express": "^4.5.0",
    "vimeo": "^2.3.1",
    "written-number": "^0.11.1"
  },
  "devDependencies": {
    "del-cli": "^4.0.0",
    "eslint": "^8.32.0",
    "eslint-config-prettier": "^8.6.0",
    "eslint-plugin-prettier": "^4.2.1",
    "husky": "^8.0.3",
    "lint-staged": "^15.0.2",
    "nodemon": "^3.0.1",
    "prettier": "^2.8.3",
    "sequelize-auto": "^0.8.3"
  }
}

How can I make the other plugins work? Below, I’ll describe the behavior I had on Windows without using Docker:

  • When there was an unused variable, it showed an error in the editor.
  • On save, or manually formatting the code, it adjusted it according to the rules.
  • When committing, Husky performed a series of pre-commits.

  • You can just apt-get install nodejs to get Node directly on your host, which seems like it solves your Docker-related problems. Is there a reason to use Docker in this workflow?

    – 

Leave a Comment