Skip to content

Latest commit

 

History

History

anaconda-postgres

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Anaconda (Python 3) & PostgreSQL (anaconda-postgres)

Develop Anaconda & PostgreSQL applications in Python3. Installs dependencies from your environment.yml file and the Python extension.

This template references an image that was pre-built to automatically include needed devcontainer.json metadata.

  • Image: mcr.microsoft.com/devcontainers/cpp (source)
  • Applies devcontainer.json contents from image: Yes (source)

Using this template

This template creates two containers, one for Anaconda and one for PostgreSQL. You will be connected 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 .devcontainer/.env. Data is stored in a volume named postgres-data.

While the template itself works unmodified, it uses the mcr.microsoft.com/devcontainers/anaconda image which includes git, a non-root vscode user with sudo access, and a set of common dependencies and Python tools for development.

You also can connect to PostgreSQL from an external tool when connecting to the Dev Container from a local tool by updating .devcontainer/devcontainer.json as follows:

"forwardPorts": [ "5432" ]

Once the PostgreSQL container has port forwarding enabled, it will be accessible from the Host machine at localhost:5432. The PostgreSQL Documentation has:

  1. An Installation Guide for PSQL a CLI tool to work with a PostgreSQL database.
  2. Tips on populating data in the database.

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.

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 template 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

Running Jupyter notebooks

Use this container to run Jupyter notebooks.

  1. Edit the ./.devcontainer/devcontainer.json file and add 8888 in the forwardPorts array:

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

.

  1. Edit the ./.devcontainer/devcontainer.json file and add a postStartCommand 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 in nohup.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 &'",
  2. View the terminal output to see the correct URL including the access token:

     http://127.0.0.1:8888/?token=1234567
  3. Open the URL in a browser. You can edit and run code from the web browser.

  4. If you have the Jupyter extension installed, you can also edit and run code from VS Code.

Using the forwardPorts property

By default, web frameworks and tools often only listen to localhost inside the container. As a result, we recommend using the forwardPorts property to make these ports available locally.

"forwardPorts": [9000]

The ports property in docker-compose.yml publishes rather than forwards the port. This will not work in a cloud environment like Codespaces and applications need to listen to * or 0.0.0.0 for the application to be accessible externally. Fortunately the forwardPorts property does not have this limitation.


Note: This file was auto-generated from the devcontainer-template.json. Add additional notes to a NOTES.md.