← Back
config docker docker-compose gist node tools vscode

.devcontainer for node

Another docker configuration for running node, compatible with vscode's remote containers plugin

This configuration has the advantage that no image building process is required (no Dockerfile):

.devcontainer/docker-compose.yml:

version: "3.4"

x-app: &app
image: node:10.16.3
volumes:
- ..:/workspace:cached
- node_modules:/workspace/node_modules
environment:
DATABASE_URL: postgres://postgres:postgres@postgres:5432
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
links:
- postgres

services:
bash:
<<: *app
command: /bin/bash -c "cd ./workspace && exec bash"
ports:
- 3000:3000
volumes:
- ..:/workspace:cached
- node_modules:/workspace/node_modules

vscode:
<<: *app
command: sleep infinity

volumes:
node_modules:

And the vscode configuration:

.devcontainer/devcontainer.json:

// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
"name": "EQT Ticket Widget",
"dockerComposeFile": "docker-compose.yml",
"service": "vscode",
"workspaceFolder": "/workspace",

// Use 'settings' to set *default* container specific settings.json values on container create.
// You can edit these settings after create using File > Preferences > Settings > Remote.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},

"runServices": [],

// "shutdownAction": "none",
//"postCreateCommand": "npm install",

"extensions": ["dbaeumer.vscode-eslint"]
}

Usage #

I tend to use two package.json. One for the .devcontainer folder:

.devcontainer/package.json:

{
"private": true,
"scripts": {
"bash": "docker-compose -p my-project-name --ports run bash"
}
}

And another one in the root folder, with aliases to the most common used scripts:

/package.json:

{
"scripts": {
"docker:bash": "cd .devcontainer && npm run bash"
}
}

🖖