Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #84 from kmehant/node-postgres
Browse files Browse the repository at this point in the history
Adding development container defination for NodeJS + Postgres
  • Loading branch information
Chuxel committed Jun 20, 2019
2 parents 745e493 + 901edb9 commit 855f127
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 0 deletions.
38 changes: 38 additions & 0 deletions containers/javascript-node-lts-postgres/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------

FROM node:lts

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# Configure apt and install packages
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils 2>&1 \
#
# Verify git and needed tools are installed
&& apt-get install -y git procps \
#
# Remove outdated yarn from /opt and install via package
# so it can be easily updated via apt-get upgrade yarn
&& rm -rf /opt/yarn-* \
&& rm -f /usr/local/bin/yarn \
&& rm -f /usr/local/bin/yarnpkg \
&& apt-get install -y curl apt-transport-https lsb-release \
&& curl -sS https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/pubkey.gpg | apt-key add - 2>/dev/null \
&& echo "deb https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get -y install --no-install-recommends yarn \
#
# Install eslint globally
&& npm install -g eslint \
#
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Node.js & Postgres",
"dockerComposeFile": "docker-compose.yml",
"service": "web",
"workspaceFolder": "/workspace",

// Uncomment the line below if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",

// Uncomment the next line if you want to add in default container specific settings.json values
// "settings": { "workbench.colorTheme": "Quiet Light" },

// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "npm install",

"extensions": [
"dbaeumer.vscode-eslint"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------

version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile

volumes:
- ..:/workspace
# This lets you avoid setting up Git again in the container
- ~/.gitconfig:/root/.gitconfig

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

links:
- db

db:
image: postgres
restart: unless-stopped
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: pass
POSTGRES_USER: user
POSTGRES_DB: data

4 changes: 4 additions & 0 deletions containers/javascript-node-lts-postgres/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
README.md
test-project
.vscode
.npmignore
16 changes: 16 additions & 0 deletions containers/javascript-node-lts-postgres/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Server",
"program": "${workspaceFolder}/test-project/server.js",
"cwd": "${workspaceFolder}/test-project",
"preLaunchTask": "npm: install - test-project"
}
]
}
49 changes: 49 additions & 0 deletions containers/javascript-node-lts-postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Node.js (latest LTS) & Postgres

## Summary

*Develop applications in Node.js and Postgres. Includes Node.js, eslint, and yarn in a container linked to a Postgres DB container*

| Metadata | Value |
|----------|-------|
| *Contributors* | [Mehant](mailto:kmehant@gmail.com) |
| *Definition type* | Docker Compose |
| *Languages, platforms* | Node.js, JavaScript, Postgres DB |

This definition does not require any special steps to use. Just follow these steps:

1. If this is your first time using a development container, please follow the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started) to set up your machine.

2. To use VS Code's copy of this definition:
1. Start VS Code and open your project folder.
2. Press <kbd>F1</kbd> select and **Remote-Containers: Create Container Configuration File...** from the command palette.
3. Select the Node.js (latest LTS) & Postgres DB definition.

3. To use latest-and-greatest copy of this definition from the repository:
1. Clone this repository.
2. Copy the contents of `containers/javascript-node-lts-mongo/.devcontainer` to the root of your project folder.
3. Start VS Code and open your project folder.

4. After following step 2 or 3, the contents of the `.devcontainer` folder in your project can be adapted to meet your needs.

5. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** to start using the definition.

## Testing the definition

This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps:

1. If this is your first time using a development container, please follow the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started) to set up your machine.
2. Clone this repository.
3. Start VS Code, press <kbd>F1</kbd>, and select **Remote-Containers: Open Folder in Container...**
4. Select the `containers/javascript-node-lts-postgress` folder.
5. After the folder has opened in the container, press <kbd>F5</kbd> to start the project. This will automatically run `npm install` before starting it.
6. Once the project is running, press <kbd>F1</kbd> and select **Remote-Containers: Forward Port from Container...**
7. Select port 3000 and click the "Open Browser" button in the notification that appears.
8. You should see "Hello remote world! Successfully connected to database." after the page loads.
9. From here, you can add breakpoints or edit the contents of the `test-project` folder to do further testing.

## License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License. See [LICENSE](https://github.com/Microsoft/vscode-dev-containers/blob/master/LICENSE).
15 changes: 15 additions & 0 deletions containers/javascript-node-lts-postgres/test-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "docker_web_app",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "First Last <email address>",
"description": "Node JS + Postgres",
"dependencies": {
"bluebird": "^3.5.5",
"express": "^4.17.1",
"pg-promise": "^8.7.2"
}
}
50 changes: 50 additions & 0 deletions containers/javascript-node-lts-postgres/test-project/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*--------------------------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
*-------------------------------------------------------------------------------------------------------------*/

'use strict';

const express = require('express');
const promise = require('bluebird');

// Constants
const PORT = 3000;
const HOST = '0.0.0.0';

var result; // stores the connection result

const initOptions = {
promiseLib: promise // overriding the default (ES6 Promise);
};
const pgp = require('pg-promise')(initOptions);



// Database connection details;
const cn = {
host: 'db', // host of db container
port: 5432, // 5432 is the default;
database: 'data', // database name
user: 'user', // database user name
password: 'pass' // database password
};

const db = pgp(cn); // database instance;

db.one(' SELECT current_database();')
.then(
result = "Successfully connected to database."
)
.catch(error => {
result = "Failed: " + error
})

// creating an express app
const app = express();
app.get('/', async (req, res) => {
res.send('Hello, remote world: ' + result);
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

0 comments on commit 855f127

Please sign in to comment.