From b36ffd8c7642a9bd3932d117711a77be73fd5abd Mon Sep 17 00:00:00 2001 From: Chuck Lantz Date: Fri, 21 Aug 2020 11:10:27 -0700 Subject: [PATCH] Improve Docker Compose definitions with databases (#509) * 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 --- .../.devcontainer/Dockerfile | 8 ++++ .../.devcontainer/devcontainer.json | 11 +++--- .../.devcontainer/docker-compose.yml | 14 ++++++- containers/javascript-node-mongo/README.md | 39 ++++++++++++++++--- .../.devcontainer/devcontainer.json | 24 ++++++++---- .../.devcontainer/docker-compose.yml | 23 +++++++---- containers/javascript-node-postgres/README.md | 31 +++++++++------ .../test-project/server.js | 6 +-- .../php-mariadb/.devcontainer/Dockerfile | 2 +- .../.devcontainer/devcontainer.json | 24 ++++++++---- .../.devcontainer/docker-compose.yml | 24 +++++++----- containers/php-mariadb/.vscode/settings.json | 13 ------- containers/php-mariadb/README.md | 5 ++- .../.devcontainer/devcontainer.json | 23 +++++++---- .../.devcontainer/docker-compose.yml | 14 ++++++- containers/python-3-postgres/README.md | 35 +++++++++-------- 16 files changed, 196 insertions(+), 100 deletions(-) delete mode 100644 containers/php-mariadb/.vscode/settings.json diff --git a/containers/javascript-node-mongo/.devcontainer/Dockerfile b/containers/javascript-node-mongo/.devcontainer/Dockerfile index 796ff88dba..e12183ee01 100644 --- a/containers/javascript-node-mongo/.devcontainer/Dockerfile +++ b/containers/javascript-node-mongo/.devcontainer/Dockerfile @@ -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 diff --git a/containers/javascript-node-mongo/.devcontainer/devcontainer.json b/containers/javascript-node-mongo/.devcontainer/devcontainer.json index c112e57f7e..0abaf3d486 100644 --- a/containers/javascript-node-mongo/.devcontainer/devcontainer.json +++ b/containers/javascript-node-mongo/.devcontainer/devcontainer.json @@ -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", @@ -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", diff --git a/containers/javascript-node-mongo/.devcontainer/docker-compose.yml b/containers/javascript-node-mongo/.devcontainer/docker-compose.yml index c50f4d56ea..690a967da9 100644 --- a/containers/javascript-node-mongo/.devcontainer/docker-compose.yml +++ b/containers/javascript-node-mongo/.devcontainer/docker-compose.yml @@ -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 @@ -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 + diff --git a/containers/javascript-node-mongo/README.md b/containers/javascript-node-mongo/README.md index 7643859a2d..a95ef0d283 100644 --- a/containers/javascript-node-mongo/README.md +++ b/containers/javascript-node-mongo/README.md @@ -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 diff --git a/containers/javascript-node-postgres/.devcontainer/devcontainer.json b/containers/javascript-node-postgres/.devcontainer/devcontainer.json index d183dc41d6..94bffa9876 100644 --- a/containers/javascript-node-postgres/.devcontainer/devcontainer.json +++ b/containers/javascript-node-postgres/.devcontainer/devcontainer.json @@ -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", @@ -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", diff --git a/containers/javascript-node-postgres/.devcontainer/docker-compose.yml b/containers/javascript-node-postgres/.devcontainer/docker-compose.yml index 75c5a8f172..f5554290aa 100644 --- a/containers/javascript-node-postgres/.devcontainer/docker-compose.yml +++ b/containers/javascript-node-postgres/.devcontainer/docker-compose.yml @@ -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 @@ -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 diff --git a/containers/javascript-node-postgres/README.md b/containers/javascript-node-postgres/README.md index c324aaf3e2..0d3e2b3a7f 100644 --- a/containers/javascript-node-postgres/README.md +++ b/containers/javascript-node-postgres/README.md @@ -1,8 +1,8 @@ -# 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 | |----------|-------| @@ -10,20 +10,29 @@ | *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 diff --git a/containers/javascript-node-postgres/test-project/server.js b/containers/javascript-node-postgres/test-project/server.js index 9d851bf51a..60d37c37f8 100644 --- a/containers/javascript-node-postgres/test-project/server.js +++ b/containers/javascript-node-postgres/test-project/server.js @@ -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 diff --git a/containers/php-mariadb/.devcontainer/Dockerfile b/containers/php-mariadb/.devcontainer/Dockerfile index 6cb47ef40c..50cf024a0a 100644 --- a/containers/php-mariadb/.devcontainer/Dockerfile +++ b/containers/php-mariadb/.devcontainer/Dockerfile @@ -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 diff --git a/containers/php-mariadb/.devcontainer/devcontainer.json b/containers/php-mariadb/.devcontainer/devcontainer.json index 1349b3bb83..953abe255b 100644 --- a/containers/php-mariadb/.devcontainer/devcontainer.json +++ b/containers/php-mariadb/.devcontainer/devcontainer.json @@ -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. @@ -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" } diff --git a/containers/php-mariadb/.devcontainer/docker-compose.yml b/containers/php-mariadb/.devcontainer/docker-compose.yml index a463ddf261..828f48bc35 100644 --- a/containers/php-mariadb/.devcontainer/docker-compose.yml +++ b/containers/php-mariadb/.devcontainer/docker-compose.yml @@ -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" diff --git a/containers/php-mariadb/.vscode/settings.json b/containers/php-mariadb/.vscode/settings.json deleted file mode 100644 index 62fe8ae872..0000000000 --- a/containers/php-mariadb/.vscode/settings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "sqltools.connections": [ - { - "database": "VscodeDev", - "dialect": "MariaDB", - "name": "Docker-Compose MariaDB", - "password": "just-for-testing", - "port": 3306, - "server": "mariadb", - "username": "root" - } - ] -} \ No newline at end of file diff --git a/containers/php-mariadb/README.md b/containers/php-mariadb/README.md index 8923174c1a..b0c95c81d0 100644 --- a/containers/php-mariadb/README.md +++ b/containers/php-mariadb/README.md @@ -3,6 +3,7 @@ ## Summary Develop PHP based applications with MariaDB (MySQL Compatible). Includes necessary extensions and tools for both PHP and MariaDB. + | Metadata | Value | |----------|-------| | *Contributors* | Richard Morrill [github.com/ThoolooExpress](https://github.com/ThoolooExpress) | @@ -13,13 +14,15 @@ Develop PHP based applications with MariaDB (MySQL Compatible). Includes necess ## Description +> **Note:** Inside the container, you will find PostgreSQL running at `mariadb:3306` rather than localhost. + This definition creates two containers, one for PHP and one for MariaDB. Code will attach to the PHP container, and from within that container the MariaDB container will be available with the hostname `mariadb`. The MariaDB instance can be managed via the automatically installed SQLTools extension, or from the container's command line with: ```bash mariadb -h mariadb -u root -p ``` -The password is defined by default as `just-for-testing`, and if desired this may be changed in `docker-compose.yml`. +The default database is called `mariadb` with a `root` user password of `just-for-testing`, and if desired this may be changed in `docker-compose.yml`. ## Using this definition with an existing folder diff --git a/containers/python-3-postgres/.devcontainer/devcontainer.json b/containers/python-3-postgres/.devcontainer/devcontainer.json index 7836b3e48f..d733ad929b 100644 --- a/containers/python-3-postgres/.devcontainer/devcontainer.json +++ b/containers/python-3-postgres/.devcontainer/devcontainer.json @@ -1,5 +1,5 @@ -// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml. -// You can also update the VARIANT arg in docker-compose.yml to pick a Python version: 3, 3.8, 3.7, 3.6 +// NOTE: Use "db" as the host name to connect to the database and update the +// VARIANT arg in docker-compose.yml to pick a Python version: 3, 3.8, 3.7, 3.6 { "name": "Python 3 & PostgreSQL", "dockerComposeFile": "docker-compose.yml", @@ -9,6 +9,16 @@ // Set *default* container specific settings.json values on container create. "settings": { "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", + }], "python.pythonPath": "/usr/local/bin/python", "python.linting.enabled": true, "python.linting.pylintEnabled": true, @@ -26,17 +36,16 @@ // Add the IDs of extensions you want installed when the container is created. "extensions": [ - "ms-python.python" + "ms-python.python", + "mtxr.sqltools", + "mtxr.sqltools-driver-pg" ] - // Uncomment the next line if you want start specific services in your Docker Compose config. - // "runServices": [], - // Uncomment the next line 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": [5000], // Use 'postCreateCommand' to run commands after the container is created. // "postCreateCommand": "pip install --user -r requirements.txt", diff --git a/containers/python-3-postgres/.devcontainer/docker-compose.yml b/containers/python-3-postgres/.devcontainer/docker-compose.yml index 20e705a6f2..3163522a96 100644 --- a/containers/python-3-postgres/.devcontainer/docker-compose.yml +++ b/containers/python-3-postgres/.devcontainer/docker-compose.yml @@ -12,9 +12,15 @@ services: args: # Update VARIANT to pick a python version: 3, 3.8, 3.7, 3.6 VARIANT: 3 + # 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 app port locally. However, "forwardPorts" in devcontainer.json + # works better with framework defaults that only allow connections from localhost (e.g. Flask) + # ports: + # - 5000:5000 + volumes: - ..:/workspace:cached @@ -27,7 +33,11 @@ services: db: image: postgres restart: unless-stopped - ports: - - 5432:5432 environment: + POSTGRES_USER: postgres + POSTGRES_DB: postgres POSTGRES_PASSWORD: LocalPassword + + # Uncomment to allow access to PostgreSQL from external tools + # ports: + # - 5432:5432 diff --git a/containers/python-3-postgres/README.md b/containers/python-3-postgres/README.md index c96bf109b2..b916bd131e 100644 --- a/containers/python-3-postgres/README.md +++ b/containers/python-3-postgres/README.md @@ -14,25 +14,28 @@ ## Using this definition with an existing folder -### Configuration +> **Note:** Inside the container, you will find PostgreSQL running at `db:5432` rather than localhost. -While the definition itself works unmodified, you can select the version of Python the container uses by updating the `VARIANT` arg in the included `.devcontainer/docker-compose.yml` (and rebuilding if you've already created the container). +This definition creates two containers, one for Python and one for PostgreSQL. VS Code will attach to the Python 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/python` 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 and Python tools for development. You can pick a different version of this image by updating the `VARIANT` arg in `.devcontainer/docker-compose.yml` to pick either Python version 3, 3.8, 3.7, or 3.6. ```yaml -args: - VARIANT": 3.7 +build: + context: . + dockerfile: Dockerfile + args: + VARIANT: 3.7 ``` -This allows you to pick from one of the following pre-built images: - -- `mcr.microsoft.com/vscode/devcontainers/python:3` (latest) -- `mcr.microsoft.com/vscode/devcontainers/python:3.6` -- `mcr.microsoft.com/vscode/devcontainers/python:3.7` -- `mcr.microsoft.com/vscode/devcontainers/python:3.8` +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`: -Alternatively, you can replace `.devcontainer/Dockerfile` in this definition with [`base.Dockerfile` from the Python 3 definition](../python-3/.devcontainer/base.Dockerfile) to fully customize your environment. +```yaml +ports: + - 5432:5432 +``` -#### Installing or updating Python utilities +### Installing or updating Python utilities This container installs all Python development utilities using [pipx](https://pipxproject.github.io/pipx/) to avoid impacting the global Python environment. You can use this same utility add additional utilities in an isolated environment. For example: @@ -42,7 +45,7 @@ pipx install prospector See the [pipx documentation](https://pipxproject.github.io/pipx/docs/) for additional information. -#### Debug Configuration +### Debug Configuration Note that only the integrated terminal is supported by the Remote - Containers extension. You may need to modify `launch.json` configurations to include the following value if an external console is used. @@ -50,7 +53,7 @@ Note that only the integrated terminal is supported by the Remote - Containers e "console": "integratedTerminal" ``` -#### Using the forwardPorts property +### Using the forwardPorts property By default, frameworks like Flask only listens to localhost inside the container. As a result, we recommend using the `forwardPorts` property (available in v0.98.0+) to make these ports available locally. @@ -62,7 +65,7 @@ The `ports` property in `docker-compose.yml` [publishes](https://docs.docker.com If you've already opened your folder in a container, rebuild the container using the **Remote-Containers: Rebuild Container** command from the Command Palette (F1) so the settings take effect. -#### [Optional] Building your requirements into the container image +### [Optional] Building your requirements into the container image If your requirements rarely change, you can include the contents of `requirements.txt` in the container by adding the following to your `Dockerfile`: @@ -74,7 +77,7 @@ RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requ Since `requirements.txt` is likely in the folder you opened rather than the `.devcontainer` folder, be sure to include `context: ..` under `build` in `docker-compose.yml`. This allows the Dockerfile to access everything in the opened folder instead of just the contents of the `.devcontainer` folder. -#### [Optional] Allowing the non-root vscode user to pip install globally without sudo +### [Optional] Allowing the non-root vscode user to pip install globally without sudo You can opt into using the `vscode` non-root user in the container by adding `"remoteUser": "vscode"` to `devcontainer.json`. However, by default, this you will need to use `sudo` to perform global pip installs.