Jupyter Docker Will Not Read Data Subfolders
Python in a container
In this guide you will acquire how to:
- Create a
Dockerfilefile describing a simple Python container. - Build, run, and verify the functionality of a Django, Flask, or Full general Python app.
- Debug the app running in a container.
Prerequisites
- Docker Desktop and the VS Code Docker extension must exist installed as described in the overview.
Create a Python project
If you don't have a Python project already, follow the tutorial Getting started with Python.
Notation: If you desire to containerize a complete Django or Flask web app, you can use one of the post-obit samples:
python-sample-vscode-django-tutorial, which is the result of following the Django Tutorial
python-sample-vscode-flask-tutorial, which is the result of following the Flask Tutorial
Afterward verifying your app runs properly, you can now Dockerize your application.
Add Docker files to the project
-
Open up the project folder in VS Code.
-
Open the Control Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P)) and use the Docker: Add Docker Files to Workspace... command:
-
When the prompt appears, select Python: Django, Python: Flask, or Python: General equally the app type. For this tutorial, we'll focus on the Python: Full general case, only volition also include notes for Django and Flask.
-
Enter the relative path to the app's entry point. This excludes the workspace folder you get-go from. If y'all created a python app with
hello.pyaccording to the Python tutorial, choose that.Note: According to official Django documentation, this path is commonly
manage.py(root folder) orsubfolder_name/manage.py. According to official Flask documentation, this is the path to where you create your Flask instance.Tip: You may also enter the path to a folder name as long as this folder includes a
__main__.pyfile. -
Select the port number. We recommend selecting port 1024 or above to mitigate security concerns from running equally a root user.
Note: If Python: Django or Python: Flask was selected, specify app port for local development. Django defaults to port 8000, while Flask defaults to port 5000; however, any unused port will work.
-
Select either Yes or No when prompted to include Docker Compose files. If yous select Yes, you will demand to verify the path to your
wsgi.pyfile in theDockerfileto run the Compose Up control successfully. Compose is typically used when running multiple containers at in one case. -
With all of this information, the Docker extension creates the following files:
-
A
Dockerfile. To learn more about IntelliSense in this file, refer to the overview. -
A
.dockerignorefile to reduce the epitome size by excluding files and folders that aren't needed such as.git,.vscode, and__pycache__. -
If Docker Etch was selected, a
docker-compose.ymlanddocker-compose.debug.ymlfile. -
If one does not already be, a
requirements.txtfile for capturing all app dependencies.
Of import Note: To apply our setup, the Python framework (Django/Flask) and Gunicorn must exist included in the
requirements.txtfile. If the virtual surround/host machine already has these prerequisites installed and is supposed to exist identical to the container environment, ensure app dependencies are ported over by runningpip freeze > requirements.txtin the last. This will overwrite your currentrequirements.txtfile. -
Add together an environment variable to the image
This step is only required if you take surroundings variables that need to be set in the container's environment.
The Docker Extension helps you writer Dockerfiles past using IntelliSense to provide automobile-completions and contextual help. To see this characteristic in action:
-
Open the
Dockerfile. -
Underneath the
EXPOSEargument, type ⌃Space (Windows, Linux Ctrl+Space) to trigger IntelliSense and gyre toENV.
-
Press Tab or Enter to complete the argument, then set the
cardinalto the proper name of the variable, and set thevalue.
For more than information about setting and using environment variables in the Dockerfile, come across the ENV instruction and Environment replacement department in the Docker documentation.
Gunicorn modifications for Django and Flask apps
To requite Python Web Developers a great starting bespeak, nosotros chose to employ Gunicorn as the default spider web server. Since information technology is referenced in the default Dockerfile, it is included as a dependency in the requirements.txt file.
Note: To use Gunicorn as your spider web server, it must be included in the
requirements.txtfile as an app dependency. It does non need to be installed in your virtual environment/host motorcar. The Gunicorn entry betoken is overridden locally if your app is run with Python: Django or Python: Flask.
Django apps
To use Gunicorn, information technology must demark to an application callable (what the application server uses to communicate with your code) as an entry point. This callable is alleged in the wsgi.py file of a Django awarding. To achieve this bounden, the final line in the Dockerfile says:
CMD [ "gunicorn" , "--bind" , "0.0.0.0:8000" , "{workspace_folder_name}.wsgi" ] If your projection does not follow Django's default project structure (that is, a workspace folder and a wsgi.py file within a subfolder named the same as the workspace) you must overwrite the Gunicorn entry bespeak in the Dockerfile to locate the correct wsgi.py file.
Tip: If your
wsgi.pyfile is in the root folder, the final argument in the command higher up will be"wsgi". Within subfolders, the statement would be"subfolder1_name.subfolder2_name.wsgi".
Flask apps
To use Gunicorn, information technology must bind to an application callable (what the application server uses to communicate with your code) as an entry bespeak. This callable corresponds with the file location and variable name of your created Flask instance. According to official Flask Documentation, users by and large create a Flask instance in the main module or in the __init__.py file of their package in this mode:
from flask import Flask app = Flask( __name__ ) # Flask instance named app To accomplish this binding, the final line in the Dockerfile says:
CMD [ "gunicorn" , "--bind" , "0.0.0.0:5000" , "{subfolder}.{module_file}:app" ] During the Docker: Add Docker Files to Workspace... command, you configure the path to the Flask instance, withal, the Docker extension assumes your Flask instance variable is named app. If this is not the case, you must alter the variable name in the Dockerfile.
Tip: If your main module was in the root folder equally a file named
chief.pyand had a Flask instance variable was namedmyapp, the final argument in the command to a higher place will be"principal:myapp". Within subfolders, the statement would exist"subfolder1_name.subfolder2_name.principal:myapp".
Build, run, and debug the container
The Docker: Add Docker Files to Workspace... command automatically creates a Docker launch configuration to build and run your container in debug mode. To debug your Python app container:
-
Navigate to the file that contains your app's startup code, and set a breakpoint.
-
Navigate to Run and Debug and select Docker: Python - General, Docker: Python - Django, or Docker: Python - Flask, equally advisable.
-
Start debugging using the F5 key.
- The Docker image builds.
- The Docker container runs.
- The python debugger stops at the breakpoint.
-
Step over this line once.
-
When gear up, press continue.
The Docker extension will launch your browser to a randomly mapped port:
Tip: To alter your Docker build settings, such as changing the paradigm tag, navigate to
.vscode -> tasks.jsonnether thedockerBuildattribute in thedocker-buildjob. Use IntelliSense within the file ( ⌃Space (Windows, Linux Ctrl+Infinite)) to display all other valid directives.
Use the Docker Explorer
The Docker Explorer provides an interactive experience to examine and manage Docker assets such as containers, images, and and so on. To see an case:
-
Navigate to the Docker Explorer.
-
In the Containers tab, correct-click on your container and choose View Logs.
-
The output will exist displayed in the terminal.
Next steps
You're done! Now that your container is ready, you may want to:
- Debug with Docker Etch
- Customize how you debug Python apps in a container
- Customize your Docker build and run tasks
- Create a container registry using the Azure portal
- Button your Django image to an Azure Container Registry
- Deploy a containerized app to Azure App Service
- Acquire about using Docker Compose
Source: https://code.visualstudio.com/docs/containers/quickstart-python
0 Response to "Jupyter Docker Will Not Read Data Subfolders"
Enregistrer un commentaire