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

Commit

Permalink
Improve Docker Compose definitions with databases (#509)
Browse files Browse the repository at this point in the history
* Improve visibility to database, ports in js-node-mongo

* Improve visibility to database, ports in js-node-postges

* Improve visibility to database, ports in php-mariadb

* Improve visibility to database, ports in python-3-postgres
  • Loading branch information
Chuxel authored Aug 21, 2020
1 parent 8089b28 commit b36ffd8
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 100 deletions.
8 changes: 8 additions & 0 deletions containers/javascript-node-mongo/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
ARG VARIANT=12
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:${VARIANT}

# Install MongoDB command line tools
ARG MONGO_TOOLS_VERSION=4.2
RUN curl -sSL "https://www.mongodb.org/static/pgp/server-${MONGO_TOOLS_VERSION}.asc" | (OUT=$(apt-key add - 2>&1) || echo $OUT) \
&& echo "deb http://repo.mongodb.org/apt/debian $(lsb_release -cs)/mongodb-org/${MONGO_TOOLS_VERSION} main" | tee /etc/apt/sources.list.d/mongodb-org-${MONGO_TOOLS_VERSION}.list \
&& apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get install mongodb-org-tools \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

# Update args in docker-compose.yaml to set the UID/GID of the "node" user.
ARG USER_UID=1000
ARG USER_GID=$USER_UID
Expand Down
11 changes: 5 additions & 6 deletions containers/javascript-node-mongo/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
// NOTE: Use "mongo" as the host name to connect to the database and update the
// VARIANT arg in docker-compose.yml to pick a Node.js version: 10, 12, 14
{
"name": "Node.js & Mongo DB",
"dockerComposeFile": "docker-compose.yml",
Expand All @@ -12,17 +13,15 @@

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint"
"dbaeumer.vscode-eslint",
"mongodb.mongodb-vscode"
]

// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],

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

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// "forwardPorts": [3000],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
Expand Down
14 changes: 12 additions & 2 deletions containers/javascript-node-mongo/.devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ services:
context: .
dockerfile: Dockerfile
args:
# Update VARIANT to pick a node version: 14, 12, 10
# Update VARIANT to pick a node version: 10, 12, 14
VARIANT: 12
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000

# Use "ports" to publish your web port locally. Consider "forwardPorts" in
# devcontainer.json instead if your app only allows connections from localhost.
# ports:
# - 3000:3000

volumes:
- ..:/workspace:cached

Expand All @@ -24,9 +29,14 @@ services:
links:
- mongo

# Note: The MongoDB host name is "mongo", not "localhost"
mongo:
image: mongo
restart: unless-stopped
volumes:
- /data/db


# Uncomment to allow access to mongodb from external tools
# ports:
# - 27017:27017

39 changes: 34 additions & 5 deletions containers/javascript-node-mongo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,45 @@

## Using this definition with an existing folder

> **Note:** Inside the container, you will find MongoDB running at `mongo:27017` rather than localhost.
This definition creates two containers, one for Node.js and one for MongoDB. VS Code will attach to the Node.js container, and from within that container the MongoDB container will be available with the hostname `mongo`.

While the definition itself works unmodified, it uses the `mcr.microsoft.com/vscode/devcontainers/javascript-node` image which includes `git`, `eslint`, `zsh`, [Oh My Zsh!](https://ohmyz.sh/), a non-root `vscode` user with `sudo` access, and a set of common dependencies for development. You can pick a different version of this image by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` to pick either Node.js version 10, 12, or 14.

```yaml
build:
context: .
dockerfile: Dockerfile
args:
VARIANT: 12
```
The MongoDB instance can be managed via the automatically installed MongoDB extension, or you can expose the database to your local machine by uncommenting the following line in `.devcontainer/docker-compose.yaml`:

```yaml
ports:
- 27017:27017
```


While the definition itself works unmodified, it uses the `mcr.microsoft.com/vscode/devcontainers/javascript-node` image which includes `git`, `eslint`, `zsh`, [Oh My Zsh!](https://ohmyz.sh/), a non-root `vscode` user with `sudo` access, and a set of common dependencies for development. [Node Version Manager](https://github.com/nvm-sh/nvm) (`nvm`) is also included in case you need to use a different version of Node.js than the one included in the image.

You can pick a different version of this image by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` to pick either Node.js version 10, 12, or 14.

```yaml
build:
context: .
dockerfile: Dockerfile
args:
VARIANT: 14
build:
context: .
dockerfile: Dockerfile
args:
VARIANT: 14
```

You can also expose MongoDB to your local machine by uncommenting the following lines in `.devcontainer/docker-compose.yml`:

```yaml
ports:
- 27017:27017
```

### Adding the definition to your project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
// NOTE: Use "db" as the host name to connect to the database and update the
// VARIANT arg in docker-compose.yml to pick a Node.js version: 10, 12, 14
{
"name": "Node.js & PostgreSQL",
"dockerComposeFile": "docker-compose.yml",
Expand All @@ -7,22 +8,31 @@

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
"terminal.integrated.shell.linux": "/bin/bash",
"sqltools.connections": [{
"name": "Container database",
"driver": "PostgreSQL",
"previewLimit": 50,
"server": "db",
"port": 5432,
"database": "postgres",
"username": "postgres",
"password": "LocalPassword",
}]
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint"
"dbaeumer.vscode-eslint",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg"
]

// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],

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

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// "forwardPorts": [3000],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ services:
context: .
dockerfile: Dockerfile
args:
# Update VARIANT to pick a node version: 14, 12, 10
# Update VARIANT to pick a node version: 10, 12, 14
VARIANT: 12
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000


# Use "ports" to publish your web port locally. Consider "forwardPorts" in
# devcontainer.json instead if your app only allows connections from localhost.
# ports:
# - 3000:3000

volumes:
- ..:/workspace:cached

Expand All @@ -24,13 +29,15 @@ services:
links:
- db

# Note: The PostgreSQL host name is "db", not "localhost"
db:
image: postgres
restart: unless-stopped
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: pass
POSTGRES_USER: user
POSTGRES_DB: data

POSTGRES_PASSWORD: LocalPassword
POSTGRES_USER: postgres
POSTGRES_DB: postgres

# Uncomment to allow access to PostgreSQL from external tools
# ports:
# - 5432:5432
31 changes: 20 additions & 11 deletions containers/javascript-node-postgres/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
# Node.js & PostgresSQL
# Node.js & PostgreSQL

## Summary

*Develop applications in Node.js and Postgres. Includes Node.js, eslint, and yarn in a container linked to a Postgres DB container*
*Develop applications in Node.js and PostgreSQL. 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 |
| *Works in Codespaces* | Yes |
| *Container host OS support* | Linux, macOS, Windows |
| *Languages, platforms* | Node.js, JavaScript, Postgres DB |
| *Languages, platforms* | Node.js, JavaScript, PostgreSQL DB |

## Using this definition with an existing folder
## Description

While the definition itself works unmodified, it uses the `mcr.microsoft.com/vscode/devcontainers/javascript-node` image which includes `git`, `eslint`, `zsh`, [Oh My Zsh!](https://ohmyz.sh/), a non-root `vscode` user with `sudo` access, and a set of common dependencies for development. [Node Version Manager](https://github.com/nvm-sh/nvm) (`nvm`) is also included in case you need to use a different version of Node.js than the one included in the image.
> **Note:** Inside the container, you will find PostgreSQL running at `db:5432` rather than localhost.
You can pick a different version of this image by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` with one of the following: 10, 12, or 14.
This definition creates two containers, one for Node.js and one for PostgreSQL. VS Code will attach to the Node.js container, and from within that container the PostgreSQL container will be available with the hostname `db`. The default database is named `postgres` with a user of `postgres` whose password is `LocalPassword`, and if desired this may be changed in `docker-compose.yml`.

While the definition itself works unmodified, it uses the `mcr.microsoft.com/vscode/devcontainers/javascript-node` image which includes `git`, `eslint`, `zsh`, [Oh My Zsh!](https://ohmyz.sh/), a non-root `vscode` user with `sudo` access, and a set of common dependencies for development. You can pick a different version of this image by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` to pick either Node.js version 10, 12, or 14.

```yaml
build:
context: .
dockerfile: Dockerfile
args:
VARIANT: 12
```
The PostgreSQL instance can be managed via the automatically installed SQLTools extension, or you can expose the database to your local machine by uncommenting the following line in `.devcontainer/docker-compose.yaml`:

```yaml
build:
context: .
dockerfile: Dockerfile
args:
VARIANT: 14
ports:
- 5432:5432
```

### Adding the definition to your project
Expand Down
6 changes: 3 additions & 3 deletions containers/javascript-node-postgres/test-project/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const HOST = '0.0.0.0';
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
database: 'postgres', // database name
user: 'postgres', // database user name
password: 'LocalPassword' // database password
};
const initOptions = {
promiseLib: promise
Expand Down
2 changes: 1 addition & 1 deletion containers/php-mariadb/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM mcr.microsoft.com/vscode/devcontainers/php:${VARIANT}
# Install MariaDB client
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get install -y mariadb-client \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/* && rm -rf /tmp/library-scripts
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

# Update args in docker-compose.yaml to set the UID/GID of the "vscode" user.
ARG USER_UID=1000
Expand Down
24 changes: 16 additions & 8 deletions containers/php-mariadb/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/php-7
// NOTE: Use "mariadb" as the host name to connect to the database and update
// the VARIANT arg in docker-compose.yml to pick a PHP version: 7, 7.3, 7.4
{
"name": "PHP & MariaDB",
"dockerComposeFile": "docker-compose.yml",
"service": "php",
"workspaceFolder": "/workspace",

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
"terminal.integrated.shell.linux": "/bin/bash",
"sqltools.connections": [{
"name": "Container database",
"driver": "MariaDB",
"server": "mariadb",
"port": 3306,
"database": "mariadb",
"username": "root",
"password": " just-for-testing",
}]
},

// Add the IDs of extensions you want installed when the container is created.
Expand All @@ -18,14 +28,12 @@
"mtxr.sqltools-driver-mysql"
],

"workspaceFolder": "/workspace",

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// For use with PHP (e.g.php -S localhost:8080)
"forwardPorts": [8080]

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "php -v",

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode",
// "remoteUser": "vscode"
}
24 changes: 14 additions & 10 deletions containers/php-mariadb/.devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@ services:
VARIANT: "7"
INSTALL_NODE: "true"
NODE_VERSION: "lts/*"
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000

volumes:
- ..:/workspace:cached

ports:
# For use with PHP (e.g. `php -S localhost:8080`)
- "8080:8080"
# Use "ports" to publish your web port locally. Consider "forwardPorts" in
# devcontainer.json instead if your app only allows connections from localhost.
# ports:
# - 8080:8080

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

links:
- mariadb

# Note: The MariaDB host name is "db", not "localhost"
mariadb:
image: mariadb:10.4
expose:
# Expose mariadb port to php service (Access as hostname "mariadb" from within php container)
- "3306"
# Uncomment to allow access to mariadb from external tools
# ports:
# - "3306:3306"
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: just-for-testing
MYSQL_DATABASE: VscodeDev
MYSQL_DATABASE: mariadb

# Uncomment to allow access to mariadb from external tools
# ports:
# - "3306:3306"

13 changes: 0 additions & 13 deletions containers/php-mariadb/.vscode/settings.json

This file was deleted.

Loading

0 comments on commit b36ffd8

Please sign in to comment.