d6c4972024 | ||
---|---|---|
.. | ||
.devcontainer | ||
.vscode | ||
test-project | ||
.npmignore | ||
README.md | ||
environment.yml |
README.md
Anaconda (Python 3) & PostgreSQL
Summary
Develop Anaconda applications in Python3. Installs dependencies from your environment.yml file and the Python extension.
Metadata | Value |
---|---|
Contributors | The VS Code Python extension team |
Categories | Core, Languages |
Definition type | Docker Compose |
Supported architecture(s) | x86-64, aarch64/arm64 |
Works in Codespaces | Yes |
Container host OS support | Linux, macOS, Windows |
Container OS | Debian |
Languages, platforms | Python, Anaconda |
Using this definition
This definition creates two containers, one for Anaconda/Python and one for PostgreSQL. VS Code will attach to the Anaconda container, and from within that container the PostgreSQL container will be available on localhost
port 5432. The default database is named postgres
with a user of postgres
whose password is postgres
, and if desired this may be changed in docker-compose.yml
. Data is stored in a volume named postgres-data
.
While the definition itself works unmodified, you can also directly reference pre-built versions by updating the FROM
statement in your own Dockerfile
to the following. An example Dockerfile
is included in this repository.
mcr.microsoft.com/vscode/devcontainers/anaconda
(oranaconda:3
)
You can decide how often you want updates by referencing a semantic version of each image. For example:
mcr.microsoft.com/vscode/devcontainers/anaconda:0-3
mcr.microsoft.com/vscode/devcontainers/anaconda:0.202-3
mcr.microsoft.com/vscode/devcontainers/anaconda:0.202.0-3
See the python-3-anaconda history for information on the contents of each version and here for a complete list of available tags.
You also can connect to PostgreSQL from an external tool when using VS Code by updating .devcontainer/devcontainer.json
as follows:
"forwardPorts": [ "5432" ]
Adding another service
You can add other services to your docker-compose.yml
file as described in Docker's documentation. However, if you want anything running in this service to be available in the container on localhost, or want to forward the service locally, be sure to add this line to the service config:
# Runs the service on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:[$SERVICE_NAME]
Using Conda
This dev container and its associated anaconda image includes the conda
package manager. Additional packages installed using Conda will be downloaded from Anaconda or another repository if you configure one. To reconfigure Conda in this container to access an alternative repository, please see information on configuring Conda channels here.
Access to the Anaconda repository is covered by the Anaconda Terms of Service, which may require some organizations to obtain a commercial license from Anaconda. However, when this dev container or its associated image is used with GitHub Codespaces or GitHub Actions, all users are permitted to use the Anaconda Repository through the service, including organizations normally required by Anaconda to obtain a paid license for commercial activities. Note that third-party packages may be licensed by their publishers in ways that impact your intellectual property, and are used at your own risk.
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.
"console": "integratedTerminal"
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.
"forwardPorts": [5000]
The appPort
property publishes rather than forwards the port, so applications need to listen to *
or 0.0.0.0
for the application to be accessible externally. This conflicts with the defaults of some Python frameworks, but fortunately the forwardPorts
property does not have this limitation.
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.
Installing Node.js
Given JavaScript front-end web client code written for use in conjunction with a Python back-end often requires the use of Node.js-based utilities to build, this container also includes nvm
so that you can easily install Node.js. You can change the version of Node.js installed or disable its installation by updating the args
property in .devcontainer/docker-compose.yml
.
args:
NODE_VERSION: "14" # Set to "none" to skip Node.js installation
Installing a different version of Python
As covered in the user FAQ for Anaconda, you can install different versions of Python than the one in this image by running the following from a terminal:
conda install python=3.6
Or in a Dockerfile:
RUN conda install -y python=3.6
[Optional] Adding the contents of environment.yml to the image
For convenience, this definition will automatically install dependencies from the environment.yml
file in the parent folder when the container is built. You can change this behavior by altering this line in the .devcontainer/Dockerfile
:
RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \
&& rm -rf /tmp/conda-tmp
Adding the definition to your folder
-
If this is your first time using a development container, please see getting started information on setting up Remote-Containers or creating a codespace using GitHub Codespaces.
-
Start VS Code and open your project folder or connect to a codespace.
-
Press F1 select and Add Development Container Configuration Files... command for Remote-Containers or Codespaces.
Note: If needed, you can drag-and-drop the
.devcontainer
folder from this sub-folder in a locally cloned copy of this repository into the VS Code file explorer instead of using the command. -
Select this definition. You may also need to select Show All Definitions... for it to appear.
-
Finally, press F1 and run Remote-Containers: Reopen Folder in Container or Codespaces: Rebuild 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:
- If this is your first time using a development container, please follow the getting started steps to set up your machine.
- Clone this repository.
- Start VS Code, press F1, and select Remote-Containers: Open Folder in Container...
- Select the
containers/python-3-anaconda-postgres
folder. - After the folder has opened in the container, press F5 to run the
plot.py
script in the project. - You should see
Open test-project/plot.png to see the result!
in the terminal and atest-project/plot.png
file should be added to the folder after it runs with the plot result. - Next, open
test-project/plot.py
and press ctrl/cmd+a then shift+enter. - You should see the
matplotlib
output in the interactive window. - To run the
database.py
PostgreSQL connection script, navigate to the "Run and Debug" pane ( ctrl/cmd+shift+D) and selectPython database.py (Integrated Terminal)
from the dropdown and press F5. - You should see
DATABASE CONNECTED
andOne database in this database server is: postgres
in the terminal. - From here, you can add breakpoints or edit the contents of the
test-project
folder to do further testing.
Running Jupyter notebooks
Use this container to run Jupyter notebooks.
-
Edit the
./.devcontainer/devcontainer.json
file and add8888
in theforwardPorts
array:// Use 'forwardPorts' to make a list of ports inside the container available locally. "forwardPorts": [8888],
.
-
Edit the
./.devcontainer/devcontainer.json
file and add apostStartCommand
command to start the Jupyter notebook web app after the container is created. Use nohup so it isn't killed when the command finishes. Logs will appear innohup.out
.// Use 'postStartCommand' to run commands after the container is created. "postStartCommand": "nohup bash -c 'jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root &'",
-
View the terminal output to see the correct URL including the access token:
http://127.0.0.1:8888/?token=1234567
-
Open the URL in a browser. You can edit and run code from the web browser.
-
If you have the Jupyter extension installed, you can also edit and run code from VS Code.
License
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. See LICENSE